Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to Make A Discord Bot - Part 6 (Updated 2021)
FishballNooodle (211)

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.

async def kick(ctx,member:discord.Member,*,reason=None):
  await member.kick(reason=reason)
#A Ban is pretty much the same
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:

Take note that the Member name is highlighted blue because it is a member object.


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:

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')

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.

GustavoGodoy1 (0)

i get same error as everyone else here

SlimeyKingX925 (0)

it says client.command not defined

FishballNooodle (211)

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

SlimeyKingX925 (0)

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

FishballNooodle (211)

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
FishballNooodle (211)

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

GustavoGodoy1 (0)

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

FishballNooodle (211)

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

MythicalHydra (0)

your tutorials a great. it helped me a lot

DarkyDiscord (0)

it does not work is an error

SamNovaDfu (0)

hi! could you maybe post a repl, so that we could browse the code?

JaJsemLolek (0)

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

FishballNooodle (211)

should be client, lowercase

justnoe (0)

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

FishballNooodle (211)

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

josysalt (1)

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

FishballNooodle (211)

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

josysalt (1)

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

FishballNooodle (211)

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

FishballNooodle (211)

Btw stay tuned for my tutorial later