Breaking Down a Hello World Discord Bot in Python

Breaking Down a Hello World Discord Bot in Python Featured Image

As promised in this series, we won’t be pulling any punches. We’ll be diving right into the code with plans to have a Discord bot as soon as possible. In fact, we’re going to create one today!

Table of Contents

What Is Hello World?

For some reason, I feel uniquely qualified to answer this question because I’ve written a Hello World program in over 100 different programming languages—thanks to my Sample Programs repo. Also, I have a fun series where a break down Hello World in several languages.

Of course, I don’t think know how to write a Hello World program is something to brag about. It’s all about taking that first step; writing a program that actually does something. In this case, that thing is printing a nice “Hello, World!” message to the user.

This type of program can be written a lot of different ways. For example, in Python, we can print the phrase “Hello, World!” to the user as follows:

print("Hello, World")

Python will interpret this line by mapping the text to the Python grammar (i.e., the syntax rules for Python). For example, any text followed be parentheses is perceived to be a function. In this case, the function is the “print” function which is responsible for converting data into text for the user. Anything we pass to this print function will automatically get converted to text before displaying it to the user.

Saying “Hello, World” to Discord

Unfortunately, we can’t print a message in Discord directly in the same way we can print to the user. That’s because we have to establish a connection to Discord first. To do that, we have to make use of some Discord tools:

import discord

To keep it simple, this line imports all the tools we need to start creating our Discord bots.

Next, we’ll create our connection to discord:

webhook = discord.Webhook.from_url(
  "<your webhook url>", 
  adapter=discord.RequestsWebhookAdapter()
)

Here, we get to run a function similar to the print function that we mentioned before. In this case, we’re calling the `from_url` function from the Discord tools. Inside this function, we pass two pieces of data: our webhook URL (see here for details on how to get one) and the adapter we’ll be using to connect to Discord. This creates a webhook that we can use to communicate with Discord.

After that, we can finally say “Hello, World!”:

webhook.send("Hello, World!")

If everything works correctly, we should see a “Hello, World!” message in our Discord server. That’s it! Three lines of code.

Unavoidable Theory

One thing I wanted to avoid in this series was a lot of overbearing theory. My thought was that I could create material that would allow you to experiment without getting bogged down in all the logistics. “Just code,” I thought.

Well, as it turns out, there are some details that we need to get the code working above. First, remember that Discord tools thing I mentioned before? We can’t just import them. We need to install them first. One way to do that is to run one of the following commands from your console:

pip install discord
py -m pip install discord

This should help you get off the ground and running (though, I also had to install requests), but it’s not a permanent solution. As a result, I recommend getting a tool like PyCharm to help you manage 3rd party tools like Discord. You can find more details about PyCharm and other options here.

Also, as I mentioned already, you’re going to need to generate a webhook URL for yourself to be able to run the code above. Hopefully, the tutorial I provided can help you out. Otherwise, I think that’s enough theory/logistics to get us going!

Taking on a Challenge

If you manage to code these three lines of code running on your system, I have a challenge for you! Try passing different values to the `send` function. What happens? For example, if you decide to run a number to Discord, does that number get sent?

webhook.send(9)

While you’re playing around with different types of data, I recommend checking out some of these articles which will help you conceptualize some of the types of data that you can send:

Once you’ve had a chance to do some experimenting, we’ll move on to trying new things! Don’t worry if you don’t understand everything. This whole series is about experimentation.

Looking Ahead

Now that we have a program which can send messages, we can start having some fun. In the next article, we’ll look at all the things we can do with our webhook. After that, we’ll start learning python by making our bot do interesting stuff!

In the meantime, why not take some time to explore this site and get familiar with the types of material that we have to support you. Here are a few examples:

Also, here are some resources from the folks at Amazon (#ad):

Otherwise, see you next time! Take care.

Learn Python by Building Discord Bots (4 Articles)—Series Navigation

In 2021, I started toying with the idea of building discord bots. Very quickly, I got over my fear of learning something new when I realized I had nothing to be scared of. This feeling got me thinking: what if I created some teaching material around Discord bots? You could learn Python and build something cool at the same time! So, I decided to give it a go.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Code Posts