[Python] How do I make it so that a bot detects who reacted to a message with the correct...

Discussão em 'Python' iniciado por Stack, Maio 10, 2025.

  1. Stack

    Stack Membro Participativo

    I want to make it so that when a command is activated, a bot will send a message in a channel specifying a certain emoji. The message it sends will delete if, either 15 seconds have passed, or someone reacted with the correct emoji. The correct emoji is a custom emoji. The bot will also react to the message with the emoji as soon as it is sent so users can just click on the reaction.

    If nobody reacts in time, the message will just delete. But if somebody reacts, then the message will delete and the bot will send a new message in the same channel shouting out who reacted to the message first. What I need help with is how to make the bot detect who reacted to a message and how to make it react to its own message as soon as the message is sent.

    buttonmsg = await channel.send("**Button: :greenbutton:**", delete_after=float(15))
    emoji = get(message.server.emojis, name="custom_emoji")
    reaction = await client.wait_for_reaction(emoji, buttonmsg)
    redeemedby = #user that reacted with the correct emoji first
    await channel.send(f"Button redeemed by {redeemedby}")
    await adminchannel.send(f"**Button redeemed by {redeemedby}**")


    How would I do this?

    New code

    global buttonExpected
    global buttonUserId
    global buttonMsgId
    global buttonStage
    buttonExpected = "none"
    buttonUserId = 0
    buttonMsgId = 0
    buttonStage = 0

    @client.event
    async def on_raw_reaction_add(payload):
    global buttonExpected
    global buttonUserId
    global buttonMsgId
    global buttonStage
    print("Reaction detected")
    print(f"payload.user_id: {str(payload.user_id)}")
    print(f"payload.message_id: {str(payload.message_id)}")
    print(f"payload.emoji.name: {str(payload.emoji.name)}")
    print(f"payload.emoji.id: {str(payload.emoji.id)}")
    guild = discord.utils.find(lambda g: g.id == payload.guild_id, client.guilds)

    if payload.emoji.id == 1123299037236433027 and payload.message_id == buttonMsgId and buttonExpected == "green":
    print("Green Button Detected")
    buttonUserId = payload.user_id
    buttonStage = 2

    # In the on_message event:

    if floodfrightbuttons_button == "normal":
    global buttonStage
    global buttonUserId
    global buttonMsgId
    global buttonExpected
    buttonStage = 1
    buttonmsg = await floodfrightbuttons_channel.send("**Button:** <:greenbutton:1123299037236433027>")
    buttonExpected = "green"
    buttonMsgId = buttonmsg.id
    def reaction_ready():
    if buttonStage == 2:
    return True
    return False
    wait(lambda: reaction_ready(), timeout_seconds=floodfrightbuttons_deletetime, waiting_for="reaction")
    print("Reaction received")
    await floodfrightbuttons_channel.send(f"Button redeemed by <@{buttonUserId}>")
    await floodfrightbuttons_bothubchannel.send(f"Button redeemed by <@{buttonUserId}>")



    New Code Output:

    Traceback (most recent call last):
    File " /home/runner/Bot-Yozy/venv/lib/pytho n3.8/site-packages/discord/client.py", line 441, in run_event
    await coro( *args, **kwargs)
    File "main.py", , line 589, in on _message
    print( "Reaction received" )
    File "/home/runner/Bot-Yozy/venv/lib/pytho n3.8/site-packages/waiting/__ init__•py":
    •Tin
    e 18, in wait
    for × in iterwait(result=result, *args,
    **kwargs) :
    File "/home/runner/Bot-Yozy/venv/lib/pytho n3.8/site-packages/waiting/ init_ _•py", lin e 56, in iterwait
    raise TimeoutExpired(timeout_seconds, wa iting_for)
    waiting. exceptions.TimeoutExpired: Timeout o f 12 seconds expired waiting for reaction
    2023-06-29 14:44:20,192 - discord.client - I

    Continue reading...

Compartilhe esta Página