仅允许特定角色使用命令

时间:2020-08-18 15:00:15

标签: discord.py

我一直在命令更改用户角色,但是目前,每个人都可以使用它。我尝试做一些事情。 这是我的代码:

async def addrole(ctx, user: discord.Member, role: discord.Role):
    await user.add_roles(role)
    await ctx.send(f"I gave {user.name} the role {role.name}")

这是我唯一想到的东西:

allowed = ["Role 1"]
if message.author.role in allowed:
   async def addrole(ctx, user: discord.Member, role: discord.Role):
      await user.add_roles(role)
      await ctx.send(f"I gave {user.name} the role {role.name}")

谢谢!

1 个答案:

答案 0 :(得分:1)

只需添加@commands.has_any_roles()

@bot.command()
@commands.has_any_roles("Role 1", "Role 2")
async def addrole(ctx, user: discord.Member, role: discord.Role):
    await user.add_roles(role)
    await ctx.send(f"I gave {user.name} the role {role.name}")

如果不允许用户使用该命令,则将引发commands.MissingAnyRole错误。