我正在尝试为我的机器人制作一个 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/