Discord py 检测反应

Dan*_*sas 2 python bots discord discord.py

我只是想学习如何“阅读”同一机器人发送的消息的反应。我已经被困了好几天了 我查了一下,但我找到的唯一教程是针对角色的特定消息。我不关心这个,我不会在整个服务器上使用一条消息,所以我无法获取消息的 ID。我只希望机器人发送一条消息,然后用户做出反应,机器人会写“您对 [表情符号] 做出反应”。

我在这个网站上发现了一些问题,但它们只会让我更加困惑。尽管如此,这还是我勉强做到的。

@bot.command()
async def react(ctx):
    await ctx.send("React to me!")

@bot.event
async def on_reaction_add(reaction, user):
            await channel.send("{}, you responded with {}".format(user, reaction))
Run Code Online (Sandbox Code Playgroud)

Dom*_*nik 5

实际上文档中已经有一个很好的示例,您可以在这里找到它:

但为了稍微简化整个事情,这里有一个示例代码

@bot.command()
async def react(ctx):
    def check(reaction, user):  # Our check for the reaction
        return user == ctx.message.author  # We check that only the authors reaction counts

    await ctx.send("Please react to the message!")  # Message to react to

    reaction = await bot.wait_for("reaction_add", check=check)  # Wait for a reaction
    await ctx.send(f"You reacted with: {reaction[0]}")  # With [0] we only display the emoji
Run Code Online (Sandbox Code Playgroud)

它是这样的:

反应输出

没有的话reaction[0]你只会得到不必要的信息。[0]仅显示第一个数字,在本例中为Reaction emoji