Bryson Noble
Bryson Noble

Reputation: 351

How can I kick a user by ID in Discord.py

I was wondering how I could make a command that kicks a specific user every time, without having to mention the user.

I am looking for something like this:

@client.command()
async def kick(ctx):
  user = #user id
  await user.kick(reason=None)

And not like this:

@client.command()
async def kick(ctx, member : discord.Member, *, reason=None):
  await member.kick(reason=reason)

Thanks in advance

Upvotes: 1

Views: 767

Answers (2)

deveIop
deveIop

Reputation: 43

you can use the built in converter for member : discord.Member like this:

from discord.ext import commands

@client.command()
async def kick(ctx, member: commands.MemberConverter, *, reason):
    await ctx.guild.kick(member, reason=reason)

correct me if i'm wrong but it should work

you can watch this video if you're confused: https://www.youtube.com/watch?v=MX29zp9RKlA (this is where i learned all of my discord.py coding stuff)

Upvotes: 0

Nurqm
Nurqm

Reputation: 4743

You can get a member from his/her id by using discord.Guild.get_member.

@client.command()
async def kick(ctx):
  user = ctx.guild.get_member(<user id>)
  await user.kick(reason=None)

Or you can use discord.utils.get. I'd recommend you to use the first one but this is also a option.

@client.command()
async def kick(ctx):
  user = discord.utils.get(ctx.guild.members, id=<user id>)
  await user.kick(reason=None)

References

Upvotes: 2

Related Questions