Share your repls and programming experiences

← Back to all posts
Discord Buttons - A library for creating reaction buttons easily in discord.py.
h
xfinnbar (82)

DiscordButtons

DiscordButtons is an extension to the discordpy framework that allows you to create cool reaction buttons more easily.

Before click:

After click:

Normally, writing this would be annoying. In the example below, I use DBtns to make the process a lot simpler.

from discordbuttons import DBtns

@client.command()
async def hello(ctx):
	msg = await ctx.send("Hello") # Save the message that we want the button to appear on in a 		variable.
	btns = DBtns(client, msg, ctx.message.author) # Create DBtns instance and provide a client object, message and author.
	client.add_cog(btns) # Add it as a cog (for permissions).
	await btns.add_button("✔", send_hi, { "name": name }) # Add the button with the check mark emoji, passing data to the callback.
	
async def send_hi(message, user, allowed_user, data):
	if allowed_user.id == user.id: # check if the right user clicked the button
		await message.channel.send("Hi, " + data["name"] + "!")

Creating a new bot with DiscordButtons

Fork the template at the bottom of this page to get started. It's easy!

Adding DiscordButtons to an existing discordpy bot

To add discordbuttons to an existing bot, go to the template at the bottom of the page. Click on "files" and select discordbuttons.py and copy all the code inside it. Then, go to your repl or a local bot and create a file named discordbuttons.py, then paste all the code you copied earlier in there. Finally, add from discordbuttons import DBtns. You now have access to all the features of DiscordButtons.

Note: I plan to upload this package to PyPi soon, but I will not do it yet.

Documentation

class DBtns (extends commands.Cog)

You are meant to use this class as a cog.

  • __init__(bot, message, user = None, clear_buttons = True)

    • bot: Your commands.Bot or discord.Client instance. Usually client or bot.
    • user: The person that sent the command or triggered the buttons to appear. This user will be passed into events.
    • clear_buttons: Whether or not to remove all the reactions after the button has been clicked.
  • async add_button(emoji = '✅', on_click = print_hello_world, data = {}):

    • emoji: The emoji to add as a reaction.
    • on_click: Function to be called when the button is clicked. Arguments are passed:
      • message: Message that the button was created on.
      • user: User who clicked the button.
      • allowed_user: User who created the button (passed to __init__).
      • data: Data passed to add_button.
Comments
hotnewtop
cuber1515 (56)

Cool! I bookmarked this for whenever I start back on my discord bot.