[Python] How to make a confirmation prompt with buttons with a discord bot in discord.py?

Discussão em 'Python' iniciado por Stack, Junho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I have a few commands in my discord bot that I'd like to ask for confirmation before running. Something like this example:

    @tree.command(name='test')
    async def testCommand(interaction: discord.Interaction):
    if await confirmationPrompt(interaction, 'run this test command'):
    # Do stuff


    But for that, i need confirmationPrompt() to return either True or False.

    I'd like to make the options to this confirmation prompt with buttons, so i wrote this:

    async def confirmationPrompt(interaction: discord.Interaction, warning: str):

    class ConfirmationButtons(discord.ui.View):

    @discord.ui.button(label = '✅', style = discord.ButtonStyle.blurple)
    async def returnTrue(self, interaction: discord.Interaction, button: discord.ui.Button):
    return()

    @discord.ui.button(label = '❌', style = discord.ButtonStyle.blurple)
    async def returnFalse(self, interaction: discord.Interaction, button: discord.ui.Button):
    return()


    await interaction.response.send_message(
    embed = discord.Embed(title = 'Are you sure you want to ' + warning),
    view = ConfirmationButtons()
    )


    The issue is: I can't figure out a way to make confirmationPrompt() return something from within returnTrue() or returnFalse(). What should I write to do that? Or if there's a better way to achieve the same result with a different method i would also like to know.

    Continue reading...

Compartilhe esta Página