Creating A Discord Bot with Python (Part 2)
By: Yash Semlani
Tags: Programming, Discord Bot, Software Development, Python, Discord

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.