【问题标题】:Problem in embed-reaction system. Discord.py嵌入反应系统中的问题。不和谐.py
【发布时间】:2021-11-04 03:42:48
【问题描述】:

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

代码部分

    @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 目的/图像。
谢谢。

【问题讨论】:

  • 也许看看你的代码,问问自己为什么会这样。在您的while True-loop 中,您不断检查条件是否已给出。虽然这是真的,但如果可能,机器人将始终编辑现有的嵌入。
  • 你能告诉我应该怎么做吗?

标签: discord.py


【解决方案1】:

这可能是由于您的检查功能(第 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

【讨论】:

  • 现在,当我单击按钮(表情符号)时,没有任何反应,也没有收到任何错误消息。可能是我做错了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 2021-05-06
  • 2020-12-25
  • 2020-09-17
  • 1970-01-01
相关资源
最近更新 更多