4

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

2 Answers2

5

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

K. Koster
  • 142
  • 9
  • can you fix? `NameError: name ‘kick_members’ is not defined` –  Dec 19 '19 at 14:00
  • @CodingAndMemes i changed the code as this is the easiest way to do it, i believe – K. Koster Dec 19 '19 at 14:43
  • I changed it up a little bit, I put this: `async def kick_command(self, ctx, Member: discord.Member):` ` await kick(Member)` And it gives me this: `NameError: name ‘kick’ is not defined` –  Dec 19 '19 at 23:14
  • probably because you put `kick(Member)` instead of `ctx.kick(Member)` – K. Koster Dec 19 '19 at 23:44
1

There is actually a better way of doing this without importing. I would recommend not importing and just doing this the way I am showing below because it will save you some time if you plan to import all of those.

This doesn't require importing anything for has_permissions.


@commands.has_permissions(kick_members=True)  
async def kick(self, ctx, Member: discord.Member):
    await bot.kick(Member)
Leo
  • 2,328
  • 2
  • 21
  • 41