Create a Discord Bot with Python (Part 1)

Yash Semlani
5 min readNov 21, 2020

Hello everyone! Welcome to my series on how to create a full featured Discord Bot with Python. This tutorial is aimed at those who want to create a Discord Bot with real features as opposed to just a Hello World bot. This series uses the latest version of discord.py. To access the latest version of the code for my completed discord bot go to https://github.com/Halfblood1223/Bebot-Discord-Bot.

The prerequisites to create this bot are a basic knowledge of Python, as well as a knowledge of the requests package. The requests package isn’t important at the start, but as we start working with various APIs to implement different functions, it increasingly becomes valuable. In addition to this, we will be exploring various other modules that may require some level of proficiency if you want to change the code I provide to you.

  1. Getting Started

The first thing we need to talk about is what a Discord Bot with Python is and the technologies that make it run. For Python, we are going to use the discord.py wrapper. Discord.py wraps the Discord API in an easy to use Pythonic form.

The first thing you’ll need to do is to install the discord.py library. This can be done by using the command underneath.

 pip install discord.py

After that, go to https://discord.com/developers/applications and set up your application. I won’t go into specifics on how to do that here, but I will provide a very useful tutorial on how to set it up: https://realpython.com/how-to-make-a-discord-bot-python/.

The final step in this process is to set up a testing server and adding your bot to it. The tutorial above details how to do this as well.

2. Project Structure

Right now, all the terms I’m going to layout in this section will sound very abstract and probably won’t make sense; however, it will be important later, so keep it in mind.

Our project is going to be structured into Cogs for each command group and will be divided into different classes for different types of functionality within the Cogs. For our database, we will be using MongoDB; however, for most of the storage, we will just use dictionaries within the bot program.

3. Basic Commands

Now we can get into the good stuff. Today, we’re going to start off with the Basics and how to implement them, as well as the start up code to get started with Cogs.

The first thing we need to address is what Cogs are. Cogs are essentially subparts of the main Bot program. They are instantiated in the main file and have a set up method at the bottom. Here is the code for an example Cog.

import discordfrom discord.ext import commandsclass excog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def hello(self, ctx):
await ctx.send("Hello this is Bot here")
def setup(bot):
bot.add_cog(excog(bot))

This code is instantiating the cog with a bot parameter and creating a set up method so that the main file knows what to do with this file. This is what the main file looks like which is instantiating this cog.

import discord
from discord.ext import commands
#setup
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix="YOUR PREFIX HERE", intents=intents, case_insensitive=True)
TOKEN = "YOUR BOT TOKEN HERE"
@client.event
async def on_ready():
print(f"{client.user} is active")
client.load_extension("cogs.example")
client.run(TOKEN)

Lets talk about what this code is doing. The first thing that we do in this code sample is import the necessary libraries. One library that is of interest is the discord.ext library. This is imported because we need it to access our Cogs. Side Note: The Cogs are being stored in a folder called cogs which is why we are referencing Cogs.example.

The second thing that this code is doing is setting up our client and its Intents. Intents are essentially permissions that allow you to access certain events. For example, “intents.members = True” allows you to see certain user events. In our case, we are using it for a welcome command. The next line sets up an instance of our bot with a certain prefix for commands. For example, my prefix is “b.” so I can call commands with b.yourcommandhere. The line after this is fairly simple as all we are doing is putting in our bot token. You can use environment variables for this, but to keep it simple, I won’t be doing that here.

The third thing that this code does is implement a server side logging command. The way that this is done is through @client.event and the async on_ready() function. One important feature of the discord.py library is its reaction abilities. The way it works is by using @client.event or @commands.Cog.listener and then denoting an event. In this case, our event is on_ready() which occurs whenever the bot starts up. For a full list of events check the docs here: https://discordpy.readthedocs.io/en/latest/api.html#event-reference. When this event occurs, the code inside the denoted function executes. In this case, we are just printing that the bot has started to the console.

The final thing that the code does is load our example cog by referencing the folder it’s in and then the name of the file. This will load this Cog and allow all the commands within it to be accessible to the end user. After that, the bot takes in the token you provided earlier and turns on our bot.

If all goes well, you should get this result when running b.hello :

4. Overview

Lets go over what we discussed. The first thing we talked about was what Cogs are. Cogs are essentially collections of commands that are separated into different classes. After that, we learned how to set up the main file by adding Intents, importing the necessary libraries, setting up logging, and running everything. In the next part, we are going to cover how mod commands are going to be set up. Setting up Mod Commands is a bit harder than what we started with today, so I would recommend reading over the documentation for the discord.py library and ironing out any difficulties with what we worked on today. Enjoy your new bot and see y’all next time. 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