python - 如何检查并等待消息下的 react ?

标签 python discord discord.py

我有一个事件,其中嵌入得到多个 react 。在这里,用户有 x 秒的时间来选择 react 。如果这是“错误的”,则应该吐出错误。如果 react 是我所定义的预期 react ,则应出现另一条消息。我有这个代码,但我不能再进一步了:

        await embedX.add_reaction("1️⃣")
        await embedX.add_reaction("2️⃣")
        await embedX.add_reaction("3️⃣")
        try:
            reaction = await self.bot.wait_for('reaction_add', timeout=10)
            if str(reaction.emoji) == "1️⃣":
                await ctx.send("Congrats, right answer.")
            else:
                await ctx.send("Incorrect reaction.")
        except asyncio.TimeoutError:
            await ctx.send("Took too long.")
错误:
AttributeError: 'tuple' object has no attribute 'emoji'.
我对 Python 还是很陌生,所以我希望能在这里找到帮助。
得到帮助后,我的代码现在如下所示:
        e = discord.Embed(color=discord.Color.gold())
        e.title = "Woah, look!"
        e.description = "**Does this work?**"
        e.add_field(name="1️⃣", value="Yes", inline=False)
        e.add_field(name="2️⃣", value="No", inline=False)
        e.add_field(name="3️⃣", value="Maybe", inline=False)
        e.set_footer(text="You have 10 seconds to answer the question", icon_url=self.bot.user.avatar_url)
        e.timestamp = datetime.datetime.utcnow()
        embedX = await ctx.send(embed=e)
        await embedX.add_reaction("1️⃣")
        await embedX.add_reaction("2️⃣")
        await embedX.add_reaction("3️⃣")
        try:
            reaction, user = await self.bot.wait_for('reaction_add', timeout=10)
            if user == self.bot.user:
                return
            if str(reaction.emoji) == "1️⃣":
                await embedX.edit(embed=er)
            else:
                await ctx.send(embed=ew)
        except asyncio.TimeoutError:
            await embedX.edit(embed=etl)
有时机器人会计算 react ,只是 1 - 对于其他人,他没有反应,但他没有做的是:如果 react 错误,则发布/编辑嵌入。
( erew 实际上是我之前定义的其他嵌入。)

最佳答案

您的错误消息应该给您一个提示:您收到了 tuple来自 self.bot.wait_for而不是 discord.Reaction .
使用时 Client.wait_for ,请记住它返回的内容(强调我的):

Returns no arguments, a single argument, or a tuple of multiple arguments that mirrors the parameters passed in the event reference.


看着 on_reaction_add event ,我们可以看到这个事件的payload是discord.Reaction和一个 discord.User目的。使用时wait_for(<event>) ,您将获得与扩展 on_<event> 相同的有效负载并以这种方式处理它。
所以把它们放在一起,这一行:
await self.bot.wait_for('reaction_add', timeout=10)
返回 tupleReaction和一个 User ,例如(<Reaction ...>, <User ...>) .所以你只需要稍微修改一下:
reaction, user = await self.bot.wait_for('reaction_add', timeout=10)
在这里,我们使用元组解包(或多重赋值,如果您愿意)从返回的元组中提取所需的值。
避免反馈循环
由于您将收到所有事件,包括由您的机器人引起的事件,因此您需要确保忽略由机器人引起的事件以避免反馈循环。您可以使用以下方法在事件处理程序中轻松防范这种情况:
# Assuming `self` is an instance of `discord.Client`
if user == self.user:  # `user` here comes from the event payload
    return
作为您的情况的具体示例:
reaction, user = await self.bot.wait_for('reaction_add', timeout=10)
if user == self.bot.user:
    return

关于python - 如何检查并等待消息下的 react ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65783267/

相关文章:

python - 如何使用 discord.py 中的 channel ID 设置 channel 对象?

python-3.x - ClientConnectorError : Cannot connect to host discordapp. com :443 ssl:default [Connect call failed ('162.159.134.233' , 44​​3)]

python - 未找到 Discord debian 模块 python3.6 和 python2.7 安装

python - 将列表与列表列表合并

python - Agg 和 Cairo 之间的 Matplotlib 后端差异

python - 如何更新覆盖率(包含在 pybuilder 中)以忽略 run.py 模块?

python - 如何用玩具名称重命名长 Pandas 数据框?

python - Discord Bot 到 DM 特定角色

python - Discord.py:wait_for ('reaction_add')未按预期工作

python - 如何制作一个在 Python 中提供角色的不和谐机器人?