Skip to content

Commit

Permalink
Application command getter (#248)
Browse files Browse the repository at this point in the history
* implement get application by id endpoint

* Add KordPreview annotations

* rename Kord#getApplicationCommand to getGlobalApplicationCommand
  • Loading branch information
HopeBaron authored Apr 15, 2021
1 parent 8ffbdfc commit 96422d4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/src/main/kotlin/Kord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class Kord(
val globalCommands: Flow<GlobalApplicationCommand>
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].
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/kotlin/SlashCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<GlobalApplicationCommand> = flow {
for (command in service.getGlobalApplicationCommands(applicationId)) {
val data = ApplicationCommandData.from(command)
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/kotlin/behavior/GuildBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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].
*
Expand Down
25 changes: 25 additions & 0 deletions rest/src/main/kotlin/route/Route.kt
Original file line number Diff line number Diff line change
Expand Up @@ -451,62 +451,87 @@ sealed class Route<T>(
object TemplateDelete
: Route<DiscordTemplate>(HttpMethod.Delete, "/guilds/${GuildId}/templates/${TemplateCode}", DiscordTemplate.serializer())

@KordPreview
object GlobalApplicationCommandsGet
: Route<List<DiscordApplicationCommand>>(
HttpMethod.Get, "/applications/${ApplicationId}/commands", ListSerializer(DiscordApplicationCommand.serializer())
)

@KordPreview
object GlobalApplicationCommandCreate : Route<DiscordApplicationCommand>(
HttpMethod.Post,
"/applications/${ApplicationId}/commands",
DiscordApplicationCommand.serializer()
)


@KordPreview
object GlobalApplicationCommandsCreate : Route<List<DiscordApplicationCommand>>(
HttpMethod.Put,
"/applications/${ApplicationId}/commands",
ListSerializer(DiscordApplicationCommand.serializer())
)

@KordPreview
object GlobalApplicationCommandModify : Route<DiscordApplicationCommand>(
HttpMethod.Patch,
"/applications/${ApplicationId}/commands/${CommandId}",
DiscordApplicationCommand.serializer()
)


@KordPreview
object GlobalApplicationCommandGet
: Route<DiscordApplicationCommand>(
HttpMethod.Get,
"/applications/${ApplicationId}/commands/${CommandId}",
DiscordApplicationCommand.serializer()
)

object GlobalApplicationCommandDelete
: Route<Unit>(
HttpMethod.Delete, "/applications/${ApplicationId}/commands/${CommandId}", NoStrategy
)

@KordPreview
object GuildApplicationCommandsGet
: Route<List<DiscordApplicationCommand>>(
HttpMethod.Get,
"/applications/${ApplicationId}/guilds/${GuildId}/commands",
ListSerializer(DiscordApplicationCommand.serializer())
)

@KordPreview
object GuildApplicationCommandCreate : Route<DiscordApplicationCommand>(
HttpMethod.Post,
"/applications/${ApplicationId}/guilds/${GuildId}/commands",
DiscordApplicationCommand.serializer()
)


@KordPreview
object GuildApplicationCommandsCreate : Route<List<DiscordApplicationCommand>>(
HttpMethod.Put,
"/applications/${ApplicationId}/guilds/${GuildId}/commands",
ListSerializer(DiscordApplicationCommand.serializer())
)

@KordPreview
object GuildApplicationCommandModify
: Route<DiscordApplicationCommand>(
HttpMethod.Patch,
"/applications/${ApplicationId}/guilds/${GuildId}/commands/${CommandId}",
DiscordApplicationCommand.serializer()
)

@KordPreview
object GuildApplicationCommandGet
: Route<DiscordApplicationCommand>(
HttpMethod.Get,
"/applications/${ApplicationId}/guilds/${GuildId}/commands/${CommandId}",
DiscordApplicationCommand.serializer()
)

object GuildApplicationCommandDelete
: Route<Unit>(
HttpMethod.Delete,
Expand Down
12 changes: 12 additions & 0 deletions rest/src/main/kotlin/service/InteractionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 96422d4

Please sign in to comment.