Making Sense of the Discord Webhook Object in Python

Making Sens of the Discord Webhook Object in Python Featured Image

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 hereOpens in a new tab.. That said, here’s a quick summary. Here’s a list of all the functions available at the time of writing:

  • avatar_url_as
  • delete
  • delete_message
  • edit
  • edit_message
  • execute
  • send

As it turns out, execute does the same thing as send. Meanwhile, 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 delete_message?

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.

Looking Ahead

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:

And as always, here are some resources from the folks at Amazon (#ad):

Thanks again for checking out this article, and I hope you got something out of it. Until next time!

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