Reputation: 237
I'm trying to create a Discord bot using Python and discord.py, but currently I want to create a "clickable button":
This is an example of the Dank Memer bot with buttons. How do I achieve this and add buttons under my messages?
Upvotes: 3
Views: 6149
Reputation: 324
Install discord.py master branch by doing
pip install git+https://github.com/Rapptz/discord.py.git@master
For using ui/interactions refer Official docs.
You can find examples on https://github.com/Rapptz/discord.py/tree/master/examples/views
Upvotes: 0
Reputation: 33744
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.
Upvotes: 3