【问题标题】:Check if a command has the check "guild_only"检查命令是否具有检查“guild_only”
【发布时间】:2020-12-08 09:18:48
【问题描述】:

我正在尝试为我的机器人创建一个 DM 帮助命令,该命令会根据我添加的命令数量自动生成。我希望它省略带有guild_only 检查的命令。

这是我的代码:

import discord
from discord.ext import commands
...
...

# Imagine that in this case, `command` is a command with the `guild_only` check.
# This is in a much larger for loop
if commands.guild_only not in command.checks:
    # add command to string of commands
    help_c = ''.join([help_c, f"`{ctx.prefix}{command.name}{brief}`"
                              f"{separate_into_aliases(command)}\n{desc}\n\n"])

我假设 commands.guild_only not in command.checks 会返回 False,因为 guild_only 检查 is 在检查列表中。但它没有。

这是带有guild_only的命令的检查属性列表返回:

[<function guild_only.<locals>.predicate at 0x04A60150>]

这里是commands.guild_only

<function guild_only at 0x0481F150>

两者相似但不相同。

我的问题是如何检查命令是否在其checks 属性中检查了guild_only

【问题讨论】:

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


    【解决方案1】:

    你可以这样做

    @commands.guild_only()
    @commands.is_owner()
    @commands.command()
    async def show_checks(self, ctx):
        checks = list(map(lambda s: s.split('.')[0], map(lambda f: f.__qualname__, ctx.command.checks)))
        # printing checks gives ['is_owner', 'guild_only']
        checks = [s.split('.')[0] for s in [f.__qualname__ for f in ctx.command.checks]]
        # same result but shorter code
    
    

    【讨论】:

      猜你喜欢
      • 2023-01-08
      • 2019-03-11
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2013-02-28
      • 2020-06-05
      相关资源
      最近更新 更多