-
Notifications
You must be signed in to change notification settings - Fork 82
Interactions
In discord's words: Application commands are commands that an application can register to Discord. They provide users a first-class way of interacting directly with your application that feels deeply integrated into Discord.
Setting up an application command is of two stages registering the command and listening to it's events
are a replacement for the text-based commands we used in bots pre-V8; However, the Chat Inputs have a reach interface and interaction system that makes it easier for the user to search for and use the registered commands.
To register a commmand, you may use createGuildChatInputCommand
which registers a command only on a given guild (good for premium features)
or createGlobalChatInputCommand
to register a command on all guilds the bot have the application.commands
scope on.
Code
val kord = Kord(System.getenv("TOKEN"))
kord.createGuildChatInputCommand(
Snowflake(556525343595298817),
"sum",
"A slash command that sums two numbers"
) {
int("first_number", "The first operand") {
required = true
}
int("second_number", "The second operand") {
required = true
}
}
}
You may also give choices for a given command using the choice
function in the DSL Builder which is a set of white listed values for a given arguement.
Code
val kord = Kord(System.getenv("TOKEN"))
kord.createGuildChatInputCommand(
Snowflake(556525343595298817),
"sum",
"A slash command that sums two numbers"
) {
int("first_number", "The first operand") {
required = true
for(i in 0L..9L) {
choice("$i", i)
}
}
int("second_number", "The second operand") {
required = true
for(i in 0L..9L) {
choice("$i", i)
}
}
}
Now that we have our command setup; we should listen to it to give it some functionality.
using the on
function in Kord, you may listen to various events that give you different contexts:
-
ChatInputInteractionCreateEvent
- occurred in either a DM or a Guild -
GlobalChatInputInteractionCreateEvent
- occurred in Guilds -
DMChatInputInteractionCreateEvent
- occurred in DMs (Direct Message)
since our command is registered only on our guild, it's better to listen to the Guild variant to have as much context as possible.
kord.on<GuildChatInputCommandInteractionCreateEvent> {
val response = interaction.deferPublicMessage()
val command = interaction.command
val first = command.integers["first_number"]!! // it's required so it's never null
val second = command.integers["second_number"]!!
response.followUp { content = "$first + $second = ${first + second}" }
}
please check Acknowledging An Interaction
-
the
followup
function is used to send a message that follows-up on the deferred interaction. -
Kord provides type-safe Maps that utilize the id as shown above in the example.
kord.on<InteractionCreateEvent> {
val response = interaction.deferPublicMessage()
//...
}
before we do anything with our code, we must defer the interaction to tell Discord we are going to respond to this interaciton, else the interaction would fail (we have a window of 3 seconds at the time of writing these docs ).
there two types of defers:
- Public: Visible to everyone
- Ephemeral: Visible only to the command user.