Skip to content
← Back to Community
How to Make A Discord Bot - Part 6 (Updated 2021)
Profile icon
FishballNooodle

Hey everyone as promised, I will be posting another tutorial.
Did you know my Bot is Called Moderation, and what is a Moderation Bot if can't ban or kick people? Today, we will be going over banning, kicking and unbanning.
Firstly, we need to know the differences between these 3 operations.
Banning has 2 parts to it, the first is to remove the user from the server and the second is to block him from ever joining back, even if he has a valid invite link.
Kicking on the other hand is much simpler, it just removes the user from the server, if he has a valid invite link, he can join back.
To unban someone is to remove that blockage that was previously there to prevent them from joining again. Now, if he has a valid invite link, he can join back.

Banning and Kicking

Now let's get into the code. To be honest, you yourself will be surprised at how short it is.

@client.command() @commands.has_role('Moderators') async def kick(ctx,member:discord.Member,*,reason=None): await member.kick(reason=reason) #A Ban is pretty much the same @client.command() @commands.has_role('Moderators') async def ban(ctx,member:discord.Member,*,reason=None): await member.ban(reason=reason)

Something new here is the 2 Argument, we actually take in member as an argument but the colon sign gives it a special data type, in this case, it's a discord.Member class which is simply accepting member as a member object. Lastly, we add a keyword argument for a reason because the in-built ban function has a reason parameter, if no extra reasons were given it would be None. You can find the reasons in the audit log of your server.
I also added the command check so only moderators can run this command.
This is how the command will be typed like:
image
Take note that the Member name is highlighted blue because it is a member object.

Unbanning

This part gets slightly trickier. You can't just unban someone by mentioning them because they are not on your server to mention. If you don't get it, you can just try mentioning a banned member, it won't turn blue.
Instead, we have to get them to manually type the players' whole Discord Tag and we will break it up into the name and discriminator.
For example: Fishball_Noodles is my Name, and #7209 is my Discriminator.
We will then compare it to the ban entries (aka list of banned players).
This is how it looks like:

@client.command() @commands.has_role('Moderators') async def unban(ctx,*,member) BanList = await ctx.guild.bans() MemberName, MemberDiscrim = member.split('#') for BanEntry in BanList: user = BanEntry.user if (MemberName,MemberDiscrim) == (user.name,user.discriminator): await ctx.guild.unban(user) await ctx.send(f'{user.mention} has been Unbanned') return

Basically what we did above was to see if the input member matched both the name and discriminator of every member in the ban list. If it did, it will unban him.
Return will stop the code from checking anymore.

Ok, that pretty much covers up banning and kicking, this might be useful if you don't want to give them a full administrator role to change your server but just to kick and ban people.
Hope this tutorial was helpful, see you tomorrow for another one.
Once again if you faced any problems regarding any of my tutorials, please feel free to ask in the comments section below.

Voters
Profile icon
VinsonDinh
Profile icon
The-real-Purple
Profile icon
vLightzy
Profile icon
DanielSong11
Profile icon
Arbuzzz
Profile icon
RevenantCashew
Profile icon
sunyyyy12
Profile icon
GamingWithWit61
Profile icon
thebowsercatbow
Profile icon
TheNutAlmond
Comments
hotnewtop
Profile icon
HarisankarSUnni

I didn't get errors but when I try to kick/ban someone its not kicking and showing error

Profile icon
HitmanTAMTAKOS

File "main.py", line 32
async def unban(ctx,*,member)
^
SyntaxError: invalid syntax

Profile icon
GustavoGodoy1

i get same error as everyone else here

Profile icon
SlimeyKingX925

it says client.command not defined

Profile icon
FishballNooodle

@SlimeyKingX925
Sorry for taking a long time to reply.
Did you do @client.command?
The @ symbol is neccessary

Profile icon
SlimeyKingX925

@FishballNooodle bro,whenever i make bots functions, i use bot.command, and if i use that, it says undefined

Profile icon
FishballNooodle

@SlimeyKingX925
You should be importing as such
from discord.ext import commands
Usage should be as such:

@client.command() #or @bot.command() async give_xp_command (ctx,user:discord.User,exp_amount): #code here
Profile icon
FishballNooodle

@SlimeyKingX925
U need to use @bot.command
not `bot.command``\

Profile icon
SlimeyKingX925
Profile icon
GustavoGodoy1

@SlimeyKingX925 can you help me? i got same error as yours

Profile icon
FishballNooodle

@GustavoGodoy1
He followed the steps above and it is solved
You should try the steps above

Profile icon
MythicalHydra

your tutorials a great. it helped me a lot

Profile icon
ExpCoder

it does not work is an error

Profile icon
FishballNooodle

@DarkyDiscord
what error please elaborate

Profile icon
JaJsemLolek

AttributeError: 'Client' object has no attribute 'command'
whats wrong with it why it doesnt work

Profile icon
FishballNooodle

@JaJsemLolek
should be client, lowercase
@client.command()

Profile icon
justnoe

What is the utility of <@commands.has_role('Moderators')>?
by the way, thanks forr the tutorial, there are suuuper useful!

Profile icon
FishballNooodle

@justnoe
Check if the person using the commands has a role name "Moderators"

Profile icon
josysalt

Hey why did you delete your Moderation 1.0 files? I was looking for them!

Profile icon
FishballNooodle

@josysalt
People were just copying it of and asking me how it worked.
Which was not how it was supposed to work.
I was supposed to teach you all.
If i could let you all copy, i would have put in under share not tutorials

Profile icon
josysalt

@FishballNooodle o i just wanted to compare.
btw my code isnt in this account its on my other account.

Profile icon
FishballNooodle

@josysalt
You can just ask me, i won't judge you.
But i will judge you if you just copy of my code

Profile icon
FishballNooodle

@josysalt
Btw stay tuned for my tutorial later