4

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 bottom of the menu):

enter image description here

I've tried contacting the developer and been searching the net but can't seem to find how they did it. Does anyone know of any resources on how to create them? Please provide links.

4
  • 2
    Those buttons bellow are discord reactions... You can check them out in the discord.py documentary
    – Sujit
    Mar 9, 2019 at 7:51
  • Can I add the reaction within the embed itself and not just beneath it like in the picture? I wish to use it like a button for my commands.
    – Legion
    Mar 9, 2019 at 10:08
  • Here's an example of a simple pagination system using discord reactions: stackoverflow.com/questions/51796005/… Mar 9, 2019 at 12:08
  • I don't think adding reactions inside an embed is possible...
    – Sujit
    Mar 9, 2019 at 14:32

3 Answers 3

10

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()
async def embedpages():
    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 = ''

    while True:
        if emoji == '⏮':
            i = 0
            await client.edit_message(message, embed = pages[i])
        elif emoji == '◀':
            if i > 0:
                i -= 1
                await client.edit_message(message, embed = pages[i])
        elif emoji == '▶':
            if i < 2:
                i += 1
                await client.edit_message(message, embed = pages[i])
        elif emoji == '⏭':
            i = 2
            await client.edit_message(message, embed=pages[i])
        
        res = await client.wait_for_reaction(message = message, timeout = 30.0)
        if res == None:
            break
        if str(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.

2
  • Great answer @Sujit, what's the best way to not get the asyncio.TimeoutError though?
    – echan00
    Dec 16, 2020 at 7:50
  • @echan00 I have written an answer for the latest version of discord.py here: stackoverflow.com/a/65336328/11146632
    – Sujit
    Dec 17, 2020 at 7:34
5

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()
async def embedpages(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('⏭')

    def check(reaction, user):
        return user == ctx.author

    i = 0
    reaction = None

    while True:
        if str(reaction) == '⏮':
            i = 0
            await message.edit(embed = pages[i])
        elif str(reaction) == '◀':
            if i > 0:
                i -= 1
                await message.edit(embed = pages[i])
        elif str(reaction) == '▶':
            if i < 2:
                i += 1
                await message.edit(embed = pages[i])
        elif str(reaction) == '⏭':
            i = 2
            await 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:
            break

    await 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.

1
  • awesome, will try this out instead
    – echan00
    Dec 17, 2020 at 17:47
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.