Creating a Discord Bot With Python
Tags: python, codenewbie, tutorial, beginners
Published: 2020-02-02 05:11:17

*[If you've found this article useful, consider buying me a cup of coffee.](https://www.patreon.com/alanjc)*

<i><a href="https://blog.alanjc.com/articles/creating-a-discord-bot-with-python.html" target="_blank">This article was originally published on my blog.</a></i>

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!

<h3>Before Starting</h3>

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.

<h3>Acquiring the Jokes</h3>

<p>To actually get the jokes, we'll have to use the <a class="underline" target="_blank"
                                href="https://github.com/15Dkatz/official_joke_api">Official Joke
                                API</a>. This API is
                            extremely easy to use, you just use <a class="underline" target="_blank"
                                href="https://official-joke-api.appspot.com/random_joke">this link</a> and every time
                            you call it, it will return a random joke in JSON format. Simple!
                        </p>

<h3>Creating a Discord Application</h3>

<p>
To create a Discord bot and invite it into your own Discord server, you can follow <a class="underline" target="_blank"
                                href="https://discordpy.readthedocs.io/en/latest/discord.html">these
                                steps here</a>.
</p>

<p>You can name it whatever you'd like but I'll just name it "Jokes-Bot."</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vue4mnye4a0e29kqjoxp.png)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mfs9pecft7973iwj2kz4.png)

Now copy the link from the "Scopes" section and paste it onto your browser to add the bot to your Discord server.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w7enwmt3g4hiz8hl3ww0.png)

Cool! Now just take a note of the Discord bot token in the "Bot" tab because we'll be needing it later.

<h3>Downloading the Discord.py Library</h3>

We'll be using the Discord.py library to create our Discord bot.

<p>
                            To install this library onto your machine you can follow <a class="underline"
                                target="_blank"
                                href="https://discordpy.readthedocs.io/en/latest/intro.html#installing">these steps</a>,
                            or simply run the following command on the command line.
                        </p>

<code>python3 -m pip install -U discord.py</code>

<p>
                            If you don't already have Python and pip installed you can go <a class="underline"
                                target="_blank" href="https://realpython.com/installing-python/">here</a> to install
                            them.
                        </p>

<h3>Installing the Requests Module</h3>

<p>
                            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
                            <a class="underline" href="https://requests.readthedocs.io/en/master/"
                                target="_blank">Requests</a>.
                        </p>

Simply type in the following command onto the command line to install this module.

<code>pip3 install requests</code>

Now it's finally the time you've all been waiting for, the code.

<h3>Testing the Bot</h3>

To test that our bot actually works, copy and paste the following code into your favorite text editor.

{% gist https://gist.github.com/AlanConstantino/eec9629d3de15ec939cc4a26388b0004 %}

<p>
                            To learn more about what this code does go <a class="underline" target="_blank"
                                href="https://discordpy.readthedocs.io/en/latest/quickstart.html#a-minimal-bot">here</a>.
                        </p>

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.

<code>python3 discord_joke_bot.py</code>

If all went well you should see something like the following:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d6hoecr6ccmhpbjb5j7c.png)

Now we can type the following command <code>$hello</code> into our Discord server and the bot should respond back with "Hello!"

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/px14l9mrxir5i7fxusmo.png)

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.

<h3>Making API Requests With Python</h3>

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.

{% gist https://gist.github.com/AlanConstantino/2e11886fa6042c449f715055e3682284 %}

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.

<p>
                            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 <a class="underline" target="_blank"
                                href="https://restfulapi.net/http-status-codes/">this article</a>.
                        </p>

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."

<h3>Adding the $joke Command</h3>

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:

<code>import joke_api.py</code>

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.

{% gist https://gist.github.com/AlanConstantino/ad022928a9516927989680b5f3365385 %}

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u2bm5n1y5whlgssx2m03.png)

Great! It works.

<h3>Complete Code</h3>

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.

{% gist https://gist.github.com/AlanConstantino/512d49c14fae1bf69273bffe22a9bb0a %}

The following is the "joke_api.py" file.

{% gist https://gist.github.com/AlanConstantino/2e11886fa6042c449f715055e3682284 %}

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

<p>
                            You can also find the complete code over at my <a class="underline" target="_blank"
                                href="https://github.com/AlanConstantino/discord-joke-bot">Github</a>.
                        </p>

<h3>Resources</h3>

Here is a list of resources that may be helpful:

<ul>
                            <li><a class="underline" target="_blank"
                                    href="https://discordpy.readthedocs.io/en/latest/">Discord.py docs</a></li>
                            <li><a class="underline" target="_blank"
                                    href="https://requests.readthedocs.io/en/master/">Requests module docs</a></li>
                            <li><a class="underline" target="_blank"
                                    href="https://discordapp.com/developers/docs/intro">Discord Bot docs</a></li>
                            <li><a class="underline" target="_blank"
                                    href="https://github.com/15Dkatz/official_joke_api">Official Joke API</a></li>
                        </ul>