N
N
Nolrox2022-02-07 13:47:24
Python
Nolrox, 2022-02-07 13:47:24

How to make react react in discord.py?

Is it possible to make the bot react to the reaction of a certain person? For example, if the bot sends a message "hello" with the reaction ✅ on a command mentioning "+hi @test", and if this reaction is clicked on by this particular @test, then it sends the message "hello @test". And if he does not click on the reaction for 30 seconds, then the message is deleted. If possible, how?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-02-07
@Vindicar

In the command handler:
1. Make a storage, a dict dictionary with a type key (message id, user id) and a value in the form of asyncio.Event objects .
2. When you send a message for the reaction, you put a pair (id of the message, id of the addressee) in the storage, and set the value to a new asyncio.Event ().
3. Use asyncio.wait_for() and Event.wait() to wait for this Event object to fire or the desired timeout.
4. If the object worked and the wait_for() function returned a value, then there was a reaction. You act accordingly.
5. If wait_for() threw an asyncio.TimeoutError, then a timeout has occurred. You act accordingly.
In both cases, you delete the key from the storage (message id, recipient id)
As for the reaction, you need the on_raw_reaction_add() event and an object of the RawReactionActionEvent class associated with it . From there you can get information about the user, message and emoji. You look for a pair (message id, user id) in the repository, if you find it, then this is one of our special messages. In the handler of this event, you select the asyncio.Event object corresponding to this pair and pull its set () method to signal the above algorithm (it will then go along branch 4). Otherwise, you do nothing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question