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.
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.
- 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 you are making a normal bot and don't have to do something frequently.
To create a webhook in your server, Go to server settings > Integrations Tab > Webhooks > New 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!
Discord.py provides a great way of sending webhook messages.
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.
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.
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.
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.
I hope this guide helped you to understand webhooks. If it did you can help me by starring it. Thanks 😁
so, is there any replacement of that module in pycord? (pycord is the replacement of discord.py)