Close Discord message on button click

Question:

I have a select menu with a submit button. When submit is clicked I want to close the current message, submit the data and then open a new select menu but I am having trouble figuring out how to have the message close.

How can I have the current menu and button close when the submit button is clicked?

Thanks in advance for your help.

Asked By: Ryan Thomas

||

Answers:

To turn my comment into an answer; you can just delete the message on button press. For example, my Cancel buttons look a little like this:

@discord.ui.button(label="Cancel", style=discord.ButtonStyle.red, row=3, disabled=False, emoji="✖️")
async def cancel_callback(self, button: discord.ui.Button, interaction: discord.Interaction):
    await interaction.message.delete()

This is within a custom View class so might have to be adapted for your needs – but using the interaction.message attribute to delete the message is the way to go.

Answered By: ESloman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.