检查特定用户是否对discord.py做出了反应

时间:2020-06-01 09:37:50

标签: discord discord.py discord.py-rewrite

我正在寻找一个脚本来检查执行命令(ctx.message.author)的用户是否对带有复选标记的消息作出了反应,它应该看起来像这样:

@client.command()
async def test(ctx):
    message = await ctx.send("Test")
    for emoji in ('✅'):
        await message.add_reaction(emoji) 
        # Here it should detect if the ctx.message.author has reacted

1 个答案:

答案 0 :(得分:1)

您可以通过用户ID并使用wait_for进行检查:

import asyncio # for the exception

@client.command()
async def test(ctx):
    message = await ctx.send("Test")
    for emoji in ('✅'):
        await message.add_reaction(emoji) 

        try:

            # the wait_for will only register if the following conditions are met
            def check(rctn, user):
                return user.id == ctx.author.id and str(rctn) == '✅'

            # timeout kwarg is optional
            rctn, user = await client.wait_for("reaction_add", check=check, timeout=30)

            # then execute your code here if the author reacts, like so:
            await ctx.send("I detected the author reacting!")

        # throws this error if user doesn't react in time
        # you won't need this if you don't provide a timeout kwarg in wait_for()
        except asyncio.TimeoutError:
            await ctx.send("Sorry, you didn't react in time!")

参考: