Learn to Code via Tutorials on Repl.it!

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

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, first is to remove the user from the server and 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 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.

Unbanning

This part gets slightly trickier. You can't just unban someone by mentioning them because they are not in 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 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.

Commentshotnewtop
justnoe (0)

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

josysalt (1)

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

FishballNooodle (133)

@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

josysalt (1)

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

FishballNooodle (133)

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

FishballNooodle (133)

@josysalt
Btw stay tuned for my tutorial later