forked from kordlib/kord
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Expose the creation of application commands behavior * Add interaction message * Apply suggestions * reference the MessageInteraction in docs * Implement Strategizable for MessageInteraction * cache user from interaction message * Fix compilation errors * Fix withStrategy return type Co-authored-by: Bart Arys <[email protected]> Co-authored-by: Bart Arys <[email protected]>
- Loading branch information
1 parent
e300c5b
commit 42c8fb5
Showing
6 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
core/src/main/kotlin/entity/interaction/MessageInteraction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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.common.exception.RequestException | ||
import dev.kord.core.Kord | ||
import dev.kord.core.behavior.UserBehavior | ||
import dev.kord.core.entity.KordEntity | ||
import dev.kord.core.entity.Message | ||
import dev.kord.core.entity.Strategizable | ||
import dev.kord.core.entity.User | ||
import dev.kord.core.exception.EntityNotFoundException | ||
import dev.kord.core.supplier.EntitySupplier | ||
import dev.kord.core.supplier.EntitySupplyStrategy | ||
|
||
/** | ||
* 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, | ||
override val supplier: EntitySupplier = kord.defaultSupplier | ||
) : KordEntity, Strategizable { | ||
/** | ||
* [id][Interaction.id] of the [Interaction] this message is responding to. | ||
*/ | ||
override val id: Snowflake get() = data.id | ||
|
||
/** | ||
* 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) | ||
|
||
/** | ||
* the [InteractionType] of the interaction [MessageInteraction]. | ||
*/ | ||
val type: InteractionType get() = data.type | ||
|
||
/** | ||
* Requests the [User] of this interaction message. | ||
* | ||
* @throws RequestException if something went wrong while retrieving the user. | ||
* @throws EntityNotFoundException if the user was null. | ||
*/ | ||
suspend fun getUser(): User = supplier.getUser(user.id) | ||
|
||
/** | ||
* Requests to get the user of this interaction message, | ||
* returns null if the [User] isn't present. | ||
* | ||
* @throws [RequestException] if anything went wrong during the request. | ||
*/ | ||
suspend fun getUserOrNull(): User? = supplier.getUserOrNull(user.id) | ||
|
||
override fun withStrategy(strategy: EntitySupplyStrategy<*>): MessageInteraction { | ||
return MessageInteraction(data, kord, strategy.supply(kord)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters