【问题标题】:Discord rich embed buttonsDiscord 丰富的嵌入按钮
【发布时间】:2019-07-31 04:35:09
【问题描述】:

我制作了一些 discord.py 机器人,但我遇到了一个令人惊讶的机器人。它被称为 IdleRPG,并使用带有按钮的丰富嵌入消息。这是一张图片(注意菜单底部的按钮):

我尝试联系开发人员并在网上搜索,但似乎无法找到他们是如何做到的。有谁知道如何创建它们的任何资源?请提供链接。

【问题讨论】:

  • 下面的那些按钮是不和谐的反应......你可以在 discord.py 纪录片中查看它们
  • 我可以在嵌入本身中添加反应,而不是像图片中那样在其下方添加反应吗?我希望将它用作我的命令的按钮。
  • 这是一个使用不和谐反应的简单分页系统示例:stackoverflow.com/questions/51796005/…
  • 我认为在嵌入中添加反应是不可能的......

标签: python discord.py


【解决方案1】:

更新:请查看this 答案以获取最新版本的discord.py


给你...我能够创建一个命令来编辑反应点击的嵌入:

计划:

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

截图:

要接受页码,您必须为该表情符号创建一个 if 语句并使用 wait_for_message() 函数。然后你必须检查页码是否有效,并相应地更改i的值。

希望你能明白。

【讨论】:

  • 很好的答案@Sujit,虽然没有得到 asyncio.TimeoutError 的最佳方法是什么?
  • @echan00 我已经为discord.py 的最新版本写了一个答案:stackoverflow.com/a/65336328/11146632
【解决方案2】:

我已经为这个问题写了一个答案,但是这个答案是针对旧版本的discord.py。看到这个问题浏览量很大,决定写一个discord.py最新版的答案。

计划:

@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()

截图:

要接受页码,您必须为该表情符号创建一个 if 语句并使用 wait_for() 函数。然后你必须检查页码是否有效,并相应地更改i 的值。

希望你能明白。

【讨论】:

  • 太棒了,试试这个
【解决方案3】:

不要将反应用作按钮。

必须一次添加一个反应,这可能非常缓慢。 Discord 目前正在实施实际按钮,您可能已经在 Dank Memer 和 Dyno 等机器人中看到了这些按钮。由于我们目前正在等待 discord.py 2.0 发布以支持按钮,因此我们已经可以使用名为 discord_components 的 Python 模块实现按钮。

您可以在我的另一个答案中获得帮助:Add button components to a message (discord.py)

这是一个建议,你不需要这样做,但它比使用反应更好。

【讨论】:

    猜你喜欢
    • 2021-08-16
    • 2011-03-10
    • 2020-08-12
    • 2011-11-29
    • 2011-07-15
    • 2021-11-26
    • 2012-07-30
    • 1970-01-01
    • 2021-04-25
    相关资源
    最近更新 更多