Disnake Bot Adding buttons to a message

201 Views Asked by At

Please tell me how to add buttons to a message that will be sent to another channel.

Example: a user writes a command !nick [name] to change the name. After that, a message with buttons is sent to the administration channel with a request to change the user's nickname. The administrator clicks on the "Accept" or "Reject" button and thereby changes the user's nickname.

I know how to send a message to another channel, but I don't know how to add buttons to this message

1

There are 1 best solutions below

0
MacItaly On

EDIT: I prefer the disnake.py package over discord.py because discord.py hadn't been fully upgraded when Discord rolled out Application Interactions. (For those that may know more, Danny had left discord.py and wasn't going to be upgrading it, so I learned disnake.py instead. Danny has since come back and done a tremendous job with discord.py, but I never switched back. They're very similar with only minor nuances. An understanding of discord.py should be proficient to understand the disnake.py coding)

This would be the last line, where the bot sends the buttons for the user to interact with. You'll see components, you can also have content for words to be displayed, embed if you want to attach an embed to send, or even embeds if you have a list of embeds you want to send (up to 10). Here I have just components because I want you to see just how to add the buttons.

Each button has the minimum arguments to define the button. label, style, and custom_id.

    await inter.response.send_message(components=[
        disnake.ui.Button(label="ACCEPT", style=disnake.ButtonStyle.green, custom_id="accept"),
        disnake.ui.Button(label="REJECT", style=disnake.ButtonStyle.red, custom_id="reject")])

In addition to this code that displays the buttons, you will also need something that listens for the button clicks.

@yourbot.listen()
async def on_button_click(inter: disnake.MessageInteraction):
    if inter.component.custom_id not in ["accept", "reject"]:
        return
    else:
        if inter.component.custom_id == 'accept':
            *Here is all your fancy code to do cool stuff
        elif inter.component.custom_id == 'reject':
            *Do coding stuff