【问题标题】:I want to make a multi-page help command using discord.py我想使用 discord.py 创建一个多页帮助命令
【发布时间】:2020-08-30 09:46:00
【问题描述】:

我正在使用 discord.py 来制作一个机器人,但是对于我的自定义帮助命令,有一页上无法容纳的命令更多。我希望机器人添加 2 个反应,前后,然后发送帮助消息的用户可以选择一个,并进入帮助命令的不同页面。我希望机器人能够编辑消息以显示第二页,如果他们返回,则编辑回原来的第一页。有人可以帮忙吗?这类似于 owobot 定义,您可以在定义之间来回滚动。

【问题讨论】:

标签: python discord.py


【解决方案1】:

此方法将使用Client.wait_For(),如果您有任何其他想法,可以轻松适应。

示例

@bot.command()
async def pages(ctx):
    contents = ["This is page 1!", "This is page 2!", "This is page 3!", "This is page 4!"]
    pages = 4
    cur_page = 1
    message = await ctx.send(f"Page {cur_page}/{pages}:\n{contents[cur_page-1]}")
    # getting the message object for editing and reacting

    await message.add_reaction("◀️")
    await message.add_reaction("▶️")

    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ["◀️", "▶️"]
        # This makes sure nobody except the command sender can interact with the "menu"

    while True:
        try:
            reaction, user = await bot.wait_for("reaction_add", timeout=60, check=check)
            # waiting for a reaction to be added - times out after x seconds, 60 in this
            # example

            if str(reaction.emoji) == "▶️" and cur_page != pages:
                cur_page += 1
                await message.edit(content=f"Page {cur_page}/{pages}:\n{contents[cur_page-1]}")
                await message.remove_reaction(reaction, user)

            elif str(reaction.emoji) == "◀️" and cur_page > 1:
                cur_page -= 1
                await message.edit(content=f"Page {cur_page}/{pages}:\n{contents[cur_page-1]}")
                await message.remove_reaction(reaction, user)

            else:
                await message.remove_reaction(reaction, user)
                # removes reactions if the user tries to go forward on the last page or
                # backwards on the first page
        except asyncio.TimeoutError:
            await message.delete()
            break
            # ending the loop if user doesn't react after x seconds

如果您的编辑器不支持直接粘贴表情符号,您可以使用this one 等网站来查找表情符号的 unicode。在这种情况下,向前箭头是\u25c0,向后箭头是\u25b6

除此之外,你应该很高兴!该消息将在该消息不活动 60 秒后自行删除(即没有人对箭头做出反应),但如果您希望在删除前有更长的时间,只需更改数字即可。

或者,您可以添加第三个表情符号,例如十字架,它可以按需删除消息。


参考资料:

【讨论】:

    【解决方案2】:

    如果您使用 client.command() 而不是 bot.command(),请将两个变量 bot 替换为 client

    【讨论】:

    • 这不是答案,它更适合作为评论。
    猜你喜欢
    • 2021-05-07
    • 1970-01-01
    • 2020-11-06
    • 2021-09-16
    • 2020-03-31
    • 2022-06-11
    • 2021-01-01
    • 2019-03-17
    • 2020-10-19
    相关资源
    最近更新 更多