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:
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:
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!”:
If everything works correctly, we should see a “Hello, World!” message in our Discord server. That’s it! Three lines of code.
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?
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:
- The Self-Taught Guide to Type Systems in Python
- How to Format a String in Python: Interpolation, Concatenation, and More
- How to Create a List in Python: Loops, Comprehensions, and More
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.
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:
- Can You Actually Return Multiple Values From a Function in Python?
- 11 Python Practice Problems for Beginners
- Roll Your Own Uppercase Function in Python
Also, here are some resources from the folks at Amazon (#ad):
- Effective Python: 90 Specific Ways to Write Better Python
- Python Tricks: A Buffet of Awesome Python Features
- Python Programming: An Introduction to Computer Science
Otherwise, see you next time! Take care.
Recent Code Posts
In my years of coding, you'd think I'd have learned the lesson that software systems are hard. Well, I learned that lesson once again in an attempt to move a bunch of Java projects from Eclipse to VS...
This might seem like an extremely niche issue, but it's a problem I struggled to solve with the existing resources on the internet. As a result, I'm here to share how I got it done!