Skip to content Skip to sidebar Skip to footer

Discord Rich Embed Buttons

I made a few discord.py bots, but I came across one which was surprising. It's called IdleRPG and uses rich embed messages with buttons. Here's a pic (note the buttons at the botto

Solution 1:

Update: Please check this answer for the latest version of discord.py.


Here you go... I was able to create a command that edits the embed on reaction clicks:

Program:

@client.command()asyncdefembedpages():
    page1 = discord.Embed (
        title = 'Page 1/3',
        description = 'Description',
        colour = discord.Colour.orange()
    )
    page2 = discord.Embed (
        title = 'Page 2/3',
        description = 'Description',
        colour = discord.Colour.orange()
    )
    page3 = discord.Embed (
        title = 'Page 3/3',
        description = 'Description',
        colour = discord.Colour.orange()
    )

    pages = [page1, page2, page3]

    message = await client.say(embed = page1)

    await client.add_reaction(message, '⏮')
    await client.add_reaction(message, '◀')
    await client.add_reaction(message, '▶')
    await client.add_reaction(message, '⏭')

    i = 0
    emoji = ''whileTrue:
        if emoji == '⏮':
            i = 0await client.edit_message(message, embed = pages[i])
        elif emoji == '◀':
            if i > 0:
                i -= 1await client.edit_message(message, embed = pages[i])
        elif emoji == '▶':
            if i < 2:
                i += 1await client.edit_message(message, embed = pages[i])
        elif emoji == '⏭':
            i = 2await client.edit_message(message, embed=pages[i])
        
        res = await client.wait_for_reaction(message = message, timeout = 30.0)
        if res == None:
            breakifstr(res[1]) != '<Bots name goes here>':  #Example: 'MyBot#1111'
            emoji = str(res[0].emoji)
            await client.remove_reaction(message, res[0].emoji, res[1])

    await client.clear_reactions(message)

Screenshot:

enter image description here

For accepting a page number you'll have to create an if statement for that emoji and use the wait_for_message() function. Then you'll have to check whether the page number is valid and change the value of i accordingly.

I hope you get the idea.

Solution 2:

I've already written an answer for this question, however that answer is for an older version of discord.py. Seeing that this question gets a large number of views, I've decided to write an answer for the latest version of discord.py.

Program:

@client.command()asyncdefembedpages(ctx):
    page1 = discord.Embed (
        title = 'Page 1/3',
        description = 'Description',
        colour = discord.Colour.orange()
    )
    page2 = discord.Embed (
        title = 'Page 2/3',
        description = 'Description',
        colour = discord.Colour.orange()
    )
    page3 = discord.Embed (
        title = 'Page 3/3',
        description = 'Description',
        colour = discord.Colour.orange()
    )
    
    pages = [page1, page2, page3]

    message = await ctx.send(embed = page1)
    await message.add_reaction('⏮')
    await message.add_reaction('◀')
    await message.add_reaction('▶')
    await message.add_reaction('⏭')

    defcheck(reaction, user):
        return user == ctx.author

    i = 0
    reaction = NonewhileTrue:
        ifstr(reaction) == '⏮':
            i = 0await message.edit(embed = pages[i])
        elifstr(reaction) == '◀':
            if i > 0:
                i -= 1await message.edit(embed = pages[i])
        elifstr(reaction) == '▶':
            if i < 2:
                i += 1await message.edit(embed = pages[i])
        elifstr(reaction) == '⏭':
            i = 2await message.edit(embed = pages[i])
        
        try:
            reaction, user = await client.wait_for('reaction_add', timeout = 30.0, check = check)
            await message.remove_reaction(reaction, user)
        except:
            breakawait message.clear_reactions()

Screenshot:

enter image description here

For accepting a page number you'll have to create an if statement for that emoji and use the wait_for() function. Then you'll have to check whether the page number is valid and change the value of i accordingly.

I hope you get the idea.

Solution 3:

Don't use reactions as buttons.

Reactions have to be added one at a time, which can be excruciatingly slow. Discord is currently implementing actual buttons, which you may already see in bots such as Dank Memer and Dyno. As we currently wait for discord.py 2.0 to be released for button support, we can already implement buttons with a python module called discord_components.

You can get help with that here in my other answer: Add button components to a message (discord.py)

This is a recommendation, you don't need to do this but it's better than using reactions.

Post a Comment for "Discord Rich Embed Buttons"