In this article, we’ll create a Discord bot using Python. The bot we’ll be creating today will have a simple purpose, telling jokes. We will give the bot a command and it will return a random joke. This should be simple enough, let’s get started!
I think it’ll be useful to understand what an API is before we actually get started. API stands for Application Programming Interface. Simply put, an API is what allows programs or services to communicate with other programs or services.
A great metaphor I came across (don’t remember the original source) was thinking of an API as a waiter at a restaurant.
So you, the customer, talk to a waiter to get your food. The waiter then talks to the chef who actually makes your food. The chef gives the food to the waiter. Finally, the waiter then gives the food to you.
Unpacking this metaphor, the user (customer) sends a request to an API (waiter). The API (waiter) then talks to the service you want to reach, usually a database (chef). That database (chef) then sends data (food) back to the API (waiter). And finally, the API (waiter) then gives you back data (food).
Great! Now that we know what an API is and have a general understanding of how it works, we can now move on to using one.
Acquiring the Jokes
To actually get the jokes, we’ll have to use the Official Joke API. This API is extremely easy to use, you just use this link and every time you call it, it will return a random joke in JSON format. Simple!
Creating a Discord Application
To create a Discord bot and invite it into your own Discord server, you can follow these steps here.
You can name it whatever you’d like but I’ll just name it “Jokes-Bot.”
Also, under the Settings tab to the left, go to “OAuth2” > “Scopes” and tick the bot option. Then in the same page, go to “Bot Permissions” and tick the Administrator box.
Now copy the link from the “Scopes” section and paste it onto your browser to add the bot to your Discord server.
Cool! Now just take a note of the Discord bot token in the “Bot” tab because we’ll be needing it later.
Downloading the Discord.py Library
We’ll be using the Discord.py library to create our Discord bot.
To install this library onto your machine you can follow these steps, or simply run the following command on the command line.
python3 -m pip install -U discord.py
If you don’t already have Python and pip installed you can go here to install them.
Installing the Requests Module
Finally we need to download and install one more thing. To actually send requests and receive data from an API using Python we’ll need to download and install the python module Requests.
Simply type in the following command onto the command line to install this module.
pip3 install requests
Now it’s finally the time you’ve all been waiting for, the code.
Testing the Bot
To test that our bot actually works, copy and paste the following code into your favorite text editor.
To learn more about what this code does go here.
Now insert your Discord bot token into the last line where it says “your token here.” Save your python file as “discord_joke_bot.py” and then input the following command onto the terminal to run and test your bot.
If all went well you should see something like the following:
Now we can type the following command
into our Discord server and the bot should respond back with “Hello!”
Cool so the bot works.
Also, whenever we make changes to the bot, you’ll have to restart the python script by running it again.
Making API Requests With Python
Now let’s take a look at how to actually make API requests with Python and the Requests module.
The following code is what we’ll be using to make requests to the Joke API.
Let me explain what’s going on here.
On line 1 we import the Requests module.
Line 3 is the url we’ll be using and I just defined it in the global scope.
Now on line 6, I define a function named “check_valid_status_code” that will check if the status code from the API is successful or not.
If the status code is equal to 200, meaning it’s a successful call, it will return back the request in JSON format. If it’s unsuccessful, it will just return false.
Most of the time, whenever you make an API call, the API will return a status code. These status codes tell you whether an API call was successful or not. To learn more about status codes check out this article.
Now on line 13 I define another function named “get_joke.” This function, as implied by its name will get a joke.
First it makes a GET request to the url we pass it. Then it will call “check_valid_response_code” and store whatever it returns into a variable named “data.” Finally it returns the variable “data.”
Adding the $joke Command
We can finally move on to actually adding in our “$joke” command.
First let’s import “joke_api.py” into the “discord_joke_bot.py” file with the following line:
Now, in the file named “discord_joke_bot.py,” we can simply replace the if statement containing the “$hello” parameter on line 16 with the following if statement.
Let me explain what’s going on here.
So first on line 1, we check if the message we type into Discord starts with the “$joke” string.
Then on line 2 we call “get_joke” function from the “joke_api.py” file and save it into a variable named “joke.”
The variable “joke” stores a dictionary with the following keys: id, type, setup, and punchline.
The only dictionary keys we are interested in are “setup” and “punchline.”
Now on line 4 we check if the variable “joke” is “False.” If so we just return an error message.
Else, if it’s not false we return the joke setup with “joke[‘setup’]” adding a newline character, ‘\n’ and then adding the joke punchline with “joke[‘punchline’].”
Now if we test this in our Discord server with our bot we should receive a joke from our bot.
Great! It works.
In total we should have two files. Namely the “discord_joke_bot.py” file and the “joke_api.py” file.
The following is the “discord_joke_bot.py” file.
The following is the “joke_api.py” file.
Congratulations, you just created your first Discord bot using Python!
You can also find the complete code over at my Github.
Here is a list of resources that may be helpful: