【问题标题】:Make global check function with discord.py-rewrite使用 discord.py-rewrite 进行全局检查功能
【发布时间】:2020-02-03 04:19:12
【问题描述】:

所以,使用discord.py-rewrite,我知道我可以使用:

def check(message):
    """Checks the message author and channel."""
    return message.author == ctx.author and message.channel == ctx.channel
await bot.wait_for("message", timeout=180, check=check)

检查消息输入(必须来自上下文作者和上下文通道)。但是因为我需要这个检查几个命令,我不能只做吗:

def check_msg(context, message):
    return context.author == message.author and context.channel == message.channel

然后使用:

await bot.wait_for("message", timeout=180, check=check_msg)

【问题讨论】:

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


    【解决方案1】:

    discord.py 的 rewrite 分支不再存在。现在只是 v1。

    Bot.wait_forcheck 谓词检查要等待的内容,只会传递正在等待的事件的参数。这意味着,由于您正在等待消息事件,它只会被传递单个消息参数。

    实现您想要的一种方法是使用处理context 参数并返回检查谓词的包装器方法,例如:

    def wrapper(context):
        def check_msg(message):
            return context.author == message.author and context.channel == message.channel
        return check_msg
    
    await bot.wait_for("message", timeout=180, check=wrapper(ctx))
    

    【讨论】:

    • 好的,我明白了,那时我将无法使用@ 语法。但它有效,谢谢!
    • 提及不应该与它有任何关系,如果这就是你在说的话。
    • 不,我说的是装饰器的 @ 语法。
    • 它不应该与使用命令扩展有任何关系。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2019-09-21
    • 2019-11-16
    • 2020-12-06
    • 2021-02-16
    • 2019-11-14
    • 2021-07-20
    相关资源
    最近更新 更多