反应处理不和.py重写命令

2024-05-17 09:54:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

到目前为止,我发现的只是使用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)

Tags: name命令yousendadd消息messagechannel
1条回答
网友
1楼 · 发布于 2024-05-17 09:54:36

下面是我用来为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

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

^{pr2}$

相关问题 更多 >