From 46ce2220e4b9cbc8d13e8065dccfad988b95b824 Mon Sep 17 00:00:00 2001 From: HopeBaron Date: Tue, 11 May 2021 16:11:58 +0300 Subject: [PATCH 1/7] Expose the creation of application commands behavior --- core/src/main/kotlin/Unsafe.kt | 17 ++++++++++ .../GlobalApplicationCommandBehavior.kt | 33 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/core/src/main/kotlin/Unsafe.kt b/core/src/main/kotlin/Unsafe.kt index dc3edb0017e0..b09c1e206ba3 100644 --- a/core/src/main/kotlin/Unsafe.kt +++ b/core/src/main/kotlin/Unsafe.kt @@ -6,6 +6,7 @@ import dev.kord.common.annotation.KordUnsafe import dev.kord.common.entity.Snowflake import dev.kord.core.behavior.* import dev.kord.core.behavior.channel.* +import dev.kord.rest.service.InteractionService /** * A class that exposes the creation of `{Entity}Behavior` classes. @@ -71,4 +72,20 @@ class Unsafe(private val kord: Kord) { return "Unsafe" } + fun guildApplicationCommand( + guildId: Snowflake, + applicationId: Snowflake, + commandId: Snowflake, + service: InteractionService = kord.rest.interaction + ): GuildApplicationCommandBehavior = + GuildApplicationCommandBehavior(guildId, applicationId, commandId, service) + + fun globalApplicationCommand( + applicationId: Snowflake, + commandId: Snowflake, + service: InteractionService = kord.rest.interaction + ): GlobalApplicationCommandBehavior = + GlobalApplicationCommandBehavior(applicationId, commandId, service) + + } \ No newline at end of file diff --git a/core/src/main/kotlin/behavior/GlobalApplicationCommandBehavior.kt b/core/src/main/kotlin/behavior/GlobalApplicationCommandBehavior.kt index 9c3168ed4a0d..72a0d953b6e5 100644 --- a/core/src/main/kotlin/behavior/GlobalApplicationCommandBehavior.kt +++ b/core/src/main/kotlin/behavior/GlobalApplicationCommandBehavior.kt @@ -79,4 +79,37 @@ interface GuildApplicationCommandBehavior : ApplicationCommandBehavior { override suspend fun delete() { service.deleteGuildApplicationCommand(applicationId, guildId, id) } + +} + +@KordPreview +fun GuildApplicationCommandBehavior( + guildId: Snowflake, + applicationId: Snowflake, + id: Snowflake, + service: InteractionService +): GuildApplicationCommandBehavior = object : GuildApplicationCommandBehavior { + override val guildId: Snowflake + get() = guildId + override val applicationId: Snowflake + get() = applicationId + override val service: InteractionService + get() = service + override val id: Snowflake + get() = id } + + +@KordPreview +fun GlobalApplicationCommandBehavior( + applicationId: Snowflake, + id: Snowflake, + service: InteractionService +): GlobalApplicationCommandBehavior = object : GlobalApplicationCommandBehavior { + override val applicationId: Snowflake + get() = applicationId + override val service: InteractionService + get() = service + override val id: Snowflake + get() = id +} \ No newline at end of file From 9be4925f9f9001315bf02ff81ee6855e55b88f77 Mon Sep 17 00:00:00 2001 From: HopeBaron Date: Wed, 12 May 2021 22:41:23 +0300 Subject: [PATCH 2/7] make message type sealed --- .../src/main/kotlin/entity/DiscordMessage.kt | 68 +++++++++++++------ 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/common/src/main/kotlin/entity/DiscordMessage.kt b/common/src/main/kotlin/entity/DiscordMessage.kt index a802889e7c87..36e0640e7d0a 100644 --- a/common/src/main/kotlin/entity/DiscordMessage.kt +++ b/common/src/main/kotlin/entity/DiscordMessage.kt @@ -719,42 +719,70 @@ data class AllRemovedMessageReactions( ) @Serializable(with = MessageType.MessageTypeSerializer::class) -enum class MessageType(val code: Int) { +sealed class MessageType(val code: Int) { /** The default code for unknown values. */ - Unknown(Int.MIN_VALUE), - Default(0), - RecipientAdd(1), - RecipientRemove(2), - Call(3), - ChannelNameChange(4), - ChannelIconChange(5), - ChannelPinnedMessage(6), - GuildMemberJoin(7), - UserPremiumGuildSubscription(8), - UserPremiumGuildSubscriptionTierOne(9), - UserPremiumGuildSubscriptionTwo(10), - UserPremiumGuildSubscriptionThree(11), - ChannelFollowAdd(12), - GuildDiscoveryDisqualified(14), + class Unknown(code: Int) : MessageType(code) + object Default : MessageType(0) + object RecipientAdd : MessageType(1) + object RecipientRemove : MessageType(2) + object Call : MessageType(3) + object ChannelNameChange : MessageType(4) + object ChannelIconChange : MessageType(5) + object ChannelPinnedMessage : MessageType(6) + object GuildMemberJoin : MessageType(7) + object UserPremiumGuildSubscription : MessageType(8) + object UserPremiumGuildSubscriptionTierOne : MessageType(9) + object UserPremiumGuildSubscriptionTwo : MessageType(10) + object UserPremiumGuildSubscriptionThree : MessageType(11) + object ChannelFollowAdd : MessageType(12) + object GuildDiscoveryDisqualified : MessageType(14) @Suppress("SpellCheckingInspection") - GuildDiscoveryRequalified(15), - Reply(19); + object GuildDiscoveryRequalified : MessageType(15) + object GuildDiscoveryGracePeriodInitialWarning : MessageType(16) + object GuildDiscoveryGracePeriodFinalWarning : MessageType(17) + object ThreadCreated : MessageType(18) + object Reply : MessageType(19) + object ApplicationCommand : MessageType(20) + object ThreadStarterMessage : MessageType(21) + object GuildInviteReminder : MessageType(22) - companion object MessageTypeSerializer : KSerializer { + object MessageTypeSerializer : KSerializer { override val descriptor: SerialDescriptor get() = PrimitiveSerialDescriptor("type", PrimitiveKind.INT) override fun deserialize(decoder: Decoder): MessageType { val code = decoder.decodeInt() - return values().firstOrNull { it.code == code } ?: Unknown + return values.firstOrNull { it.code == code } ?: Unknown(code) } override fun serialize(encoder: Encoder, value: MessageType) { encoder.encodeInt(value.code) } } + + companion object { + val values: Set + get() = setOf( + Default, + RecipientAdd, + RecipientRemove, + Call, + ChannelNameChange, + ChannelIconChange, + ChannelPinnedMessage, + GuildMemberJoin, + UserPremiumGuildSubscription, + UserPremiumGuildSubscriptionTierOne, + UserPremiumGuildSubscriptionTwo, + UserPremiumGuildSubscriptionThree, + ChannelFollowAdd, + GuildDiscoveryDisqualified, + GuildDiscoveryRequalified, + Reply, + ) + } } @Serializable(with = AllowedMentionType.Serializer::class) From 0103eaa9c7bb3cfbf3bbc98fae457693446cd3b1 Mon Sep 17 00:00:00 2001 From: HopeBaron Date: Sat, 15 May 2021 13:23:27 +0300 Subject: [PATCH 3/7] Fix typo --- .../src/main/kotlin/behavior/interaction/InteractionBehavior.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt b/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt index d9d2c75b6472..7ddd9f8dd89d 100644 --- a/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt +++ b/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt @@ -50,7 +50,7 @@ interface InteractionBehavior : KordEntity, Strategizable { * * @return [PublicInteractionResponseBehavior] public acknowledgement of an interaction. */ - suspend fun ackowledgePublic(): PublicInteractionResponseBehavior { + suspend fun acknowledgePublic(): PublicInteractionResponseBehavior { val request = PublicInteractionResponseCreateBuilder().toRequest() kord.rest.interaction.createInteractionResponse(id, token, request) return PublicInteractionResponseBehavior(applicationId, token, kord) From 232fcc3335ba81275081e9a29c0352297da69538 Mon Sep 17 00:00:00 2001 From: HopeBaron Date: Sat, 15 May 2021 13:23:42 +0300 Subject: [PATCH 4/7] document interaction create event --- .../event/interaction/InteractionCreate.kt | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/core/src/main/kotlin/event/interaction/InteractionCreate.kt b/core/src/main/kotlin/event/interaction/InteractionCreate.kt index 7848a6ff4b50..d860cd202636 100644 --- a/core/src/main/kotlin/event/interaction/InteractionCreate.kt +++ b/core/src/main/kotlin/event/interaction/InteractionCreate.kt @@ -2,9 +2,28 @@ package dev.kord.core.event.interaction import dev.kord.common.annotation.KordPreview import dev.kord.core.Kord -import dev.kord.core.event.Event +import dev.kord.core.behavior.interaction.* import dev.kord.core.entity.interaction.Interaction +import dev.kord.core.event.Event +/** + * This event fires when an interaction is created. + * + * + * Discord currently have one type of interactions, + * [Slash Commands][dev.kord.core.entity.interaction.ApplicationCommand]. + * + * The event should be acknowledged using one of the following methods: + * * [acknowledgeEphemeral][Interaction.acknowledgeEphemeral] - acknowledges an interaction ephemerally. + * * [acknowledgePublic][Interaction.acknowledgePublic] - acknowledges an interaction in public. + * * [respondPublic][Interaction.respondPublic] - same as public acknowledgement, but an immediate result (message) can be supplied. + * * [respondEphemeral][Interaction.respondEphemeral] - same as ephemeral acknowledgement, but an immediate result (message) can be supplied. + * + * Once an interaction has been acknowledged, + * you can use the [PublicInteractionResponseBehavior.followUp] or [EphemeralInteractionResponseBehavior.followUp]. + * + * The resulting followup message and its methods may defer based on which method is used. + */ @KordPreview class InteractionCreateEvent( val interaction: Interaction, From 8841b2a3763669b2ad80120ac43aeb95c4ff0e83 Mon Sep 17 00:00:00 2001 From: HopeBaron Date: Sat, 15 May 2021 22:13:03 +0300 Subject: [PATCH 5/7] apply suggestions and add more details --- .../kotlin/event/interaction/InteractionCreate.kt | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/core/src/main/kotlin/event/interaction/InteractionCreate.kt b/core/src/main/kotlin/event/interaction/InteractionCreate.kt index d860cd202636..9e2165e2d03f 100644 --- a/core/src/main/kotlin/event/interaction/InteractionCreate.kt +++ b/core/src/main/kotlin/event/interaction/InteractionCreate.kt @@ -10,19 +10,26 @@ import dev.kord.core.event.Event * This event fires when an interaction is created. * * - * Discord currently have one type of interactions, + * Discord currently has one type of interaction, * [Slash Commands][dev.kord.core.entity.interaction.ApplicationCommand]. * - * The event should be acknowledged using one of the following methods: + * The event should be acknowledged withing 3 seconds of reception using one of the following methods: * * [acknowledgeEphemeral][Interaction.acknowledgeEphemeral] - acknowledges an interaction ephemerally. * * [acknowledgePublic][Interaction.acknowledgePublic] - acknowledges an interaction in public. * * [respondPublic][Interaction.respondPublic] - same as public acknowledgement, but an immediate result (message) can be supplied. * * [respondEphemeral][Interaction.respondEphemeral] - same as ephemeral acknowledgement, but an immediate result (message) can be supplied. * * Once an interaction has been acknowledged, - * you can use the [PublicInteractionResponseBehavior.followUp] or [EphemeralInteractionResponseBehavior.followUp]. + * you can use [PublicInteractionResponseBehavior.followUp] or [EphemeralInteractionResponseBehavior.followUp] to display additional messages. * - * The resulting followup message and its methods may defer based on which method is used. + * The resulting follow-up message and its methods may differ based on which method is used. + * * Following up an acknowledgement results in replacing "The bot is thinking prompt" with the follow-up content. + * * Following up a respond results in a completely new message instance. + * + * As such, due to how Discord handles ephemeral acknowledgements, + * a follow-up on a ephemeral acknowledgement will result in an ephemeral message. + * + * In the current iteration, ephemeral messages (regardless of the type) doesn't support files and/or embeds. */ @KordPreview class InteractionCreateEvent( From 0bfa21d7ee40bb8e21c88b3f1e49474fc58c61c3 Mon Sep 17 00:00:00 2001 From: HopeBaron Date: Sat, 15 May 2021 22:14:38 +0300 Subject: [PATCH 6/7] replace the quotes --- core/src/main/kotlin/event/interaction/InteractionCreate.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/kotlin/event/interaction/InteractionCreate.kt b/core/src/main/kotlin/event/interaction/InteractionCreate.kt index 9e2165e2d03f..3abfb02258f8 100644 --- a/core/src/main/kotlin/event/interaction/InteractionCreate.kt +++ b/core/src/main/kotlin/event/interaction/InteractionCreate.kt @@ -23,7 +23,7 @@ import dev.kord.core.event.Event * you can use [PublicInteractionResponseBehavior.followUp] or [EphemeralInteractionResponseBehavior.followUp] to display additional messages. * * The resulting follow-up message and its methods may differ based on which method is used. - * * Following up an acknowledgement results in replacing "The bot is thinking prompt" with the follow-up content. + * * Following up an acknowledgement results in replacing "The bot is thinking" prompt with the follow-up content. * * Following up a respond results in a completely new message instance. * * As such, due to how Discord handles ephemeral acknowledgements, From c3e90febb8c2516711193d4a5291a24cc752bc84 Mon Sep 17 00:00:00 2001 From: HopeBaron Date: Sat, 15 May 2021 22:15:49 +0300 Subject: [PATCH 7/7] proper grammar, Hope --- core/src/main/kotlin/event/interaction/InteractionCreate.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/kotlin/event/interaction/InteractionCreate.kt b/core/src/main/kotlin/event/interaction/InteractionCreate.kt index 3abfb02258f8..893a90c74338 100644 --- a/core/src/main/kotlin/event/interaction/InteractionCreate.kt +++ b/core/src/main/kotlin/event/interaction/InteractionCreate.kt @@ -29,7 +29,7 @@ import dev.kord.core.event.Event * As such, due to how Discord handles ephemeral acknowledgements, * a follow-up on a ephemeral acknowledgement will result in an ephemeral message. * - * In the current iteration, ephemeral messages (regardless of the type) doesn't support files and/or embeds. + * In the current iteration, ephemeral messages (regardless of the type) don't support files and/or embeds. */ @KordPreview class InteractionCreateEvent(