Creating a Discord Bot With Python

Before Starting

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.

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.

Downloading the Discord.py Library

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

python3 -m pip install -U discord.py

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.

pip3 install requests

Testing the Bot

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

python3 discord_joke_bot.py
$hello

Making API Requests With Python

Now let’s take a look at how to actually make API requests with Python and the Requests module.

Adding the $joke Command

We can finally move on to actually adding in our “$joke” command.

import joke_api.py

Complete Code

In total we should have two files. Namely the “discord_joke_bot.py” file and the “joke_api.py” file.

Resources

Here is a list of resources that may be helpful:

--

--

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