如何检查用户是否有权使用命令?不和谐

时间:2021-05-14 12:45:33

标签: discord discord.py

我正在创建自定义帮助命令,但遇到问题。默认的帮助命令可以以某种方式隐藏调用该命令的用户不可用的命令。我到处寻找,但找不到任何解决方案。我尝试使用 hidden 对象的 command 属性,但它不起作用。最好的解决方案是使用 Member.can_use(command) 之类的方法,它会返回 TrueFalse,但文档中没有这样的方法。或者至少我找不到它。 将不胜感激。

2 个答案:

答案 0 :(得分:1)

使用 Command.can_run 方法,例如:

@bot.command()
async def can_it_run(ctx, command_name: str):
    command = bot.get_command(command_name)
    can_use = await command.can_run(ctx)
    if can_use:
        await ctx.send(f"You can use {command_name}") 
    else:
        await ctx.send(f"You can't use {command_name}") 

遗憾的是,如果检查引发异常(如 commands.guild_only),则不会抑制异常,如果您想检查其他人(不是调用者)是否可以使用您必须使用的命令覆盖 Context.author,一个方便的函数是:

async def can_run(ctx, command_name: str, member: discord.Member=None) -> bool:
    command = bot.get_command(command_name)
    if command is None: # Command doesn't exist / invalid command name
        return False # Or raise an exception

    if member is not None: # If a member is passed overwrite the attribute
        ctx.author = member

    try:
        return await command.can_run(ctx)
    except Exception:
        return False

@bot.command()
async def can_it_run(ctx, command_name: str, member: discord.Member=None):
    resp = await can_run(ctx, command_name, member)
    await ctx.send(resp)

答案 1 :(得分:0)

您可以通过执行以下操作来检查用户是否包含某个 ID:

if (message.author.id == "WHoever ID1" || message.author.id == "Whoever ID2"):
   #Do Something

如果您想放入一堆 ID,请执行以下操作:

bool = false;
arr = ["ID1", "ID2", "ID3"]

for x in arr:
    if(x == message.author.id):
        bool = true;

if(bool):
  #Do stuff
  bool = false;

这是非常模糊的代码,但应该与此类似