Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a clickable button under the message?

I'm trying to create a Discord bot using Python and discord.py, but currently I want to create a "clickable button":

example

This is an example of the Dank Memer bot with buttons. How do I achieve this and add buttons under my messages?

like image 908
Aaron Avatar asked Mar 24 '26 07:03

Aaron


1 Answers

First of all, these are part of the new "Message Components" in Discord. For discord.py, this is called a Button Component. This feature isn't available in the PyPi version of discord.py and is only available in the 2.0 rewrite on the master branch.

$ pip install git+https://github.com/Rapptz/discord.py.git@master

But since discord.py is no longer maintained, perhaps look into one of the maintained forks such as nextcord, disnake, etc.

To use create a button on the dpy 2.0:

view = discord.ui.View()
item = discord.ui.Button(style=discord.ButtonStyle.blurple, label="Click Me", url="https://google.com")
view.add_item(item=item)
await ctx.send("This message has a button!", view=view)

Or for more complicated features, you can subclass discord.ui.View instead:

class ViewWithButton(discord.ui.View):
    @discord.ui.button(style=discord.ButtonStyle.blurple, label='Click Me')
    async def click_me_button(self, button: discord.ui.Button, interaction: discord.Interaction):
        print("Button was clicked!")


await ctx.send("This message has a button!", view=ViewWithButton())

Button reference for discord.py can be found here: https://discordpy.readthedocs.io/en/master/api.html#button.

like image 131
Taku Avatar answered Mar 25 '26 21:03

Taku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!