discord.py - 嵌入反应系统中的问题。不和谐.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 目的/图像。
谢谢。
解决方案
这可能是由于您的检查功能而发生的(第 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
推荐阅读
- html - 无法让此颜色框留在其他框内并向右浮动
- python - (Discord.py) 如何检查机器人是否已经在语音通道中播放音频?
- python - 如何在 Python 中使用 tesseract 获得图像中最大的文本?
- javascript - 即使选中了一个,我也会收到单选按钮的输出值
- python - PMTA:带有 Gmail API 的 SMTP 中继服务器
- javascript - 为什么表单没有在 Ajax 调用上提交
- c++ - 在原地销毁和重新创建对象后,对对象的引用是否仍然有效?
- javascript - 如何在高图中给出时间而不是角度?
- javascript - 我在哪里将 mozimageSmoothingEnabled 替换为 imageSmoothingEnabled
- python - 如何获得多处理结果?