检查用户是否在 discord.py 中具有权限

2022-01-15 00:00:00 python discord discord.py repl.it

问题描述

我正在为我的 discord.py 机器人创建一个踢命令.以下是 kick 命令可能的一些代码:

I am making a kick command for my discord.py bot. Here is some code that the kick command might be:

    async kick_command(self, ctx, userName: discord.User):
      prefix_used = ctx.prefix
      alias_used = ctx.invoked_with
      text = msg[len(prefix_used) + len(alias_used):]
      if discord.User permissions = "admin":
        try:
          kick(User)
        except Exception as e:
          ee = "Error: " + e
          await ctx.send_message(content=ee)

我很确定 if 语句和 kick(User) 是无效的语法.你能帮帮我吗?

I am pretty sure the if statement and kick(User) are invalid syntax. Can you help me?

我的代码:点击这里


解决方案

试试这个:

@bot.command()
@has_permissions(kick_members=True)  
async def kick(self, ctx, Member: discord.Member):
          await bot.kick(Member)

@kick.error
async def kick_error(error, ctx):
   if isinstance(error, MissingPermissions):
       await ctx.send("You don't have permission to do that!")

别忘了导入 has_permissions:from discord.ext.commands import has_permissions

相关文章