Creating a Discord Bot With Python

If you’ve found this article useful, consider donating.

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!

Before Starting

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

Creating a Discord Application

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 Library

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

If you don’t already have Python and pip installed you can go here to install them.

Installing the Requests Module

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 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 “” 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

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

First let’s import “” into the “” file with the following line:


Now, in the file named “,” 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 “” 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.

Complete Code

The following is the “” file.

The following is the “” file.

Congratulations, you just created your first Discord bot using Python!

You can also find the complete code over at my Github.


Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store