不和谐机器人(python)“wait_for”“reaction_add”复制嵌入

时间:2021-05-10 23:51:42

标签: python discord bots

我在尝试制作一个可以显示剧集拥有的角色列表的嵌入时遇到了问题。我基本上想要 :left_arrow: 和 :right_arrow: 表情符号作为在用户拥有的长字符数据库之间左右平移的反应。

我确实设法做到了,但是我注意到如果我使用该命令在一个超时之前两次显示所有字符的嵌入,然后我单击向左箭头以查看下一页两个嵌入的下一页,而不仅仅是我做出反应的那个。

这是我的源代码。

@commands.command(aliases=["collection", "c"])
@commands.cooldown(1, 5, commands.BucketType.user)
async def characters(self, ctx):
    if not await bot.check_channel_permissions(ctx.author, ctx.channel): # Checks if the bot can send messages in the channel.
        return

    playerData = await bot.get_player_data(ctx.author, ctx.channel) # Returns the user's data.

    if playerData:

        if len(playerData["Characters"]) > 0: # Checks amount of characters the user owns.

            if len(playerData["Characters"]) > 20: # Player owns 20 so it will create an embed with reactions.

                global pageNumber
                global maxPages
                pageNumber = 1
                maxPages = math.ceil(len(playerData["Characters"]) / 20)

                embed = await characters.setupEmbed(self, playerData, ctx.author, pageNumber)

                currentEmbed = await ctx.channel.send(ctx.author.mention, embed=embed)

                await currentEmbed.add_reaction("⬅")
                await currentEmbed.add_reaction("➡")

                async def listenForRection(self, mainUser): # Waits until the user reacts with the left or right arrow in order to edit the current embed.

                    global pageNumber # the current page (20 characters are shown a page)
                    global maxPages # the max amount of pages the user can pan between.

                    def check(reaction, reactionuser):
                        if reactionuser != mainUser:
                            return False
                        elif str(reaction.emoji) == '⬅' and pageNumber > 1:
                            return True
                        elif str(reaction.emoji) == '➡' and pageNumber < maxPages:
                            return True
                        else:
                            return False

                    try:
                        reaction, reactionuser = await self.client.wait_for(
                            "reaction_add",
                            timeout=60,
                            check=check
                        )
                    except:
                        asyncio.TimeoutError
                        await currentEmbed.edit(content="Timed out.")
                    else:
                        if str(reaction.emoji) == '⬅':
                            pageNumber += -1
                        elif str(reaction.emoji) == "➡":
                            pageNumber += 1
                        newEmbed = await characters.setupEmbed(self, playerData, mainUser, pageNumber) # Returns an embeded list given the page number and info on the user.
                        await currentEmbed.edit(embed=newEmbed) # re-edits the current embed to show the next or previous page.
                        await listenForRection(self, mainUser) # re-runs the function again to listen for the next reaction.

                await listenForRection(self, ctx.author) # runs the listener once so that it can begin detecting and re-running on its own.


            else: # player will get an embeded list with no reactions since they only have 1 page of characters.
                embed = await characters.setupEmbed(self, playerData, ctx.author, 1)
                await ctx.channel.send(ctx.author.mention, embed=embed)
        else:
            await ctx.reply("You don't have any characters.")

会发生什么是我运行命令一次,它运行得非常好。然后,当我在超时之前再次运行它并单击一个反应时,它要么不起作用,要么在新嵌入和旧嵌入上都运行。我花了大量时间尝试多种方法并进行搜索,但除了在修复当前错误后立即创建新错误之外,我没有做太多事情。

1 个答案:

答案 0 :(得分:0)

实际上有一个名为 discord-ext-menus 的用于制作分页菜单的 ext 库。它会为您完成大部分工作,因此您无需自行创建。