我正在尝试使用 on_raw_reaction_add 事件制作一个 discord.py 验证系统

时间:2021-02-01 14:13:48

标签: python discord.py

@client.command(name='verify')
            async def verify(ctx, channel: discord.TextChannel):
                msg = await channel.send('React to this message')
                await msg.add_reaction('?')
            
                @client.event
                async def on_raw_reaction_add(payload):
                    print('payload message.id ' + str(payload.message_id))
                    print('messages.id ' + str(msg.id)

谁能解释一下这将如何工作

2 个答案:

答案 0 :(得分:0)

您不要在命令中放置事件,您可以使用 client.wait_for

@client.command()
async def verify(ctx, channel: discord.TextChannel):
    msg = await channel.send("React to this message")
    await msg.add_reaction('?')

    def check(reaction, user):
        return user == ctx.author and reaction.message == msg
    # Waiting for the reaction
    reaction, user = await client.wait_for("reaction_add", check=check)
    print(f"{ctx.author} reacted")

参考:

答案 1 :(得分:-1)

有两种简单的方法可以做到这一点

第一种方式(如果您希望它在运行命令后永远工作,则不推荐。但如果您希望这样,则选择此方式)

@bot.command() # since you are using client, replace bot with client
async def verify(ctx): # I left out "channel" because I don't know what you want to do with it
    # Same thing as you did
    msg = await ctx.send("React to this message")
    await msg.add_reaction("?")

    # Waiting for someone to react
    def check(payload):
        return payload.message_id == msg.id # This will check if the message reacted on is the same as the bot's message, if you want it to be the user as well, replace this line with "return payload.message_id == msg.id and payload.user_id == ctx.author.id"
    payload = await bot.wait_for("raw_reaction_add", check=check) # You can add a timeout like this: "payload = await bot.wait_for("raw_reaction_add", check=check, timeout=int)"
    if str(payload.emoji) == "?":
        print(f"{payload.member} reacted")

第二种方式(如果您想运行一次命令,则更难但很有用,但如果您的机器人关闭,它将重置,因此请使用某种保存文件来保存 {{1} })

verify_msg

参考: