如何使bot只对同一条消息进行添加反应?

2024-10-02 16:23:15 发布

您现在位置:Python中文网/ 问答频道 /正文

def isAdmin(reaction, user):
    for i in admins:
        if user.id == i: return (str(reaction.emoji) == '👍' or str(reaction.emoji) == '👎')
try: reaction, user = await self.bot.wait_for('reaction_add', timeout=43200, check=isAdmin)
except asyncio.TimeoutError: await msg.edit(embed=embedEnd)
else:
    resign_channel = discord.utils.get(ctx.guild.channels, name="resignings") 
    if str(reaction.emoji) == '👍':
        print('{}: {} resigned {} {}.'.format(extCalls.timeNow('datetime'), ctx.channel.category.name, firstName, lastName))
        await msg.edit(embed=embedFin)
        await resign_channel.send(embed=embedConf)
    else: await msg.edit(embed=embedEnd)

这是我让机器人等待msg上的反应的代码。但是,出于某种原因,当我有两条msg(消息)同时等待反应时,当我对一条消息作出反应时,两条都会触发成功,即使我没有对第二条消息作出反应。这对于一个基于管理员的审批系统来说是一个很大的问题。。。有人知道为什么会发生这种情况或者如何解决吗


Tags: 消息forifchannelmsgembedawaitedit
1条回答
网友
1楼 · 发布于 2024-10-02 16:23:15

这段代码非常混乱,但如果您正在寻找一种只等待对一条消息的反应的方法,您应该在check命令中添加内容,例如:

@bot.command(name="some_command")
async def sc(ctx):
    msg = await ctx.send("Some interesting message!")
    def check(reaction, user):
        return reaction.message.id == msg.id
    try:
        reaction, user = await bot.wait_for("reaction_add", check=check,timeout=30.0)
    except asyncio.TimeoutError:
        await ctx.send("timeout")
        return
    await ctx.send(f"You reacted {str(reaction)}")

相关问题 更多 >