Search code examples
pythondiscorddiscord.pydiscord-buttons

How to add buttons to an embed in discord.py


So me and my friend have an embed,

@client.event
async def on_message(message):
    if message.content.lower().startswith("!help"):
        HelpEmbed = discord.Embed(
            title="Help screen",
            description=
            "Here is you can find instructions of how to use the bot!",
            color=discord.Colour.blue())
        HelpEmbed.add_field(
            name="Game Commands",
            value=
            "These are commands to do stuff in the game, use !GameCMDS to see all commands relate to the game",
            inline=False)
        HelpEmbed.add_field(
            name="Server commands",
            value=
            "These are commands to do stuff with the server. Use !ServerCMDS to see all commands related to the server",
            inline=False)
        HelpEmbed.set_thumbnail(
            url=
            "https://images.unsplash.com/photo-1477959858617-67f85cf4f1df?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1244&q=80"
        )
        await message.channel.send(embed=HelpEmbed) 

We want to add buttons to it, however all tutorials do not work. My friend also could not figure out how, so if you know please tell me, also if you could tell me how to add a footer because HelpEmbed.set_footer does not work. Thanks!


Solution

  • Discord.py v2:

    Since discord.py 2.0 release, interactions are now availible https://discordpy.readthedocs.io/en/v2.3.2/api.html#discord.Message.components

    Old answer:

    Take a look at discord-ineraction, a python library for discord components (buttons, selects, slash commands, ...) GitHub, Docs

    Firts, install the library

    $ pip install -U discord-py-interactions
    

    According to the docs, you can use create_button:

    from discord_slash.utils.manage_components import create_button, create_actionrow
    from discord_slash.model import ButtonStyle
    
    # ...
    
    buttons = [
                create_button(
                    style=ButtonStyle.green,
                    label="A Green Button"
                ),
              ]
    
    action_row = create_actionrow(*buttons)
    
    await message.channel.send(embed=HelpEmbed, components=[action_row])
    

    As for the footer, you can set embed.set_footer():

    embed.set_footer(text="My Footer")