From 96422d4b8d3a3c1f13725001caf8b09c6fda40ea Mon Sep 17 00:00:00 2001 From: Hope <34831095+HopeBaron@users.noreply.github.com> Date: Thu, 15 Apr 2021 13:17:12 +0300 Subject: [PATCH] Application command getter (#248) * implement get application by id endpoint * Add KordPreview annotations * rename Kord#getApplicationCommand to getGlobalApplicationCommand --- core/src/main/kotlin/Kord.kt | 6 +++++ core/src/main/kotlin/SlashCommands.kt | 12 +++++++++ .../src/main/kotlin/behavior/GuildBehavior.kt | 4 +++ rest/src/main/kotlin/route/Route.kt | 25 +++++++++++++++++++ .../main/kotlin/service/InteractionService.kt | 12 +++++++++ 5 files changed, 59 insertions(+) diff --git a/core/src/main/kotlin/Kord.kt b/core/src/main/kotlin/Kord.kt index 2bd5efbb2d0..083e06c4ef5 100644 --- a/core/src/main/kotlin/Kord.kt +++ b/core/src/main/kotlin/Kord.kt @@ -71,6 +71,12 @@ class Kord( val globalCommands: Flow get() = slashCommands.getGlobalApplicationCommands() + + @KordPreview + suspend fun getGlobalApplicationCommand(commandId: Snowflake) = + slashCommands.getGlobalApplicationCommand(commandId) + + /** * The default supplier, obtained through Kord's [resources] and configured through [KordBuilder.defaultStrategy]. * By default a strategy from [EntitySupplyStrategy.rest]. diff --git a/core/src/main/kotlin/SlashCommands.kt b/core/src/main/kotlin/SlashCommands.kt index 15c372a9c2f..d95aeaf2a48 100644 --- a/core/src/main/kotlin/SlashCommands.kt +++ b/core/src/main/kotlin/SlashCommands.kt @@ -70,6 +70,12 @@ class SlashCommands( } + suspend fun getGuildApplicationCommand(guildId: Snowflake, commandId: Snowflake): GuildApplicationCommand { + val response = service.getGuildCommand(applicationId, guildId, commandId) + val data = ApplicationCommandData.from(response) + return GuildApplicationCommand(data, service, guildId) + } + @OptIn(ExperimentalContracts::class) suspend inline fun createGuildApplicationCommands( guildId: Snowflake, @@ -94,6 +100,12 @@ class SlashCommands( } + suspend fun getGlobalApplicationCommand(commandId: Snowflake): GlobalApplicationCommand { + val response = service.getGlobalCommand(applicationId, commandId) + val data = ApplicationCommandData.from(response) + return GlobalApplicationCommand(data, service) + } + fun getGlobalApplicationCommands(): Flow = flow { for (command in service.getGlobalApplicationCommands(applicationId)) { val data = ApplicationCommandData.from(command) diff --git a/core/src/main/kotlin/behavior/GuildBehavior.kt b/core/src/main/kotlin/behavior/GuildBehavior.kt index 5d2569d9701..82d10c6dd4f 100644 --- a/core/src/main/kotlin/behavior/GuildBehavior.kt +++ b/core/src/main/kotlin/behavior/GuildBehavior.kt @@ -228,6 +228,10 @@ interface GuildBehavior : KordEntity, Strategizable { } } + @KordPreview + suspend fun getApplicationCommand(commandId: Snowflake) = + kord.slashCommands.getGuildApplicationCommand(id, commandId) + /** * Requests to get the this behavior as a [Guild]. * diff --git a/rest/src/main/kotlin/route/Route.kt b/rest/src/main/kotlin/route/Route.kt index 04ab7db7202..b54a1ed5f5b 100644 --- a/rest/src/main/kotlin/route/Route.kt +++ b/rest/src/main/kotlin/route/Route.kt @@ -451,11 +451,13 @@ sealed class Route( object TemplateDelete : Route(HttpMethod.Delete, "/guilds/${GuildId}/templates/${TemplateCode}", DiscordTemplate.serializer()) + @KordPreview object GlobalApplicationCommandsGet : Route>( HttpMethod.Get, "/applications/${ApplicationId}/commands", ListSerializer(DiscordApplicationCommand.serializer()) ) + @KordPreview object GlobalApplicationCommandCreate : Route( HttpMethod.Post, "/applications/${ApplicationId}/commands", @@ -463,23 +465,35 @@ sealed class Route( ) + @KordPreview object GlobalApplicationCommandsCreate : Route>( HttpMethod.Put, "/applications/${ApplicationId}/commands", ListSerializer(DiscordApplicationCommand.serializer()) ) + @KordPreview object GlobalApplicationCommandModify : Route( HttpMethod.Patch, "/applications/${ApplicationId}/commands/${CommandId}", DiscordApplicationCommand.serializer() ) + + @KordPreview + object GlobalApplicationCommandGet + : Route( + HttpMethod.Get, + "/applications/${ApplicationId}/commands/${CommandId}", + DiscordApplicationCommand.serializer() + ) + object GlobalApplicationCommandDelete : Route( HttpMethod.Delete, "/applications/${ApplicationId}/commands/${CommandId}", NoStrategy ) + @KordPreview object GuildApplicationCommandsGet : Route>( HttpMethod.Get, @@ -487,6 +501,7 @@ sealed class Route( ListSerializer(DiscordApplicationCommand.serializer()) ) + @KordPreview object GuildApplicationCommandCreate : Route( HttpMethod.Post, "/applications/${ApplicationId}/guilds/${GuildId}/commands", @@ -494,12 +509,14 @@ sealed class Route( ) + @KordPreview object GuildApplicationCommandsCreate : Route>( HttpMethod.Put, "/applications/${ApplicationId}/guilds/${GuildId}/commands", ListSerializer(DiscordApplicationCommand.serializer()) ) + @KordPreview object GuildApplicationCommandModify : Route( HttpMethod.Patch, @@ -507,6 +524,14 @@ sealed class Route( DiscordApplicationCommand.serializer() ) + @KordPreview + object GuildApplicationCommandGet + : Route( + HttpMethod.Get, + "/applications/${ApplicationId}/guilds/${GuildId}/commands/${CommandId}", + DiscordApplicationCommand.serializer() + ) + object GuildApplicationCommandDelete : Route( HttpMethod.Delete, diff --git a/rest/src/main/kotlin/service/InteractionService.kt b/rest/src/main/kotlin/service/InteractionService.kt index c34bcc4b580..2c6e094e322 100644 --- a/rest/src/main/kotlin/service/InteractionService.kt +++ b/rest/src/main/kotlin/service/InteractionService.kt @@ -175,4 +175,16 @@ class InteractionService(requestHandler: RequestHandler) : RestService(requestHa keys[Route.MessageId] = messageId body(FollowupMessageModifyRequest.serializer(), request) } + + suspend fun getGlobalCommand(applicationId: Snowflake, commandId: Snowflake) = call(Route.GlobalApplicationCommandGet){ + keys[Route.ApplicationId] = applicationId + keys[Route.CommandId] = commandId + } + + + suspend fun getGuildCommand(applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake) = call(Route.GuildApplicationCommandGet){ + keys[Route.ApplicationId] = applicationId + keys[Route.GuildId] = guildId + keys[Route.CommandId] = commandId + } } \ No newline at end of file