How to kick a user using slash commands Discord.py

Question:

I’m trying to make my Discord bot kick a member, and send that "user banned because reason" to a specific channel and not the channel the command was used.
The code I’m using:

@bot.slash_command(description = "Kick someone", guild_ids=[1041057700823449682])
@commands.has_permissions(kick_members=True)
@option("member",description = "Select member")
@option("reason",description = "Reason for kick (you can leave this empty)")
async def kick(
    ctx, 
    member: discord.Member,
    channel: bot.get_channel(1042042492020863037),
    *, 
    reason=None):
    if reason==None:
      reason="(no reason)"
    await ctx.guild.kick(member)
    await ctx.respond("Done :)")
    await ctx.channel.send(f'User {member.mention} was kicked because {reason}')

When I try using this code I get a few errors:

Traceback (most recent call last):
  File "c:UsersfontiDocumentsProjetos PythonBot do DiscordIniciar Bot.py", line 152, in <module>
    async def kick(
  File "C:UsersfontiAppDataLocalProgramsPythonPython310libsite-packagesdiscordbot.py", line 905, in decorator
    self.add_application_command(result)
  File "C:UsersfontiAppDataLocalProgramsPythonPython310libsite-packagesdiscordbot.py", line 127, in add_application_command
    command._set_cog(None)
  File "C:UsersfontiAppDataLocalProgramsPythonPython310libsite-packagesdiscordcommandscore.py", line 603, in _set_cog
    self.cog = cog
  File "C:UsersfontiAppDataLocalProgramsPythonPython310libsite-packagesdiscordcommandscore.py", line 827, in cog
    self._validate_parameters()
  File "C:UsersfontiAppDataLocalProgramsPythonPython310libsite-packagesdiscordcommandscore.py", line 705, in _validate_parameters
    self.options: list[Option] = self._parse_options(params)
  File "C:UsersfontiAppDataLocalProgramsPythonPython310libsite-packagesdiscordcommandscore.py", line 745, in _parse_options
    option = Option(option)
  File "C:UsersfontiAppDataLocalProgramsPythonPython310libsite-packagesdiscordcommandsoptions.py", line 210, in __init__
    self.input_type = SlashCommandOptionType.from_datatype(input_type)
  File "C:UsersfontiAppDataLocalProgramsPythonPython310libsite-packagesdiscordenums.py", line 707, in from_datatype
    if datatype.__name__ in ["Member", "User"]:
AttributeError: 'NoneType' object has no attribute '__name__'. Did you mean: '__ne__'?

I was trying to send the message…

(f'User {member.mention} was kicked because {reason}')

to a specific channel. If I remove the channel condition, the bot works, but sends this message to the channel the command was used.

Asked By: Jack142

||

Answers:

To send it in the channel, instead of using ctx.channel.send, you can use ctx.send. I think that’s where you’re running into your error. Also here’s how I tend to set up my kick command using slash commands so that my answer makes more sense:

@nextcord.slash_command() # I use nextcord, a dpy fork so your setup is gonna be different
@commands.has_permissions(whatever permissions you want)
async def kickuser(self, ctx, member : nextcord.Member, *, reason='None'): # default reason is none so that it is optional in the slash command
      # side note for nextcord.Member, having it there makes it so that there's a drop down menu that functions the same way as if you were to @ someone in a message. This makes it easier to kick the right person        
      embed = discord.Embed(description=f'You have been kicked from {ctx.guild} for reason: {reason}')
      
      embed = nextcord.Embed(description=f'{member} has been kicked for reason: {reason}') # setting up embed to send
      await ctx.send(embed=embed) # sends the embed in chat letting people know the person has been kicked
      await member.kick(reason=reason) # actually kicking the person, this comes after all the other steps so that we are able to mention and direct message them that they have been kicked

Hope this helps

Answered By: Sam Shields

This snip of code uses PyCord ( Confirmed to work by myself )

@discord.default_permissions(kick_members = True)
async def kick(ctx, member : discord.Member, *, reason=None):
        await member.kick(reason=reason)
        await ctx.respond(f'{member.mention} has been kicked!')
Answered By: Im2Slothy

Just get a channel object with bot.get_channel()

Then use the channels send() function to send your message.
Also, : after arguments in function definitions are type hints, they are made for IDEs, if you want to assign a default value, you have to use = instead. (Look at your channel argument)

EDIT

You are using type hints in your code. Type hints are made for IDEs in first place, so they can show you mistakes in your code more easier. But you are „setting“ a value in it with the function, but this is for discord.py None, thats causing your error. Use : for showing which class an argument has to be. But use = for setting a default value, if this argument is not being passed.

def function(type_hint: str, default_value = 0, mixed : int = 10):
    print(type_hint, default_value, mixed)

Answer again if you need even further help 😉

Answered By: Kejax

I believe the cause of your error is your channel definition inside your kick command definition. Try removing the channel definition from your kick command definition and put it inside the function instead. The way I have it setup on my bot, other than the channel definition, is the same as yours and mine works perfectly

Answered By: Sam Shields
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.