How to kick users on command

Question:

I don’t have much knowledge on Python, and still in the process of learning it.

I have been significantly modifying an open-source Discord bot coded in Python 2.7.

I would like to add a feature that allows me to kick a user based on a command.

Something like
[commandprefix]kickuser [userid]
But I have no idea how to make the bot grab the userid from the message I sent, and when I try to make it ultra-specific to kick my second account as a test, it doesn’t work either.

    if message_content == ',purgeIdiots':
        await kick('userid')
        return

That’s the ultra-specific code where I manually enter a userID into the document. But I cannot get it to work.

I’m extremely new and I’d appreciate some help.

Asked By: sairento

||

Answers:

If you’re looking into the process of making commands, I’d suggest looking further into reading about the discord.ext.commands sub-library of discord.py.

Here’s an FAQ about it, and an example of a bot using it following:

FAQ

Bot Example

This extension will allow you to create commands using prefixes.

In regards to your question about kicking via user id, with the command extension you could do something like:

@bot.command(pass_context = True)
async def kick(ctx, userName: discord.User):
    await bot.kick(userName)

I believe that should work, but I can’t compile it just yet to check. However, do learn more about the command extension as it’ll help you out a lot more than checking messages for content.

You’ll first need to import discord.ext, you can do that with from discord.ext import commands at the top of the program.

You’ll then need to define bot to ensure you can use stuff like @bot.command, because you’ll need that. This is done like this: bot = commands.Bot(command_prefix=',', description=description), with the comma being defined as the command prefix now.

This should allow the code snippet I added originally to function with a user being able to type ,kick <username>.

Answered By: ocelot

This is my kick command which i have used in my bot
note: u need to write this thing before writting the command below ==> from discord.ext.commands import has_permissions, CheckFailure, BadArgument

@bot.command(pass_context=True, name="kick")

@has_permissions(kick_members=True)

async def kick(ctx, *, target: Member):

if target.server_permissions.administrator:

    await bot.say("Target is an admin")

else:
    try:
        await bot.kick(target)
        await bot.say("Kicked")
    except Exception:
        await bot.say("Something went wrong")

@kick.error

  async def kick_error(error, ctx):

if isinstance(error, CheckFailure):

     await bot.send_message(ctx.message.channel, "You do not have permissions")

elif isinstance(error, BadArgument):

    await bot.send_message(ctx.message.channel, "Could not identify target")

else:
    raise error

So now the command @bot.command(pass_context=True)

@has_permissions(kick_members=True) ==> It checks if
the user using that command has that permission or not. The rest of it is Self explanatory.
@kick.error parts checks the error on that kick command. Note: If in the first part u right async def kick_command in
@kick.error u must right @kick_command.error.

Also note:
in your bot command u have written @client=command.Bot(command_prefix=’YOUR_PREFIX’)

in the @bot.command()
u just have to change @bot to the thing u have written in that @client:
for ex. If u have wrriten @mybot.command_Bot(command_prefix=’YOUR_PREFIX’)
u have to change @bot to @mybot.command(). If u have any questions feel free to ask

Answered By: bos gamer
@commands.has_permissions(kick_members=True)
@bot.command()
async def kick(ctx, user: discord.Member, *, reason="No reason provided"):
        await user.kick(reason=reason)
        kick = discord.Embed(title=f":boot: Kicked {user.name}!", description=f"Reason: {reason}nBy: {ctx.author.mention}")
        await ctx.message.delete()
        await ctx.channel.send(embed=kick)
        await user.send(embed=kick)
Answered By: THEMEGACODERS

Hey bro I have modified your code and now it will work and it has embeds too

@client.command(description="Kicks a member from the server...")
@has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
try:
    if ctx.author.guild_permissions.kick_members:
        await member.kick(reason=reason)
        em = discord.Embed(
            title="kicked the person",
            description=" kicked "f'{member.mention} from the server',
            colour=discord.Colour.random(),
    )
   em.set_image(url="https://gcdnb.pbrd.co/images/mQtFtodtDYsb.jpg? 
      o=1")
   em.set_author(name="TASK DONE :)",  # Me :)
              icon_url="https://emoji.discord.st/emojis/676f16e6-cb20-4251- 
              87cc-7f598fb21321.gif")
   em.set_footer(text="DONE  ")
   await ctx.send(embed=em)
except:
      await ctx.send("Bot doesn't have the perms to kick members );")
Answered By: I am goku
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.