Skip to content

Instantly share code, notes, and snippets.

@tobihans
Forked from izxxr/README.MD
Created March 30, 2022 20:28
Show Gist options
  • Save tobihans/9e2c83b50f59fe642c1832b721a88989 to your computer and use it in GitHub Desktop.
Save tobihans/9e2c83b50f59fe642c1832b721a88989 to your computer and use it in GitHub Desktop.
Discord.py Webhook Guide (Both async and sync).

Basic Webhooks Example using Discord.py (Rewrite)

Webhooks are a great way to send messages to Discord without having a bot account. You just need a webhook URL and just do a POST request on that URL and the message will be sent to discord.

Webhooks can also be used if your bot has to send messages to a channel a lot. For example, If your bot has event logging so everytime having to fetch channel and sending can be slow so you can use some webhook magic.

This guide will tell you when exactly to use webhooks and how to use them in discord.py both async and using requests.

When to use webhooks?

  • You are sending messages into Discord from another app or program and don't want to get into issue of making a bot account.
  • You are sending messages frequently to a channel like events logging or error logging.

When NOT to use webhooks?

  • When you are making a normal bot and don't have to do something frequently.

Making webhook in a server

To create a webhook in your server, Go to server settings > Integrations Tab > Webhooks > New Webhook

Creating webhook.

Give it a name and select a channel in which it will send the messages. (Yes, you have to select a channel in which webhook will send message you cannot send it in multiple channels) Copy the URL and start coding!

Webhooks in Discord.py

Discord.py provides a great way of sending webhook messages.

Using requests (non-async)

from discord import Webhook, RequestsWebhookAdapter # Importing discord.Webhook and discord.RequestsWebhookAdapter

webhook = Webhook.from_url('webhook-url-here', adapter=RequestsWebhookAdapter()) # Initializing webhook
webhook.send(content="Hello World") # Executing webhook.

Using these three lines, We have sent "Hello World" message to a webhook.

Embeds

You can send embeds too which are discord.Embed object.

from discord import Webhook, RequestsWebhookAdapter, Embed # Importing discord.Webhook and discord.RequestsWebhookAdapter as well as Embed class

webhook = Webhook.from_url('webhook-url-here', adapter=RequestsWebhookAdapter()) # Initializing webhook
embed = discord.Embed(title="Hello World", description=":wave:") # Initializing an Embed
embed.add_field(name="Field name", value="Field value") # Adding a new field
webhook.send(embed=embed) # Executing webhook and sending embed.

Customized Name and Avatars

Webhooks are great because you can change it's URL and avatar on every execution! Pretty cool ain't it?

You just have to pass in username and avatar_url argument in send() method.

from discord import Webhook, RequestsWebhookAdapter # Importing discord.Webhook and discord.RequestsWebhookAdapter

webhook = Webhook.from_url('webhook-url-here', adapter=RequestsWebhookAdapter()) # Initializing webhook
webhook.send(username="Webhook URL", avatar_url="http://some-image-url.here", content="Hello World") # Executing webhook.

Using aiohttp (async)

Although above method is easy but shouldn't be used in discord bots that execute webhooks frequently because they can be blocking functions.

For discord bots or other async programs, Discord.py has AsyncWebhookAdapter!

from discord import Webhook, AsyncWebhookAdapter # Importing discord.Webhook and discord.AsyncWebhookAdapter
import aiohttp # We need aiohttp for async usage.

async def coroutine():
  async with aiohttp.ClientSession() as session:  
    webhook = Webhook.from_url('webhook-url-here', adapter=AsyncWebhookAdapter(session)) # Initializing webhook with AsyncWebhookAdapter
    await webhook.send(content="Hello World") # Executing webhook.

You can change avatar and name in above method too.

Thanks.

I hope this guide helped you to understand webhooks. If it did you can help me by starring it. Thanks 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment