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.
Start by opening a command prompt, then type the following command: Installing the required libraries
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
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.
Now, it should automatically redirect you to your bot page. Click on Bot, and then Add Bot. Click, Yes, do it.
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.
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.
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.
Write your discord bot
Start by making a new folder for your bot files to reside. It can be anywhere you like.
Open up your IDE of your choosing and navigate to the directory you made.
From there, create a new python file. You can name it whatever you like, just make it easy to type, like
main.py
.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.Now we get to the coding part. Start by importing all the needed libraries, it's as simple as:
import osimport discordfrom dotenv import load_dotenv
- Initialize the dotenv library by adding the following line below:
load_dotenv()
- Now go back to the .env file and create a new variable called token and paste your token:
token=your_token_here
- 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")
- Now we get to actually code the bot. Start by initializing the client. To do that, it's as simple as:
client = discord.Client()
- 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.eventasync def on_ready():# Print this when the bot starts up for the first time.print(f'{client.user} has connected to Discord!')
- 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.eventasync 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!")
- 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 osimport discordfrom dotenv import load_dotenvload_dotenv()token = os.getenv("token")client = discord.Client()@client.eventasync def on_ready():print(f"{client.user} has connected to Discord!")@client.eventasync def on_message(message):if message.author == client.user:returnif 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:3WORKDIR /appCOPY . .RUN python3 -m pip install -U discord.py python-dotenvCMD 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
- Sign up to Qovery.
- Web
- CLI
Sign in to the Qovery web interface.
Create a new project, and choose your git repo.
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.
Go back to your app, then settings, and select Dockerfile as the build mode. Qovery will automatically choose the Dockerfile.
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!