【问题标题】:Pagination - Discord.py rewrite分页 - Discord.py 重写
【发布时间】:2020-07-07 16:48:24
【问题描述】:

我一直在尝试制作一个显示一些信息的命令,然后,当我对表情做出反应时,它应该显示另一组信息。

我尝试使用 this 的部分内容,特别是第 335 到 393 行中的部分 让它工作。但是,它什么也不做。甚至没有错误消息。

这是我现在使用的代码。

            def check_react(reaction, user):
            if reaction.message.id != msg.id:
                return False
            if user != ctx.message.author:
                return False
            return True
        res, user = await bot.wait_for('reaction_add', check=check_react, timeout=None,)
        if user != ctx.message.author:
            print('if user != ctx.message.author:')
        elif '⬅️' in str(res.emoji):
            page -=1
            print(page)
            embed = discord.Embed(title='generic title', description='generic description', color=0x52fffc)
            await msg.edit(embed=embed)
        elif '➡️' in str(res.emoji):
            page +=1
            print(page)
            embed = discord.Embed(title='generic title 2', description='generic description 2', color=0x52fffc)
            await msg.edit(embed=embed)

好像停在了

等待 bot.wait_for('reaction_add', ..)

这是为什么呢?我怎样才能使代码工作?顺便说一句,那是齿轮。如果需要,我很乐意提供更多代码。

【问题讨论】:

  • “它似乎停在:await bot.wait_for('reaction_add)'”。该函数等待一个reaction_add。或者换句话说,它停在那条线上。

标签: discord.py


【解决方案1】:

忘记了,我最终想通了。我是这样做的:

起初,我将页面的所有内容放在一个列表中。每个条目都是一个页面。

然后我定义一个函数作为检查。这会将反应侦听器限制为发送的实际嵌入,并防止命令调用者以外的任何人做出反应。很抱歉任何格式错误。

def check(reaction, user):
   return reaction.message.id == msg.id and user == ctx.author #msg.id is the id of the embed sent by the bot.

page = 0

while True: #can be changed to a variable to let it work a certain amount of times.
   try:
       reaction, _ = await bot.wait_for('reaction_add', timeout= 20.0, check=check)
       if reaction.emoji == 'emote of choice here' and page > 0:
           page -= 1
           embed = discord.Embed(title='Title Here', description=pages[page]
           await msg.edit(embed=embed)
       if reaction.emoji == 'emote of choice here' and page < len(pages) -1:
           page += 1
           embed = discord.Embed(title='Title Here', description= pages[page]
           await msg.edit(embed=embed)
   except asyncio.TimeoutError:
      #do stuff here, could be pass or a message saying the timeout has been reached or something similar.

【讨论】:

  • 由于某种原因,机器人在 while 循环处停止并且不继续执行命令
【解决方案2】:

更好更简单的方法是使用DiscordUtils

这是一个例子:

@commands.command()
async def paginate(self, ctx):
    embed1 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 1")
    embed2 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 2")
    embed3 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 3")
    paginator = DiscordUtils.Pagination.CustomEmbedPaginator(ctx, remove_reactions=True)
    paginator.add_reaction('⏮️', "first")
    paginator.add_reaction('⏪', "back")
    paginator.add_reaction('?', "lock")
    paginator.add_reaction('⏩', "next")
    paginator.add_reaction('⏭️', "last")
    embeds = [embed1, embed2, embed3]
    await paginator.run(embeds)

【讨论】:

    猜你喜欢
    • 2021-01-07
    • 2020-05-31
    • 2020-12-13
    • 2020-10-19
    • 2021-07-15
    • 1970-01-01
    • 2019-08-31
    • 2019-10-20
    • 2019-08-23
    相关资源
    最近更新 更多