What are interactions?¶
An Interaction is the message that your bot receives when a user initiates an application command or a message component.
Interactions and Bot Users¶
We’re all used to the way that Discord bots have worked for a long time. You make an application in Developer Portal, make a new bot user, and copy the token. Interactions bring something entirely new - the ability to interact with an application without needing a bot user in the guild. Responding to the interaction doesn’t require a bot token.
disnake
is primarily focused on using the gateway events, so you still need a bot user. Check out
hikari-py
with their REST-API part for this purpose.
Welcome to the new world.
Responding to interactions¶
You have only 3 seconds to respond to the interaction. If do not have time to do it, Discord will shown “This interaction failed” error.
In fact, there are 3 types of interactions:
disnake.ApplicationCommandInteraction
(for application commands)disnake.Interaction
(a base class, usually not used)
But responding is the same for both interactions types.
interaction.response
¶
disnake.Interaction.response
attribute returns a disnake.InteractionResponse
instance that has 4
useable methods. A response can only be done once. If you want to send secondary messages, consider using
disnake.Interaction.followup
webhook instead.
disnake.InteractionResponse.send_message()
- Sends response messagedisnake.InteractionResponse.edit_message()
- Edits original message, you’re unable to use this in application command because there are no message while you respondingdisnake.InteractionResponse.defer()
- Defers the interactiondisnake.InteractionResponse.is_done()
- Indicates whether an interaction response has been done before
Note
disnake.InteractionResponse.defer()
works differently depending on the type of interaction.
It creates “Bot is thinking…” message for application commands and
doesn’t throw “This interaction failed” if you’re not going to respond to message components.
Note
If you’re going to run long processes (more than 3 seconds) while responding, you must first defer the interaction.
Then when your response is ready you can edit the message using disnake.InteractionResponse.edit_message()
method
@bot.slash_command()
async def ping(inter: ApplicationCommandInteraction):
await inter.response.send_message("Pong!")
@bot.slash_command()
async def defer(inter: ApplicationCommandInteraction):
await inter.response.defer()
await asyncio.sleep(10)
await inter.edit_original_message("The wait is over, my comrades!")
interaction.followup
¶
Often used when you need to send secondary messages after responding.