Reaction Handling in Discord.py Rewrite Commands

前端 未结 1 1622
梦如初夏
梦如初夏 2020-12-22 06:06

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

相关标签:
1条回答
  • 2020-12-22 06:30

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

    from collections.abc import Sequence
    
    def make_sequence(seq):
        if seq is None:
            return ()
        if isinstance(seq, Sequence) and not isinstance(seq, str):
            return seq
        else:
            return (seq,)
    
    def reaction_check(message=None, emoji=None, author=None, ignore_bot=True):
        message = make_sequence(message)
        message = tuple(m.id for m in message)
        emoji = make_sequence(emoji)
        author = make_sequence(author)
        def check(reaction, user):
            if ignore_bot and user.bot:
                return False
            if message and reaction.message.id not in message:
                return False
            if emoji and reaction.emoji not in emoji:
                return False
            if author and user not in author:
                return False
            return True
        return 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 logic
        elif reaction.emoji == emoji2:
            # emoji2 logic
    except TimeoutError:
        # timeout logic
    
    0 讨论(0)
提交回复
热议问题