Discord Bot Tutorial

Techweek 2022 LaSalle College

Hello, welcome to this tutorial to use during the presentation of the Techweek LaSalle Colllege Montreal.

Next we will present the information to prepare our bot in Discord.

Let's start!

Docker Instalation

We are going to use Docker as a Runing Envarioment, this makes it easy for us to run the Discord Bot in a universal development environment for Python 3.10 with all the necessary libraries for the correct operation.

To achieve this we need you to do the following prior to our presentation

  1. You need to create a Docker account at (https://www.docker.com/)
  2. You need download and install Docker

Once you download and install Docker, the next step is to download the container image that we have prepared for an optimal development environment for this moment.

You can find the image of Docker in the follow repository. (https://github.com/esquerg/discordbot)



You can place the downloaded image in your preferred location, however we recommend that it be the root of the C:\ since we are going to execute a series of commands from the command line to run our container.

Open your Docker App and leave it there while you run the following commands in the shell

Execute the Command by pressing the Windows key + r, then we run cmd

Go to the dirrectory where you put your Docker container discordbot-main

Finally you need these two commands to get your Docker up and running.

docker build -t discord-bot .
docker run -it --rm --name discordio discord-bot

In your first step it will look like this.



In your last step it will look like this.



Finally in your Docker App you will see that the container is running.



If you have already reached this point, everything is ready for us to see each other at Techweek, if you have not achieved it, do not worry, we will briefly repeat the steps and you can also support us during the workshop.


How to Create a Discord Bot Account step by step.

From this moment on, these instructions are for reference to guide you during our talk at TechWeek 2022, each of these steps will be shared live.

In order to work with the Python library and the Discord API, we must first create a Discord Bot account. Here are the step to creating a Discord Bot account.

  1. Logged on to the Discord website.(https://discord.com/)
  2. Navigate to the application page.(https://discord.com/developers/applications)
  3. Go to click on the New Application button.
  4. Go to give the application a name and click Create



  5. Go to the Bot tab and then click Add Bot You will have to confirm by clicking Yes, do it!



  6. Please keep the default settings for Public Bot, as the picture indicates (checked) and Require OAuth2 Code Grant have to remain (unchecked).



  7. Now, rour bot has been created.
  8. Now we have to copy the token.

This token is your bot's password so don't share it with anybody. It could allow someone to log in to your bot and do anything he wants.

You can regenerate the token anytime


How to Invite Your Bot to Join a Server

Now you have to get your Bot User into a server. To do this, you should create an invite URL for it. Go to the OAuth2 tab. Then select bot under the scopessection. Now choose the permissions you want for the bot. Our bot is going to mainly use text messages so we don't need a lot of the permissions. You may need more depending on what you want your bot to do.



After selecting the appropriate permissions, click the copy button above the permissions. That will copy a URL which can be used to add the bot to a server.

Paste the URL into your browser, choose a server to invite the bot to, and click “Authorize”. To add the bot, your account needs Manage Server permissions.

Now that you've created the bot user, we'll start writing the Python code for the bot.


How to Set Up Discord Events for Your Bot

discord.py revolves around the concept of events. An event is something you listen to and then respond to. For example, when a message happens, you will receive an event about it that you can respond to.>

Let’s make a bot that replies to a specific message. This simple bot code, along with the code explanation, is taken from the discord.py documentation. We will be adding more features to the bot later.

Add this code to main.py. (You can name the file something else if you like, just not discord.py.) I'll explain what all this code does shortly.


When you created your bot user on Discord, you copied a token. Now we are going to create a .env file to store the token. If you are running your code locally, you don't need the .env file. Just replace os.getenv('TOKEN') with the token. .env files are used for declaring environment variables.


This code line by line

“Now let's go over what each line of code is doing in your Discord bot code.”

  1. The first line imports the discord.py library.
  2. The second line imports the os library, but this is only used for getting the TOKEN variable from the .env file. If you are not using a .env file, you do not need this line.
  3. Next, we create an instance of a Client. This is the connection to Discord.
  4. The @client.event() decorator is used to register an event. This is an asynchronous library, so things are done with callbacks. A callback is a function that is called when something else happens. In this code, the on_ready() event is called when the bot is ready to start being used. Then, when the bot receives a message, the on_message() event is called.
  5. The on_message() event triggers each time a message is received but we don't want it to do anything if the message is from ourselves. So if the Message.author is the same as the Client.user the code just returns.
  6. Next, we check if the Message.content starts with '$hello'. If so, then the bot replies with 'Hello!' to the channel it was used in.
  7. Now that the bot is set up, the final line runs the bot with the login token. It gets the token from out .env file.