首页 > 解决方案 > 嵌入反应系统中的问题。不和谐.py

问题描述

因此,我编写了这段代码,一旦用户对某些表情符号做出反应,它就会发送嵌入并更改其值/项目。它适用于单个嵌入,但是当用户要求像您在图像中看到的相同的多个嵌入时,对嵌入的反应也会改变其他类似嵌入的值。

代码部分

    @client.command()
    async def embed(ctx):
    current = 1
    embed = discord.Embed(title = f'{current}')
    buttons = [ u"\u25C0", u"\u25FC" , u"\u25B6" ,  u"\U0001F5D1"]
    msg = await ctx.send(embed = embed)
    
    for button in buttons:
        await msg.add_reaction(button)
    while True:
        
        try:
            reaction , user = await client.wait_for("reaction_add", check = lambda reaction,user: user == ctx.author and reaction.emoji in buttons, timeout = 180.0)
        except asyncio.TimeoutError:
            embed.set_footer(text= "Timeout.")
            await msg.clear_reactions()
        
        else:
            previous_page = current
            if reaction.emoji == u"\u25C0":
                current -= 1
                embed.add_field(name = None, value = f'{current}')
            
            elif reaction.emoji == u"\u25FC":
                if current > 0:
                    current = 0
                    embed.add_field(name = None, value = f'{current}')
                    
            elif reaction.emoji == u"\u25B6":
               
                current += 1
                embed.add_field(name = None, value = f'{current}')
            
            elif reaction.emoji ==  u"\U0001F5D1":
                
                await msg.edit(embed = embed)
                await msg.clear_reactions()

                
            for button in buttons:
                await msg.remove_reaction(button, ctx.author)
            

            if current != previous_page:
                embed.add_field(name = None, value = f'{current}')
                await msg.edit(embed = embed)

图片:https ://imgur.com/a/fEpz9jD

注意:我在我的机器人中使用的代码与这个完全相同。我没有包括那些嵌入的截图和截图,因为它被用于 NSFW 目的/图像。
谢谢。

标签: discord.py

解决方案


这可能是由于您的检查功能而发生的(第 13 行)

check = lambda reaction,user: user == ctx.author and reaction.emoji in buttons

在这里,您正在检查表情符号是否匹配,并且做出反应的用户是执行命令的同一用户。但是您不会检查将反应添加到哪条消息。要解决此问题,您可以通过检查reaction.message.id == ctx.message.id结果是否为来检查消息 ID:

check = lambda reaction,user: user == ctx.author and reaction.emoji in buttons and reaction.message.id == msg.id

推荐阅读