Creating A Discord Bot with Python (Part 2)

Yash Semlani
5 min readNov 29, 2020

Hello Everyone! Welcome to the second part of the Discord Bot with Python series. In this part, we are going to be going over the basics of Moderation commands as well as further details about the way the discord.py library works.

  1. What are Moderation Commands?

First, we need to detail what Moderation commands are. For our case, we’ll classify them into two categories: Static and Active moderation. Static moderation is single events such as kicking a user. Active Moderation is constant analysis for some type of action by users. A curse filter would fall into this category. For this part of the series, we’re only going to be going over Static moderation commands, but for the next installment, I’ll be going into detail on active moderation.

2. Implementations

Static moderation is very simple to implement and requires mainly a proficiency with the various methods of the discord.py library. The commands that are implemented in this snippet are Mute, Unmute, Clear, Nuke, Ban, Softban, Kick, and Unban.

There are three categories of commands here: Ban, Mute, and Clear. Ban removes a user from the server. Mute silences or unsilences a user. Clear gets rid of messages.

Lets first go over how the Clear commands work. At the heart of the clear commands is the “ctx.channel.purge(limit=num_messages)” method. To understand what this method is, we first need to understand the idea of context (ctx stands for context). Context is essentially a set of attributes that is passed to the function. The way that it is passed is dependent on the command that comes after the “@”. For example, in our mod commands, we pass “@commands.command()” to the function. This passes information about what the conditions of the function call are. It gives us details like the channel that this command was called in. This is important for our Clear commands because we take the attribute “ctx.channel” and use the purge method on that channel. The purge command takes in the argument “limit” and clears that many messages from the channel in order of most recent to least recent. There are also some assorted methods within the command that are added for cleanliness and certain edge cases, but in most cases, you can just use the purge method.

With that, we’ve covered the basics of the Clear commands. I encourage you to try and implement this before you move onto the next section. This will help you improve your understanding of the methods as well as allow you time to look at the discord.py docs to understand the cleanliness functions.

Moving on, lets look at the ban functions which are three part. We have the Unban method, Ban method, and Kick commands. First, we’ll address the Ban method. The Ban command is powered by the “member.ban” method. What this method does is take in a member object and bans them from the guild where this command was summoned. The reason this works is because we are getting a member object by specifying “member: discord.Member”. If we do not specify that we will get a string that we have to parse. The Unban command is very similar to the ban method in that it is powered by the “member.unban” method. However, because the user has already been banned it is not always possible to get an @mention of them. Because of this, we have to take in a user id. This user id comes in the form of a string which we then have to parse and turn into a user object. From there, the procedure is largely the same as the ban function where you do a user operation. The Kick command is essentially the same as the ban command. You just take in a member object and use a built in method of the member class on the member object. In this case the built in method is “member.kick().”

Finally, lets go into the Mute commands. The Mute commands are very simple to implement. All they do is take in a member object in and give it the muted role. There are two chains of logic in the Mute command. The first chain of logic is for when the Muted role doesn’t exist. In that case, we create a Muted role and set the permissions of that role in every channel to disallow speaking. The way we do this is by attaining a list of all the channels of the guild that this command was summoned in. This is done by getting the “ctx.guild.channels” attribute, which is a list. We then iterate over all the channels in the list and set permissions in each to be what we desire in this case muting the users with this role. Then, the role is given to the specified member and all the permissions are applied to them. The second chain of logic is for when the Muted role does exist. This chain of logic is the same as the first except that it forgoes the creation of the Muted role.

With that we’ve completed the Static Moderation Commands. All the code is available above so make sure to read it thoroughly to get a better understanding of how everything works and all the cleanliness functions.

3. Overview

In this lesson we went over how to create Static Moderation Commands. In doing so we went over what Context is. Context will be a very important part of all the future commands we create. Next, we looked at the Member object and some of the built in methods that can be applied to it. Finally we pieced that all together into the three types of commands; Mute, Ban, and Kick. This lesson is a good indicator of the difficulty of the future lessons. The only additional resources I’d recommend looking at are the discord.py docs and specifically the member methods. If you want to get a head-start on the next lesson look into the basics of listeners. Enjoy the new commands that you have and come back for the next lesson. All feedback is welcome at yashveersemlan@gmail.com.

--

--

Yash Semlani

High Schooler with a strong interest in Tech. Host of the TechIN series featuring innovative tech innovators. Contact me at yash.made@gmail.com