【问题标题】:Reaction Handling in Discord.py Rewrite CommandsDiscord.py 重写命令中的反应处理
【发布时间】:2019-11-03 19:51:27
【问题描述】:

有没有办法从命令中捕捉反应。我做了一个删除频道的命令,但我想问用户他们是否确定使用反应。我想阻止其他人对此消息做出反应(只有上下文作者应该做出反应)。

到目前为止,我发现只是使用on_reaction_add() 但这无法检测到发送命令的用户。如果消息作者是对消息做出反应的人,我只想更新命令,其他任何人,忽略它。

更新:我发现wait_for() 完全符合我的要求,但现在的问题是如何检查是否设置了错误的反应? (即如果我按下第二个反应,删除消息)

    if is_admin:
        msg = await ctx.send('Clear: Are you sure you would like to purge this entire channel?')
        emoji1 = u"\u2705"
        emoji2 = u"\u274E"
        await msg.add_reaction(emoji=emoji1)
        await msg.add_reaction(emoji=emoji2)

        def check(reaction, user):
            return user == ctx.message.author and reaction.emoji == u"\u2705"

        try:
            reaction, user = await self.client.wait_for('reaction_add', timeout=10.0, check=check)
        except asyncio.TimeoutError:
            return await msg.delete()
        else:
            channel = ctx.message.channel
            new_channel = await channel.clone(name=channel.name, reason=f'Clear Channel ran by {ctx.message.author.name}')
            await new_channel.edit(position=channel.position)
            await channel.delete(reason=f'Clear Channel ran by {ctx.message.author.name}')
            await new_channel.send('Clear: Channel has now been cleared.', delete_after=7)
    else:
        await ctx.send(f"Sorry, you do not have access to this command.", delete_after=5)

【问题讨论】:

    标签: python-3.x discord.py-rewrite


    【解决方案1】:

    这是我用来为wait_for 生成check 函数的函数:

    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
    

    我们可以传递我们想要等待的消息、用户和表情符号,它会自动忽略其他所有内容。

    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
    

    【讨论】:

    • 很好的答案,非常感谢。不过有一个问题,我应该为make_sequence() 包括什么,因为它在我的机器人中似乎是“未定义的”哈哈。
    • 那是我的错,我在复制main函数的时候没有复制helper函数。
    猜你喜欢
    • 2020-10-19
    • 2021-07-15
    • 2020-05-03
    • 2020-11-06
    • 2021-03-30
    • 2020-09-11
    • 2020-08-25
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多