Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message interaction #283

Merged
merged 10 commits into from
May 15, 2021
11 changes: 11 additions & 0 deletions common/src/main/kotlin/entity/DiscordMessage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.kord.common.entity

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.common.entity.optional.OptionalInt
Expand Down Expand Up @@ -99,6 +100,7 @@ data class DiscordMessage(
val stickers: Optional<List<DiscordMessageSticker>> = Optional.Missing(),
@SerialName("referenced_message")
val referencedMessage: Optional<DiscordMessage?> = Optional.Missing(),
val interaction: Optional<DiscordMessageInteraction> = Optional.Missing()
)

/**
Expand Down Expand Up @@ -789,3 +791,12 @@ data class AllowedMentions(
@SerialName("replied_user")
val repliedUser: OptionalBoolean = OptionalBoolean.Missing
)

@KordPreview
@Serializable
data class DiscordMessageInteraction(
val id: Snowflake,
val type: InteractionType,
val name: String,
val user: DiscordUser
)
6 changes: 5 additions & 1 deletion core/src/main/kotlin/cache/data/MessageData.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.kord.core.cache.data

import cache.data.MessageInteractionData
import dev.kord.cache.api.data.description
import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.*
import dev.kord.common.entity.optional.*
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -34,6 +36,7 @@ data class MessageData(
val flags: Optional<MessageFlags> = Optional.Missing(),
val stickers: Optional<List<MessageStickerData>> = Optional.Missing(),
val referencedMessage: Optional<MessageData?> = Optional.Missing(),
val messageInteraction: Optional<MessageInteractionData> = Optional.Missing()
) {

fun plus(selfId: Snowflake, reaction: MessageReactionAddData): MessageData {
Expand Down Expand Up @@ -127,7 +130,8 @@ data class MessageData(
messageReference.map { MessageReferenceData.from(it) },
flags,
stickers.mapList { MessageStickerData.from(it) },
referencedMessage.mapNotNull { from(it) }
referencedMessage.mapNotNull { from(it) },
interaction.map { MessageInteractionData.from(it) }
)
}
}
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/kotlin/cache/data/MessageInteractionData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cache.data;

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.DiscordMessageInteraction
import dev.kord.common.entity.InteractionType
import dev.kord.common.entity.Snowflake
import dev.kord.core.cache.data.UserData
import dev.kord.core.cache.data.toData
import kotlinx.serialization.Serializable

@KordPreview
@Serializable
data class MessageInteractionData(
val id:Snowflake,
val type:InteractionType,
val name:String,
val user: Snowflake
) {
companion object {
fun from(entity: DiscordMessageInteraction): MessageInteractionData = with(entity) {
MessageInteractionData(id, type, name, user.id)
}
}
}
10 changes: 10 additions & 0 deletions core/src/main/kotlin/entity/Message.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package dev.kord.core.entity

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.MessageType
import dev.kord.common.entity.Snowflake
import dev.kord.common.entity.optional.map
import dev.kord.common.entity.optional.mapNullable
import dev.kord.common.entity.optional.orEmpty
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
Expand All @@ -13,6 +16,7 @@ import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.GuildChannel
import dev.kord.core.entity.channel.GuildMessageChannel
import dev.kord.core.entity.channel.MessageChannel
import dev.kord.core.entity.interaction.MessageInteraction
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
Expand Down Expand Up @@ -174,6 +178,12 @@ class Message(
*/
val mentionedUserBehaviors: Set<UserBehavior> get() = data.mentions.map { UserBehavior(it, kord) }.toSet()

/**
* The [MessageInteraction] sent on this message object when it is a response to an [dev.kord.core.entity.interaction.Interaction].
*/
@KordPreview
val interaction: MessageInteraction? get() = data.messageInteraction.mapNullable { MessageInteraction(it, kord) }.value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider documenting this


/**
* The [users][User] mentioned in this message.
*
Expand Down
40 changes: 40 additions & 0 deletions core/src/main/kotlin/entity/interaction/MessageInteraction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.kord.core.entity.interaction

import cache.data.MessageInteractionData
import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.InteractionType
import dev.kord.common.entity.Snowflake
import dev.kord.core.Kord
import dev.kord.core.behavior.UserBehavior
import dev.kord.core.entity.KordEntity
import dev.kord.core.entity.Message

/**
* An instance of [MessageInteraction](https://discord.com/developers/docs/interactions/slash-commands#messageinteraction)
* This is sent on the [Message] object when the message is a response to an [Interaction].
*/
@KordPreview
class MessageInteraction(
val data: MessageInteractionData,
override val kord: Kord,
) : KordEntity {
/**
* [id][Interaction.id] of the [Interaction] this message is responding to.
*/
override val id: Snowflake get() = data.id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the interactions's ID, right? Is there any kind of behavior/entity we can get from this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there isn't but since it had an ID and requires Kord for the user object that it would be fine to consider it a KordEntity


/**
* the [name][ApplicationCommand.name] of the [ApplicationCommand] that triggered this message.
*/
val name: String get() = data.name

/**
* The [UserBehavior] of the [user][Interaction.user] who invoked the [Interaction]
*/
val user: UserBehavior get() = UserBehavior(data.id, kord)
Comment on lines +37 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs the usual getUser and getUserOrNull


/**
* the [InteractionType] of the interaction [MessageInteraction].
*/
val type: InteractionType get() = data.type
}