Nightingale Discord Bot

Prashant Gaurav
4 min readNov 19, 2021

What is a Discord Bot?

In simple words, it’s a program that can watch over servers to which it has been added. This includes managing roles, looking at events happening on the server, responding to commands, answering questions, etc.

There are different variations of discord bots available online, some focus on specific tasks, like playing music(FredBoat) or roasting members(DankMemer) while others are general, like this one. Learn how to create a solid bot with a variety of features from scratch using python in this post.

Source code is available at Github

First Things First!

In this tutorial, I will be using an awesome library called discord.py and some other libraries while coding. You can find the super awesome documentation of discord.py Here. Highly recommend you to go through these docs and understand them as this is very very important and will help you a lot in your development. Firstly, we will look at some basic classes and annotations which are used in building a discord bot, and then we will be using them to understand useful features of the Nightingale bot.

To learn how to set up the bot, generate a client secret, and add your bot to your server check out my previous post Link

But for the time being, you can learn from this post 😋

The Bot Client 🤖

Represents a client connection that connects to Discord. This class is used to interact with the Discord Web-socket and API.

This is basically the instance of the Bot class and can be thought of as the actual object to control the bot. Using this we can direct the bot to do things like sending messages and managing roles. We can also get all the information about the server (aka guild ) and its members.

The bot client is instance is created in the following way:

Server Events🔰

Events are basically actions or occurrences on the part of the server bot has access to along with the changes that bot goes through.

So several changes can be caught and handled by using different event classes provided. There are different kinds of events, for instance, on_ready which is triggered when the client is done preparing the data received from Discord. on_message is another such event handler that is triggered when a message is added to any text channel on the server.

These events are very useful in developing different features of the bot. One such example would be the Anti advertisement feature of Nightingale. Basically, it prevents the members of the server from sharing the join link on any of the text channels. Whenever a member posts a link on the server the on_message event is triggered and the message can be analyzed to check if it contains any discord server invite. If it does, the bot automatically deletes the message and sends the user a warning. This can be understood from the code snippet below.

Two other important event listeners are on_member_join() and on_member_remove() which are pretty self-explanatory. Both the events get a member in arguments which can be used to carry out different tasks about the member(ex. giving a member a particular role or checking if they are allowed to join the server, etc). In the Nightingale bot, they are used to add members to a local MongoDB database which is a NoSQL database. Below is the Snippet of the code of how it is being carried out.

Note: on_member_remove event is triggered on two events- when a member exits a server and when they get kicked/banned by someone which that permission.

Change ThiS XX to be the main script

In the code above whenever a new member joins the server, their record is getting updated to the database and status set to Online. Whereas when they leave the server only the status is changed to Offline. Their record still stays with us for analytical purposes.

There are dozens of other useful events like on_raw_reaction_add, on_message_delete, etc which you can read about on the discord.py docs.

Modularizing The Code — Cogs🗃

One of the most important aspects of writing any python program is the modulation of different components. We don’t want to cramp it all up in a single python script(.py file). There should be a division of different features depending on their properties. There comes a point in bot’s development when we want to organize a collection of commands, listeners, and some state into one class. Cogs allow you to do just that.

So in simple terms, Cogs are basically different classes that can be hooked into the bot. These classes can be created to serve a definite purpose, for example, a cog that is responsible for handling roles of members, another cog that has all the commands.

Loading All Cogs from ‘Cogs’ Directory.
Example Cog Src

Defining commands and listeners in a cog is similar to the main script with minor variations which will be cleared from an example code. It's a good practice to keep the tasks of a bot divided which helps in testing and debugging as well.

In the next blog, I’ll be explaining some specific features that I added to the bot — invitation system, handling roles, punishments, etc which you can use in your bot to better handle your public servers.

--

--

Prashant Gaurav

Hi, I like to write blogs about challenges I faced while working on new tech so that the solution could benefit others & myself in case I forget it later. :P