Check if user has permission in discord.py

user12327406 picture user12327406 · Dec 18, 2019 · Viewed 10.7k times · Source

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)

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

My code: click here

Answer

K. Koster picture K. Koster · Dec 19, 2019

Try this:

@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!")

don't forget to import the has_permissions: from discord.ext.commands import has_permissions