Skip to content Skip to sidebar Skip to footer

Reaction Handling In Discord.py Rewrite Commands

Is there a way to capture a reaction from a command. I have made a command that deletes a channel but I would like to ask the user if they are sure using reactions. I would like to

Solution 1:

Here's a function I use for generating check functions for wait_for:

from collections.abc importSequencedefmake_sequence(seq):
    if seq isNone:
        return ()
    ifisinstance(seq, Sequence) andnotisinstance(seq, str):
        return seq
    else:
        return (seq,)

defreaction_check(message=None, emoji=None, author=None, ignore_bot=True):
    message = make_sequence(message)
    message = tuple(m.idfor m in message)
    emoji = make_sequence(emoji)
    author = make_sequence(author)
    defcheck(reaction, user):
        if ignore_bot and user.bot:
            returnFalseif message and reaction.message.idnotin message:
            returnFalseif emoji and reaction.emoji notin emoji:
            returnFalseif author and user notin author:
            returnFalsereturnTruereturn check

We can pass the message(s), user(s), and emoji(s) that we want to wait for, and it will automatically ignore everything else.

check = reaction_check(message=msg, author=ctx.author, emoji=(emoji1, emoji2))
try: 
    reaction, user = await self.client.wait_for('reaction_add', timeout=10.0, check=check)
    if reaction.emoji == emoji1:
        # emoji1 logicelif reaction.emoji == emoji2:
        # emoji2 logicexcept TimeoutError:
    # timeout logic

Post a Comment for "Reaction Handling In Discord.py Rewrite Commands"