py重写命令中的反应处理

py重写命令中的反应处理

本文介绍了Discord.py重写命令中的反应处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

有没有一种方法可以捕获命令中的反应。我已经发出了删除通道的命令,但是我想问用户是否确定使用响应。我想防止其他人对此消息作出反应(只有上下文作者才应作出反应)。

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 prevent others from reacting to this message (Only the Context Author should React).

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

So far what I have found is just to use the on_reaction_add() but this can not detect the user who sent the command. I would like to only update the command if the message author is the one who reacted to the message, anyone else, ignore it.

更新 :我发现 wait_for()确实可以满足我的要求,但是现在的问题是如何检查是否设置了错误的响应? (即,如果我按第二个反应,请删除该消息)

Update: I found that wait_for() does exactly what I want but the issue now is how do I check for if the wrong reaction is set? (i.e if I press the second reaction, delete the message)

    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)


推荐答案

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

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

这篇关于Discord.py重写命令中的反应处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:14