From 4975ced38cbda41dcc948d4b1ebc4e0207718ab0 Mon Sep 17 00:00:00 2001 From: BartArys Date: Sat, 8 May 2021 19:39:27 +0200 Subject: [PATCH 01/22] Make slash command creation eager createGuildApplicationCommands and createGlobalApplicationCommands should eagerly create new commands to be more in line with the rest of the API --- core/src/main/kotlin/SlashCommands.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/main/kotlin/SlashCommands.kt b/core/src/main/kotlin/SlashCommands.kt index 142a6daf55d..c4e022fa155 100644 --- a/core/src/main/kotlin/SlashCommands.kt +++ b/core/src/main/kotlin/SlashCommands.kt @@ -49,10 +49,10 @@ class SlashCommands( contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } val request = ApplicationCommandsCreateBuilder().apply(builder).toRequest() - + val commands = service.createGlobalApplicationCommands(applicationId, request) return flow { - for (command in service.createGlobalApplicationCommands(applicationId, request)) { - val data = ApplicationCommandData.from(command) + commands.forEach { + val data = ApplicationCommandData.from(it) emit(GlobalApplicationCommand(data, service)) } } @@ -88,9 +88,10 @@ class SlashCommands( contract { callsInPlace(builder, InvocationKind.EXACTLY_ONCE) } val request = ApplicationCommandsCreateBuilder().apply(builder).toRequest() + val commands = service.createGuildApplicationCommands(applicationId, guildId, request) return flow { - for (command in service.createGuildApplicationCommands(applicationId, guildId, request)) { - val data = ApplicationCommandData.from(command) + commands.forEach { + val data = ApplicationCommandData.from(it) emit(GuildApplicationCommand(data, service, guildId)) } } From 07739072a6d1589ade57e637509fbedcad9ac4dd Mon Sep 17 00:00:00 2001 From: BartArys Date: Sat, 8 May 2021 19:41:29 +0200 Subject: [PATCH 02/22] Fix typo in InteractionBehavior ackowledgePublic -> acknowledgePublic --- .../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 d9d2c75b647..7ddd9f8dd89 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 90290f10d1ed9a1465b1ed24ec7ab37ac7137f1d Mon Sep 17 00:00:00 2001 From: BartArys Date: Sun, 9 May 2021 09:59:45 +0200 Subject: [PATCH 03/22] Don't compute supplier in InteractionBehavior It's a waste of resources, and might result in unexpected behavior for non-Kord suppliers. --- .../main/kotlin/behavior/interaction/InteractionBehavior.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt b/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt index 7ddd9f8dd89..98d808cd72d 100644 --- a/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt +++ b/core/src/main/kotlin/behavior/interaction/InteractionBehavior.kt @@ -93,8 +93,7 @@ fun InteractionBehavior( get() = channelId - override val supplier: EntitySupplier - get() = strategy.supply(kord) + override val supplier: EntitySupplier = strategy.supply(kord) } From f791645e56d7ed03e15c4da92d8a49fb3cb2a4d4 Mon Sep 17 00:00:00 2001 From: BartArys Date: Sun, 9 May 2021 10:07:02 +0200 Subject: [PATCH 04/22] Specify withStrategy for Interactions Return types are important! --- core/src/main/kotlin/entity/interaction/Interaction.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/src/main/kotlin/entity/interaction/Interaction.kt b/core/src/main/kotlin/entity/interaction/Interaction.kt index 714acf4dcc9..1bef7fffd6b 100644 --- a/core/src/main/kotlin/entity/interaction/Interaction.kt +++ b/core/src/main/kotlin/entity/interaction/Interaction.kt @@ -66,6 +66,8 @@ sealed class Interaction : InteractionBehavior { */ val version: Int get() = data.version + abstract override fun withStrategy(strategy: EntitySupplyStrategy<*>): Interaction + companion object { fun from( data: InteractionData, @@ -291,6 +293,9 @@ class DmInteraction( * The user who invoked the interaction. */ override val user get() = User(data.user.value!!, kord) + + override fun withStrategy(strategy: EntitySupplyStrategy<*>): DmInteraction = + DmInteraction(data, applicationId, kord, strategy.supply(kord)) } @KordPreview @@ -325,6 +330,8 @@ class GuildInteraction( override val user: UserBehavior get() = UserBehavior(member.id, kord) + override fun withStrategy(strategy: EntitySupplyStrategy<*>): GuildInteraction = + GuildInteraction(data, applicationId, kord, supplier) } From 05bad6a43dd7f0a758fb447e258c5fcd62f36908 Mon Sep 17 00:00:00 2001 From: BartArys Date: Mon, 10 May 2021 14:05:21 +0200 Subject: [PATCH 05/22] Introduce type to command options Also add Mentionable. Seems like Discord consistently tells us what kind of option an object is via the type field, this allows us to more clearly represent the json data in the lower level APIs. I ended up deleting DiscordOptionValue and replacing it with a more fleshed out CommandArgument. --- common/src/main/kotlin/entity/Interactions.kt | 320 ++++++++++++++---- .../src/test/kotlin/json/InteractionTest.kt | 16 +- .../json/interaction/groupsubcommand.json | 3 + .../json/interaction/rootcommand.json | 2 + .../json/interaction/subcommand.json | 2 + .../main/kotlin/cache/data/InteractionData.kt | 7 +- .../kotlin/entity/interaction/Interaction.kt | 108 +++--- .../kotlin/interaction/CommandTypesTest.kt | 5 + 8 files changed, 338 insertions(+), 125 deletions(-) diff --git a/common/src/main/kotlin/entity/Interactions.kt b/common/src/main/kotlin/entity/Interactions.kt index 80f4130d371..4cd693c2cc9 100644 --- a/common/src/main/kotlin/entity/Interactions.kt +++ b/common/src/main/kotlin/entity/Interactions.kt @@ -5,10 +5,8 @@ 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.OptionalSnowflake -import kotlinx.serialization.KSerializer -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable -import kotlinx.serialization.SerializationException +import kotlinx.serialization.* +import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.serializer import kotlinx.serialization.descriptors.* import kotlinx.serialization.encoding.* @@ -73,6 +71,7 @@ sealed class ApplicationCommandOptionType(val type: Int) { object User : ApplicationCommandOptionType(6) object Channel : ApplicationCommandOptionType(7) object Role : ApplicationCommandOptionType(8) + object Mentionable : ApplicationCommandOptionType(9) class Unknown(type: Int) : ApplicationCommandOptionType(type) companion object; @@ -220,6 +219,7 @@ data class DiscordApplicationCommandInteractionData( @KordPreview sealed class Option { abstract val name: String + abstract val type: ApplicationCommandOptionType internal object Serializer : KSerializer