Creating a webhook object and using it to send simple messages is only interesting for so long. In this article, we’ll take a look at other things we can do with our webhook object.
Table of Contents
Revisiting Hello World
In the previous article, we talked about how to write a bot that would send “Hello, World!” to Discord. If you did a little experimentation, you might have even found that you can send pretty much any type of data to Discord including numbers and lists—all with the following three lines of code:
import discord webhook = discord.Webhook.from_url("<your webhook url>", adapter=discord.RequestsWebhookAdapter()) webhook.send("Hello, World!")
One of the cool things about this code is that we can call
send as many times as we like. For instance, the following is perfectly valid code:
import discord webhook = discord.Webhook.from_url("<your webhook url>", adapter=discord.RequestsWebhookAdapter()) webhook.send("Hello, World!") webhook.send("My name is Jeremy, and I brought this bot to life.")
All of this is possible because of the webhook object we created. Let’s learn a little more about what it can do.
Demystifying the Webhook Object
In modern programming, there are a variety of paradigms (more on paradigms here) that determine how we approach the process of programming. The webhook object is an example of object-oriented programming, and it allows us to perform actions on data.
One of the actions we performed already was
send which allowed us to send a message to a Discord channel. Of course, that’s not all
send can do! For example, we can change the username that shows up when we send a message. To do that, we have to specify a keyword argument:
webhook.send("Hello, World!", username="Bowser")
Keyword arguments are a little different from normal arguments because we specify them using their name and an equals sign. That said, they work just like regular arguments. In other words, this message will carry the same content as before but with a new username.
Why stop there? We can also specify an avatar URL:
webhook.send( "Bwah hah hah!", username="Bowser", avatar_url="https://mario.wiki.gallery/images/7/7d/MSOGT_Bowser.png" )
In this case, I have a Bowser profile sending “Bwah hah hah!” And if you want members of your Discord server to hear Bowser speak, why not turn on text-to-speech?
webhook.send( "Bwah hah hah!", username="Bowser", avatar_url="https://mario.wiki.gallery/images/7/7d/MSOGT_Bowser.png", tts=True )
Unfortunately, some of the other options are a little less intuitive to setup. For example, we have the option to send pictures, but that requires us to get familiar with some of Discord’s data types. As a result, we’ll skip that for now.
But Wait! There’s More!
One thing that’s worth mentioning is that the webhook object is write-only. That means, we can only send messages to Discord; we cannot read them. Don’t worry! In the future, we’ll talk about ways to write bots that do more interesting things. That said, in the meantime, let’s get comfortable with more of the things we can do with a Discord webhook.
In short, you can find everything you could possible want to know about Discord webhooks here. That said, here’s a quick summary. Here’s a list of all the functions available at the time of writing:
As it turns out,
execute does the same thing as
delete deletes this webhook,
delete_message deletes a message,
edit edits the webhook,
edit_message edits a message, and
avatar_url_as gets the avatar url as an image. Clearly, the function names are all very descriptive, so they do more or less what you would expect.
Taking on a Challenge
As I’ve mentioned several times already, I want this series to be more experimental. Rather than me telling you exactly what everything does and how to make sense of it, the onus is on you to experiment. In education, we call this constructivism (i.e., learn by doing), and I find it to be a great way to learn to code.
As a result, here’s my challenge for you this time around. Can you extend your previous Hello World code to make use of the extended
send functionality. For example, have you tried changing your webhook’s avatar URL or turning on tts?
Once you have a hang of keyword arguments, head on over to the Discord Python documentation, and try to make sense of it. What happens when you try to edit your webhook using
edit? How about if you try to delete a message using
Don’t feel discouraged if you run into any issues. That’s all a part of the learning process! Also, I’m happy to field any questions you have.
Once again, we’re through yet another Discord bot lesson. Again, no worries if this all seems new and overwhelming. The best thing you can do for yourself is to celebrate whenever you get something working. Hopefully, you’re sending all kinds of silly bot messages to your friends.
Next time, we’ll take a look at a more interesting webhook bot example. Specifically, we’ll learn how to create a dice rolling bot that we can use to learn about variables in Python. Hope to see you there!
In the meantime, consider reading up a bit about Python here:
- 10 Answers to Google Autocomplete Questions About Python
- Algorithmic Thinking for Python Beginners
- Making Sense of the Python Interpreter
And as always, 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
Thanks again for checking out this article, and I hope you got something out of it. Until next time!
Recent Code Posts
With a free service like Google Forms, you can expect there to be some hiccups. That's why it's sometimes helpful to automate their data collection.
If you're learning a new programming language, sometimes it's helpful to compare its syntax to something you already know.