python - 检查命令是否有检查 "guild_only"

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

我正在尝试为我的机器人制作一个 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 检查 在检查列表中。但它没有。

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

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

这里是 commands.guild_only:

<function guild_only at 0x0481F150>

两者相似但不相同。

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

最佳答案

你可以做类似的事情

@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

关于python - 检查命令是否有检查 "guild_only",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65196191/

相关文章:

python - 如何使用 Librosa 在 python 中读取音频?

python - 如何从我创建的函数创建循环和新数据集?

Python - 我应该使用线程还是进程来进行网络事件?

python - Python 3.5.1 中requests模块如何获取URL的IP地址

具有基于键的默认值的Python dict

javascript - javascript 中未附加的匿名函数和双重命名方法?

javascript - 让 For 循环成为一个函数

python - 如何并行对 pandas 数据帧执行多个 SQL 查询

python - 关于在 Bokeh 中使用的数据

python - Django 应用程序的 Cython : would it work?