Skip to content

Instantly share code, notes, and snippets.

@izxxr
Last active November 16, 2022 20:13
Show Gist options
  • Save izxxr/086a16bfd52b32b34a587b356bc32584 to your computer and use it in GitHub Desktop.
Save izxxr/086a16bfd52b32b34a587b356bc32584 to your computer and use it in GitHub Desktop.
Discord.py Webhook Guide (Both async and sync).

⚠️ Notice

This guide is for discord.py 1.7 version. In upcoming v2.0, Webhooks were greatly overhauled and in fact simplified a lot. They no longer require adapters. Usage of webhooks is now quite straight forward. As such, this guide is now outdated and should not be followed.

Furthermore, this guide is focused on discord.py. No support for forks are covered in this guide.

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 😁

@wotanut
Copy link

wotanut commented May 6, 2021

Very useful guide tysm

@izxxr
Copy link
Author

izxxr commented May 6, 2021

@wotanut

Very useful guide tysm

Glad you liked it 👍

@oliiiiiiiiiiiii
Copy link

Cool I was perfect thing at the perfect time

@izxxr
Copy link
Author

izxxr commented May 28, 2021

Cool I was perfect thing at the perfect time

👍 I'm glad it helped you. :D

@Salvare933
Copy link

from discord import Webhook, RequestsWebhookAdapter
ImportError: cannot import name 'RequestsWebhookAdapter' from 'discord' why

@izxxr
Copy link
Author

izxxr commented Feb 28, 2022

@bigzOnScratch This guide is no longer up to date with with the 2.0 version of discord.py. Webhooks were greatly overhauled in that updated removing the "adapters".

@Phonfo
Copy link

Phonfo commented Mar 3, 2022

so, is there any replacement of that module in pycord? (pycord is the replacement of discord.py)

@izxxr
Copy link
Author

izxxr commented Mar 4, 2022

No, this guide isn't based on any of the forks. Read their documentation instead. Webhooks were changed in discord.py 2.0 and forks are based off that version so there wouldn't be practically any difference b/w their webhook implementation.

@axhuwastaken
Copy link

cool!

@Will9221
Copy link

Will9221 commented Jun 4, 2022

How could I edit a webhook? That's something I would like to do but am not sure how

@wotanut
Copy link

wotanut commented Jun 9, 2022

@Will9221 it is not possible to edit webhooks

@izxxr
Copy link
Author

izxxr commented Jun 9, 2022

How could I edit a webhook? That's something I would like to do but am not sure how

You can use edit() method on your async or sync webhook instance counterpart. Also I suggest reading the documentation next time. Nevertheless, This guide is now outdated if you are using v2.0 of discord.py.

@Will9221 it is not possible to edit webhooks

@wotanut It is.

@wotanut
Copy link

wotanut commented Jun 9, 2022

You can edit the webhook and it's properties, but not the actual sent message, that's what I meant. Sorry for any confusion.

@izxxr
Copy link
Author

izxxr commented Jun 10, 2022

You can edit the webhook and it's properties, but not the actual sent message, that's what I meant. Sorry for any confusion.

That's possible too, Webhook.edit_message(id, ...) or WebhookMessage.edit(...)

@006-uuuu
Copy link

from discord import Webhook, RequestsWebhookAdapter ImportError: cannot import name 'RequestsWebhookAdapter' from 'discord' why

go to cmd, type 'pip install discord.py==1.7.3'

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