Discord rich embed buttons

喜夏-厌秋 提交于 2021-02-07 09:45:36

问题


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):

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.


回答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()
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:

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:


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:

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.



来源:https://stackoverflow.com/questions/55075157/discord-rich-embed-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!