How to make a discord bot in python

Chapter 1: getting ready!

What’s included in this chapter:

Discord is an awesome messaging platform to chat with your friends. But did you know you could also build a bot? In this series we’ll go over the major concepts of making a discord bot in python.

Why a bot?

Discord bots take your discord experience to the next level. Anything is possible! You could create a bot that lets you play a fun card game or you could build a bot that moderates your server, it’s all up to you. For a fact, you could even start a business with your bot as the major attraction.

An example of popular discord bots

Getting a bot from the discord developer portal

Before we dive into actual coding, there’s a few things we need to get ready.

First of all we need a working discord account. If you don’t have one yet, you can make one here. Then we need to create a discord application in the discord developer portal.

To create an application click the “New Application” button.

Now that we have a discord application, the next step is to add a bot user. We do this by clicking the bot button in the list on the left.

Then we click the add bot button and choose “yes” in the prompt. Now we have a bot user and are ready to dive into some coding.

Coding a simple bot

The bot we are about to code uses the discord.py module, so before we can use its functions we have to include it in our project. We do this with the pip installer, so just write:

pip install discord.py

This will make the discord.py module available for global use. If you only want your current folder/project to be able to access the module, you may want to create a python virtual environment first.

Next we will create a python file that will serve as the core of our bot. A name like bot.py or main.py will do.

Our first step is to establish a connection to discord. We do this by instancing the “Client” class of discord.py.

import discord
client = discord.Client()

@client.event
async def on_ready():
    print('{} has connected!'.format(client.user))

client.run("your token")

A quick breakdown of the code so far. First we import the discord.py module, then we create a discord client instance on the next line. This client contains a lot of information like the name of your bot, the bot avatar url and the bot’s id, just to name a few.

Then further on we create our first event listener. The bot (and all discord bots in fact) is based on asynchronous programming. This means we’ll see a lot more of that “async” keyword and asycnhronous principles. In our case the event listener will print that our bot has connected to the console, once the bot is ready.

To finish the code, we run the bot with the run function. The only thing that needs to be changed is the token inside the function. This should be replace with your own token. You get this token from the developer portal as well, as shown on the image.

Running the bot

That’s it, we coded our first bot! All that’s left is to run the bot so it responds. We do this by running the python file. On windows this means we need to do:

py bot.py

And on other platforms:

python bot.py

And we’ll shortly see the bot online in discord.

In chapter 1 we got ready for further development and created our first bot. We’ll learn how to welcome a member and code a simple command in the next chapter!