How to create and deploy a Discord bot in Python

This tutorial will show you how to create and host a Discord bot made with Python.

Discord bots are rather famous lately for their flexibilty and ease to make simple ones, provided you have the needed knowlegde. In this guide, you'll learn how to create a very simple Discord bot in Python that will respond to messages and how you can host it with Qovery.

Discord.py is a famous Python package you can use for creating complex bots made in Python that can do everything Discord's API supports. It's flexible and easy to use.

Installing the required libraries

Start by opening a command prompt, then type the following command:
python3 -m pip install -U discord.py python-dotenv

This will fetch the latest version of both packages from PyPI. If you run into any issues, make sure pip is installed and set up correctly. You can follow the pip docs here to install it correctly.

Now you are ready to write your Discord bot!

Get your Bot token

  1. Head back to the browser, and go to the Discord developer portal. Here you are going to log in with your account, and click on New Application on the top right. From there, give your bot a name. It can be anything you like.

  2. Now, it should automatically redirect you to your bot page. Click on Bot, and then Add Bot. Click, Yes, do it.

  3. From here, you're gonna need the token, so go ahead and click on copy, and keep it somewhere for now until we use it.

  4. Go back to the OAuth2 tab, and click on Bot, then below, check all the permissions you want to have. For the purpose of the tutorial, I just clicked on Administrator, which gives the bot every permission.

  5. Copy the link that comes up, and paste it in your browser. Discord will ask you which server you want to add your bot, so choose the one you want. After that, your bot should be in your server. It will be offline and that's okay for now.

Discord Portal

Write your discord bot

  1. Start by making a new folder for your bot files to reside. It can be anywhere you like.

  2. Open up your IDE of your choosing and navigate to the directory you made.

  3. From there, create a new python file. You can name it whatever you like, just make it easy to type, like main.py.

  4. You're also going to create a new file called .env. There we are going to store all our sensitive values like our bot token.

  5. Now we get to the coding part. Start by importing all the needed libraries, it's as simple as:

import os
import discord
from dotenv import load_dotenv
  1. Initialize the dotenv library by adding the following line below:
load_dotenv()
  1. Now go back to the .env file and create a new variable called token and paste your token:
token=your_token_here
  1. After that, you're ready to import the value from the file. Go ahead and add this line to your code:
token = os.getenv("token")
  1. Now we get to actually code the bot. Start by initializing the client. To do that, it's as simple as:
client = discord.Client()
  1. Then, we're going to be adding a very simple function where the client will say something in the console when the bot is ready. To do this, you can add this function to your code:
@client.event
async def on_ready():
# Print this when the bot starts up for the first time.
print(f'{client.user} has connected to Discord!')
  1. Now, we can make a simple function where the bot will respond to someone when they say hello. The following code is responsible for that:
@client.event
async def on_message(message):
# Ignore messages from the bot itself so that there's no conflict.
if message.author == client.user:
return
# Respond to hello.
if message.content == 'hello':
await message.channel.send("Hi there!")
  1. Lastly, we're going to need to make the bot run when we run our file. Add this line to your code:
client.run(token)

The finalized code should look something like this:

import os
import discord
from dotenv import load_dotenv
load_dotenv()
token = os.getenv("token")
client = discord.Client()
@client.event
async def on_ready():
print(f"{client.user} has connected to Discord!")
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == 'hello':
await message.channel.send("Hi there!")
client.run(token)

Write a Dockerfile

In order to host your bot to Qovery, you're going to need a very simple Dockerfile. This one should be sufficient enough for this simple application.

FROM python:3
WORKDIR /app
COPY . .
RUN python3 -m pip install -U discord.py python-dotenv
CMD python -u ./main.py

For more complex applications and a more in depth look in Dockerfiles, you can check out this tutorial.

Final Steps

All that's left now is adding all your files (except the .env file) in your git repo. I'm not going to go in depth in this tutorial since that's not the purpose of this tutorial, but there are plenty of guides online.

Hosting your bot with Qovery

  1. Sign up to Qovery.
  • Sign in to the Qovery web interface.

    Qovery Sign-up page

    1. Create a new project, and choose your git repo.

    2. Go to your app, select Environment Variables, then add your token. This will make sure the token is injected during runtime so the bot can start without any issues.

    3. Go back to your app, then settings, and select Dockerfile as the build mode. Qovery will automatically choose the Dockerfile.

    4. Click deploy and your bot should be deployed in the next few minutes.

    Your bot is now online!

    Should you run into any issues, feel free to join the Qovery Discord server and ask for help there!

    Tutorial