Slash command commons¶
Here's a few common details and tips about slash commands.
Using predefined choices¶
If your choices stay the same for every command, you can improve re-usability and avoid extra code by using choices on the resolver's level, that is, the resolver will return the choices used for every option of their type.
All you now need to do is enable usePredefinedChoices on your option.
Example
Here, the resolver for TimeUnit is already defined and will be explained in Adding option resolvers.
=== ":material-language-kotlin: Kotlin"
```kotlin
@Command
class SlashConvertSimplified {
@JDASlashCommand(name = "convert", description = "Convert time to another unit")
suspend fun onSlashConvertSimplified(
event: GuildSlashEvent,
@SlashOption(description = "The time to convert") time: Long,
@SlashOption(description = "The unit to convert from", usePredefinedChoices = true) from: TimeUnit,
@SlashOption(description = "The unit to convert to", usePredefinedChoices = true) to: TimeUnit
) {
event.reply("${to.convert(time, from)} ${to.name.lowercase()}").await()
}
}
```
=== "Kotlin (DSL)"
```kotlin
@Command
class SlashConvertSimplified : GlobalApplicationCommandProvider {
suspend fun onSlashConvertSimplified(event: GuildSlashEvent, time: Long, from: TimeUnit, to: TimeUnit) {
event.reply("${to.convert(time, from)} ${to.name.lowercase()}").await()
}
override fun declareGlobalApplicationCommands(manager: GlobalApplicationCommandManager) {
manager.slashCommand("convert", function = ::onSlashConvertSimplified) {
description = "Convert time to another unit"
option("time") {
description = "The time to convert"
}
option("from") {
description = "The unit to convert from"
usePredefinedChoices = true
}
option("to") {
description = "The unit to convert to"
usePredefinedChoices = true
}
}
}
}
```
=== ":material-language-java: Java"
```java
@Command
public class SlashConvertSimplified {
@JDASlashCommand(name = "convert", description = "Convert time to another unit")
public void onSlashTimeInSimplified(
GuildSlashEvent event,
@SlashOption(description = "The time to convert") long time,
@SlashOption(description = "The unit to convert from", usePredefinedChoices = true) TimeUnit from,
@SlashOption(description = "The unit to convert to", usePredefinedChoices = true) TimeUnit to
) {
event.reply(to.convert(time, from) + " " + to.toString().toLowerCase()).queue();
}
}
```