Webhook Support#

Pycord offers support for creating, editing, and executing webhooks through the Webhook class.

class discord.Webhook(data, session, proxy=None, proxy_auth=None, token=None, state=None)[source]#

Represents an asynchronous Discord webhook.

Webhooks are a form to send messages to channels in Discord without a bot user or authentication.

There are two main ways to use Webhooks. The first is through the ones received by the library such as Guild.webhooks() and TextChannel.webhooks(). The ones received by the library will automatically be bound using the library’s internal HTTP session.

The second form involves creating a webhook object manually using the from_url() or partial() classmethods.

For example, creating a webhook from a URL and using aiohttp:

from discord import Webhook
import aiohttp

async def foo():
    async with aiohttp.ClientSession() as session:
        webhook = Webhook.from_url('url-here', session=session)
        await webhook.send('Hello World', username='Foo')

For a synchronous counterpart, see SyncWebhook.

x == y

Checks if two webhooks are equal.

x != y

Checks if two webhooks are not equal.

hash(x)

Returns the webhook’s hash.

Changed in version 1.4: Webhooks are now comparable and hashable.

id#

The webhook’s ID

Type:

int

type#

The type of the webhook.

New in version 1.3.

Type:

WebhookType

token#

The authentication token of the webhook. If this is None then the webhook cannot be used to make requests.

Type:

Optional[str]

guild_id#

The guild ID this webhook is for.

Type:

Optional[int]

channel_id#

The channel ID this webhook is for.

Type:

Optional[int]

user#

The user this webhook was created by. If the webhook was received without authentication then this will be None.

Type:

Optional[abc.User]

name#

The default name of the webhook.

Type:

Optional[str]

source_guild#

The guild of the channel that this webhook is following. Only given if type is WebhookType.channel_follower.

New in version 2.0.

Type:

Optional[PartialWebhookGuild]

source_channel#

The channel that this webhook is following. Only given if type is WebhookType.channel_follower.

New in version 2.0.

Type:

Optional[PartialWebhookChannel]

Parameters:
property url#

Returns the webhook’s url.

Return type:

str

classmethod partial(id, token, *, session, proxy=None, proxy_auth=None, bot_token=None)[source]#

Creates a partial Webhook.

Parameters:
  • id (int) – The ID of the webhook.

  • token (str) – The authentication token of the webhook.

  • session (aiohttp.ClientSession) –

    The session to use to send requests with. Note that the library does not manage the session and will not close it.

    New in version 2.0.

  • bot_token (Optional[str]) –

    The bot authentication token for authenticated requests involving the webhook.

    New in version 2.0.

  • proxy (str | None) –

  • proxy_auth (aiohttp.BasicAuth | None) –

Returns:

A partial Webhook. A partial webhook is just a webhook object with an ID and a token.

Return type:

Webhook

classmethod from_url(url, *, session, proxy=None, proxy_auth=None, bot_token=None)[source]#

Creates a partial Webhook from a webhook URL.

Parameters:
  • url (str) – The URL of the webhook.

  • session (aiohttp.ClientSession) –

    The session to use to send requests with. Note that the library does not manage the session and will not close it.

    New in version 2.0.

  • bot_token (Optional[str]) –

    The bot authentication token for authenticated requests involving the webhook.

    New in version 2.0.

  • proxy (str | None) –

  • proxy_auth (aiohttp.BasicAuth | None) –

Returns:

A partial Webhook. A partial webhook is just a webhook object with an ID and a token.

Return type:

Webhook

Raises:

InvalidArgument – The URL is invalid.

await fetch(*, prefer_auth=True)[source]#

This function is a coroutine.

Fetches the current webhook.

This could be used to get a full webhook from a partial webhook.

New in version 2.0.

Note

When fetching with an unauthenticated webhook, i.e. is_authenticated() returns False, then the returned webhook does not contain any user information.

Parameters:

prefer_auth (bool) – Whether to use the bot token over the webhook token if available. Defaults to True.

Returns:

The fetched webhook.

Return type:

Webhook

Raises:
await delete(*, reason=None, prefer_auth=True)[source]#

This function is a coroutine.

Deletes this Webhook.

Parameters:
  • reason (Optional[str]) –

    The reason for deleting this webhook. Shows up on the audit log.

    New in version 1.4.

  • prefer_auth (bool) –

    Whether to use the bot token over the webhook token if available. Defaults to True.

    New in version 2.0.

Raises:
  • HTTPException – Deleting the webhook failed.

  • NotFound – This webhook does not exist.

  • Forbidden – You do not have permissions to delete this webhook.

  • InvalidArgument – This webhook does not have a token associated with it.

await edit(*, reason=None, name=..., avatar=..., channel=None, prefer_auth=True)[source]#

This function is a coroutine.

Edits this Webhook.

Parameters:
  • name (Optional[str]) – The webhook’s new default name.

  • avatar (Optional[bytes]) – A bytes-like object representing the webhook’s new default avatar.

  • channel (Optional[abc.Snowflake]) –

    The webhook’s new channel. This requires an authenticated webhook.

    New in version 2.0.

  • reason (Optional[str]) –

    The reason for editing this webhook. Shows up on the audit log.

    New in version 1.4.

  • prefer_auth (bool) –

    Whether to use the bot token over the webhook token if available. Defaults to True.

    New in version 2.0.

Raises:
  • HTTPException – Editing the webhook failed.

  • NotFound – This webhook does not exist.

  • InvalidArgument – This webhook does not have a token associated with it, or it tried editing a channel without authentication.

Return type:

Webhook

await send(content=..., *, username=..., avatar_url=..., tts=False, ephemeral=False, file=..., files=..., embed=..., embeds=..., allowed_mentions=..., view=..., thread=..., thread_name=None, wait=False, delete_after=None)[source]#

This function is a coroutine.

Sends a message using the webhook.

The content must be a type that can convert to a string through str(content).

To upload a single file, the file parameter should be used with a single File object.

If the embed parameter is provided, it must be of type Embed and it must be a rich embed type. You cannot mix the embed parameter with the embeds parameter, which must be a list of Embed objects to send.

Parameters:
  • content (str) – The content of the message to send.

  • wait (bool) – Whether the server should wait before sending a response. This essentially means that the return type of this function changes from None to a WebhookMessage if set to True. If the type of webhook is WebhookType.application then this is always set to True.

  • username (str) – The username to send with this message. If no username is provided then the default username for the webhook is used.

  • avatar_url (str) – The avatar URL to send with this message. If no avatar URL is provided then the default avatar for the webhook is used. If this is not a string then it is explicitly cast using str.

  • tts (bool) – Indicates if the message should be sent using text-to-speech.

  • ephemeral (bool) –

    Indicates if the message should only be visible to the user. This is only available to WebhookType.application webhooks. If a view is sent with an ephemeral message, and it has no timeout set then the timeout is set to 15 minutes.

    New in version 2.0.

  • file (File) – The file to upload. This cannot be mixed with files parameter.

  • files (List[File]) – A list of files to send with the content. This cannot be mixed with the file parameter.

  • embed (Embed) – The rich embed for the content to send. This cannot be mixed with embeds parameter.

  • embeds (List[Embed]) – A list of embeds to send with the content. Maximum of 10. This cannot be mixed with the embed parameter.

  • allowed_mentions (AllowedMentions) –

    Controls the mentions being processed in this message.

    New in version 1.4.

  • view (discord.ui.View) –

    The view to send with the message. You can only send a view if this webhook is not partial and has state attached. A webhook has state attached if the webhook is managed by the library.

    New in version 2.0.

  • thread (Snowflake) –

    The thread to send this webhook to.

    New in version 2.0.

  • thread_name (str) –

    The name of the thread to create. Only works for forum channels.

    New in version 2.0.

  • delete_after (float) – If provided, the number of seconds to wait in the background before deleting the message we just sent.

Returns:

If wait is True then the message that was sent, otherwise None.

Return type:

Optional[WebhookMessage]

Raises:
  • HTTPException – Sending the message failed.

  • NotFound – This webhook was not found.

  • Forbidden – The authorization token for the webhook is incorrect.

  • TypeError – You specified both embed and embeds or file and files.

  • ValueError – The length of embeds was invalid.

  • InvalidArgument – Either there was no token associated with this webhook, ephemeral was passed with the improper webhook type, there was no state attached with this webhook when giving it a view, or you specified both thread_name and thread.

property avatar#

Returns an Asset for the avatar the webhook has.

If the webhook does not have a traditional avatar, an asset for the default avatar is returned instead.

Return type:

Asset

property channel#

The text channel this webhook belongs to.

If this is a partial webhook, then this will always return None.

Return type:

TextChannel | None

property created_at#

Returns the webhook’s creation time in UTC.

Return type:

datetime

await fetch_message(id, *, thread_id=None)[source]#

This function is a coroutine.

Retrieves a single WebhookMessage owned by this webhook.

New in version 2.0.

Parameters:
  • id (int) – The message ID to look for.

  • thread_id (Optional[int]) – The ID of the thread that contains the message.

Returns:

The message asked for.

Return type:

WebhookMessage

Raises:
  • NotFound – The specified message was not found.

  • Forbidden – You do not have the permissions required to get a message.

  • HTTPException – Retrieving the message failed.

  • InvalidArgument – There was no token associated with this webhook.

property guild#

The guild this webhook belongs to.

If this is a partial webhook, then this will always return None.

Return type:

Guild | None

is_authenticated()#

Whether the webhook is authenticated with a bot token.

New in version 2.0.

Return type:

bool

is_partial()#

Whether the webhook is a “partial” webhook.

New in version 2.0.

Return type:

bool

await edit_message(message_id, *, content=..., embeds=..., embed=..., file=..., files=..., attachments=..., view=..., allowed_mentions=None, thread=...)[source]#

This function is a coroutine.

Edits a message owned by this webhook.

This is a lower level interface to WebhookMessage.edit() in case you only have an ID.

New in version 1.6.

Changed in version 2.0: The edit is no longer in-place, instead the newly edited message is returned.

Parameters:
  • message_id (int) – The message ID to edit.

  • content (Optional[str]) – The content to edit the message with or None to clear it.

  • embeds (List[Embed]) – A list of embeds to edit the message with.

  • embed (Optional[Embed]) – The embed to edit the message with. None suppresses the embeds. This should not be mixed with the embeds parameter.

  • attachments (List[Attachment]) – A list of attachments to keep in the message. If [] is passed then all attachments are removed.

  • file (File) –

    The file to upload. This cannot be mixed with files parameter.

    New in version 2.0.

  • files (List[File]) –

    A list of files to send with the content. This cannot be mixed with the file parameter.

    New in version 2.0.

  • allowed_mentions (AllowedMentions) – Controls the mentions being processed in this message. See abc.Messageable.send() for more information.

  • view (Optional[View]) –

    The updated view to update this message with. If None is passed then the view is removed. The webhook must have state attached, similar to send().

    New in version 2.0.

  • thread (Optional[Snowflake]) – The thread that contains the message.

Returns:

The newly edited webhook message.

Return type:

WebhookMessage

Raises:
  • HTTPException – Editing the message failed.

  • Forbidden – Edited a message that is not yours.

  • TypeError – You specified both embed and embeds or file and files

  • ValueError – The length of embeds was invalid

  • InvalidArgument – There was no token associated with this webhook or the webhook had no state.

await delete_message(message_id, *, thread_id=None)[source]#

This function is a coroutine.

Deletes a message owned by this webhook.

This is a lower level interface to WebhookMessage.delete() in case you only have an ID.

New in version 1.6.

Parameters:
  • message_id (int) – The message ID to delete.

  • thread_id (Optional[int]) – The ID of the thread that contains the message.

Raises:
Return type:

None

Methods
class discord.WebhookMessage(*, state, channel, data)[source]#

Represents a message sent from your webhook.

This allows you to edit or delete a message sent by your webhook.

This inherits from discord.Message with changes to edit() and delete() to work.

New in version 1.6.

Parameters:
await edit(content=..., embeds=..., embed=..., file=..., files=..., attachments=..., view=..., allowed_mentions=None)[source]#

This function is a coroutine.

Edits the message.

New in version 1.6.

Changed in version 2.0: The edit is no longer in-place, instead the newly edited message is returned.

Parameters:
  • content (Optional[str]) – The content to edit the message with or None to clear it.

  • embeds (List[Embed]) – A list of embeds to edit the message with.

  • embed (Optional[Embed]) – The embed to edit the message with. None suppresses the embeds. This should not be mixed with the embeds parameter.

  • file (File) –

    The file to upload. This cannot be mixed with files parameter.

    New in version 2.0.

  • files (List[File]) –

    A list of files to send with the content. This cannot be mixed with the file parameter.

    New in version 2.0.

  • attachments (List[Attachment]) –

    A list of attachments to keep in the message. If [] is passed then all attachments are removed.

    New in version 2.0.

  • allowed_mentions (AllowedMentions) – Controls the mentions being processed in this message. See abc.Messageable.send() for more information.

  • view (Optional[View]) –

    The updated view to update this message with. If None is passed then the view is removed.

    New in version 2.0.

Returns:

The newly edited message.

Return type:

WebhookMessage

Raises:
  • HTTPException – Editing the message failed.

  • Forbidden – Edited a message that is not yours.

  • TypeError – You specified both embed and embeds or file and files

  • ValueError – The length of embeds was invalid

  • InvalidArgument – There was no token associated with this webhook.

await delete(*, delay=None)[source]#

This function is a coroutine.

Deletes the message.

Parameters:

delay (Optional[float]) – If provided, the number of seconds to wait before deleting the message. The waiting is done in the background and deletion failures are ignored.

Raises:
  • Forbidden – You do not have proper permissions to delete the message.

  • NotFound – The message was deleted already.

  • HTTPException – Deleting the message failed.

Return type:

None

class discord.SyncWebhook(data, session, token=None, state=None)[source]#

Represents a synchronous Discord webhook.

For an asynchronous counterpart, see Webhook.

x == y

Checks if two webhooks are equal.

x != y

Checks if two webhooks are not equal.

hash(x)

Returns the webhook’s hash.

Changed in version 1.4: Webhooks are now comparable and hashable.

id#

The webhook’s ID

Type:

int

type#

The type of the webhook.

New in version 1.3.

Type:

WebhookType

token#

The authentication token of the webhook. If this is None then the webhook cannot be used to make requests.

Type:

Optional[str]

guild_id#

The guild ID this webhook is for.

Type:

Optional[int]

channel_id#

The channel ID this webhook is for.

Type:

Optional[int]

user#

The user this webhook was created by. If the webhook was received without authentication then this will be None.

Type:

Optional[abc.User]

name#

The default name of the webhook.

Type:

Optional[str]

source_guild#

The guild of the channel that this webhook is following. Only given if type is WebhookType.channel_follower.

New in version 2.0.

Type:

Optional[PartialWebhookGuild]

source_channel#

The channel that this webhook is following. Only given if type is WebhookType.channel_follower.

New in version 2.0.

Type:

Optional[PartialWebhookChannel]

Parameters:
  • data (WebhookPayload) –

  • session (Session) –

  • token (str | None) –

property url#

Returns the webhook’s url.

Return type:

str

classmethod partial(id, token, *, session=..., bot_token=None)[source]#

Creates a partial Webhook.

Parameters:
  • id (int) – The ID of the webhook.

  • token (str) – The authentication token of the webhook.

  • session (requests.Session) – The session to use to send requests with. Note that the library does not manage the session and will not close it. If not given, the requests auto session creation functions are used instead.

  • bot_token (Optional[str]) – The bot authentication token for authenticated requests involving the webhook.

Returns:

A partial Webhook. A partial webhook is just a webhook object with an ID and a token.

Return type:

Webhook

classmethod from_url(url, *, session=..., bot_token=None)[source]#

Creates a partial Webhook from a webhook URL.

Parameters:
  • url (str) – The URL of the webhook.

  • session (requests.Session) – The session to use to send requests with. Note that the library does not manage the session and will not close it. If not given, the requests auto session creation functions are used instead.

  • bot_token (Optional[str]) – The bot authentication token for authenticated requests involving the webhook.

Returns:

A partial Webhook. A partial webhook is just a webhook object with an ID and a token.

Return type:

Webhook

Raises:

InvalidArgument – The URL is invalid.

fetch(*, prefer_auth=True)[source]#

Fetches the current webhook.

This could be used to get a full webhook from a partial webhook.

Note

When fetching with an unauthenticated webhook, i.e. is_authenticated() returns False, then the returned webhook does not contain any user information.

Parameters:

prefer_auth (bool) – Whether to use the bot token over the webhook token if available. Defaults to True.

Returns:

The fetched webhook.

Return type:

SyncWebhook

Raises:
delete(*, reason=None, prefer_auth=True)[source]#

Deletes this Webhook.

Parameters:
  • reason (Optional[str]) –

    The reason for deleting this webhook. Shows up on the audit log.

    New in version 1.4.

  • prefer_auth (bool) – Whether to use the bot token over the webhook token if available. Defaults to True.

Raises:
  • HTTPException – Deleting the webhook failed.

  • NotFound – This webhook does not exist.

  • Forbidden – You do not have permissions to delete this webhook.

  • InvalidArgument – This webhook does not have a token associated with it.

Return type:

None

edit(*, reason=None, name=..., avatar=..., channel=None, prefer_auth=True)[source]#

Edits this Webhook.

Parameters:
  • name (Optional[str]) – The webhook’s new default name.

  • avatar (Optional[bytes]) – A bytes-like object representing the webhook’s new default avatar.

  • channel (Optional[abc.Snowflake]) – The webhook’s new channel. This requires an authenticated webhook.

  • reason (Optional[str]) –

    The reason for editing this webhook. Shows up on the audit log.

    New in version 1.4.

  • prefer_auth (bool) – Whether to use the bot token over the webhook token if available. Defaults to True.

Returns:

The newly edited webhook.

Return type:

SyncWebhook

Raises:
  • HTTPException – Editing the webhook failed.

  • NotFound – This webhook does not exist.

  • InvalidArgument – This webhook does not have a token associated with it, or it tried editing a channel without authentication.

send(content=..., *, username=..., avatar_url=..., tts=False, file=..., files=..., embed=..., embeds=..., allowed_mentions=..., thread=..., thread_name=None, wait=False)[source]#

Sends a message using the webhook.

The content must be a type that can convert to a string through str(content).

To upload a single file, the file parameter should be used with a single File object.

If the embed parameter is provided, it must be of type Embed and it must be a rich embed type. You cannot mix the embed parameter with the embeds parameter, which must be a list of Embed objects to send.

Parameters:
  • content (str) – The content of the message to send.

  • wait (bool) – Whether the server should wait before sending a response. This essentially means that the return type of this function changes from None to a WebhookMessage if set to True.

  • username (str) – The username to send with this message. If no username is provided then the default username for the webhook is used.

  • avatar_url (str) – The avatar URL to send with this message. If no avatar URL is provided then the default avatar for the webhook is used. If this is not a string then it is explicitly cast using str.

  • tts (bool) – Indicates if the message should be sent using text-to-speech.

  • file (File) – The file to upload. This cannot be mixed with files parameter.

  • files (List[File]) – A list of files to send with the content. This cannot be mixed with the file parameter.

  • embed (Embed) – The rich embed for the content to send. This cannot be mixed with embeds parameter.

  • embeds (List[Embed]) – A list of embeds to send with the content. Maximum of 10. This cannot be mixed with the embed parameter.

  • allowed_mentions (AllowedMentions) –

    Controls the mentions being processed in this message.

    New in version 1.4.

  • thread (Snowflake) –

    The thread to send this message to.

    New in version 2.0.

  • thread_name (str) –

    The name of the thread to create. Only works for forum channels.

    New in version 2.0.

Returns:

If wait is True then the message that was sent, otherwise None.

Return type:

Optional[SyncWebhookMessage]

Raises:
  • HTTPException – Sending the message failed.

  • NotFound – This webhook was not found.

  • Forbidden – The authorization token for the webhook is incorrect.

  • TypeError – You specified both embed and embeds or file and files

  • ValueError – The length of embeds was invalid

  • InvalidArgument – There was no token associated with this webhook, or you specified both a thread to send to and a thread to create (the thread and thread_name parameters).

fetch_message(id, *, thread_id=None)[source]#

Retrieves a single SyncWebhookMessage owned by this webhook.

New in version 2.0.

Parameters:
  • id (int) – The message ID to look for.

  • thread_id (Optional[int]) – The ID of the thread that contains the message.

Returns:

The message asked for.

Return type:

SyncWebhookMessage

Raises:
  • NotFound – The specified message was not found.

  • Forbidden – You do not have the permissions required to get a message.

  • HTTPException – Retrieving the message failed.

  • InvalidArgument – There was no token associated with this webhook.

property avatar#

Returns an Asset for the avatar the webhook has.

If the webhook does not have a traditional avatar, an asset for the default avatar is returned instead.

Return type:

Asset

property channel#

The text channel this webhook belongs to.

If this is a partial webhook, then this will always return None.

Return type:

TextChannel | None

property created_at#

Returns the webhook’s creation time in UTC.

Return type:

datetime

edit_message(message_id, *, content=..., embeds=..., embed=..., file=..., files=..., allowed_mentions=None, thread=...)[source]#

Edits a message owned by this webhook.

This is a lower level interface to WebhookMessage.edit() in case you only have an ID.

New in version 1.6.

Parameters:
  • message_id (int) – The message ID to edit.

  • content (Optional[str]) – The content to edit the message with or None to clear it.

  • embeds (List[Embed]) – A list of embeds to edit the message with.

  • embed (Optional[Embed]) – The embed to edit the message with. None suppresses the embeds. This should not be mixed with the embeds parameter.

  • file (File) – The file to upload. This cannot be mixed with files parameter.

  • files (List[File]) – A list of files to send with the content. This cannot be mixed with the file parameter.

  • allowed_mentions (AllowedMentions) – Controls the mentions being processed in this message. See abc.Messageable.send() for more information.

  • thread (Optional[Snowflake]) – The thread that contains the message.

Raises:
  • HTTPException – Editing the message failed.

  • Forbidden – Edited a message that is not yours.

  • TypeError – You specified both embed and embeds or file and files

  • ValueError – The length of embeds was invalid

  • InvalidArgument – There was no token associated with this webhook.

Return type:

SyncWebhookMessage

property guild#

The guild this webhook belongs to.

If this is a partial webhook, then this will always return None.

Return type:

Guild | None

is_authenticated()#

Whether the webhook is authenticated with a bot token.

New in version 2.0.

Return type:

bool

is_partial()#

Whether the webhook is a “partial” webhook.

New in version 2.0.

Return type:

bool

delete_message(message_id, *, thread_id=None)[source]#

Deletes a message owned by this webhook.

This is a lower level interface to WebhookMessage.delete() in case you only have an ID.

New in version 1.6.

Parameters:
  • message_id (int) – The message ID to delete.

  • thread_id (Optional[int]) – The ID of the thread that contains the message.

Raises:
Return type:

None

Methods
class discord.SyncWebhookMessage(*, state, channel, data)[source]#

Represents a message sent from your webhook.

This allows you to edit or delete a message sent by your webhook.

This inherits from discord.Message with changes to edit() and delete() to work.

New in version 2.0.

Parameters:
edit(content=..., embeds=..., embed=..., file=..., files=..., allowed_mentions=None)[source]#

Edits the message.

Parameters:
  • content (Optional[str]) – The content to edit the message with or None to clear it.

  • embeds (List[Embed]) – A list of embeds to edit the message with.

  • embed (Optional[Embed]) – The embed to edit the message with. None suppresses the embeds. This should not be mixed with the embeds parameter.

  • file (File) – The file to upload. This cannot be mixed with files parameter.

  • files (List[File]) – A list of files to send with the content. This cannot be mixed with the file parameter.

  • allowed_mentions (AllowedMentions) – Controls the mentions being processed in this message. See abc.Messageable.send() for more information.

Returns:

The newly edited message.

Return type:

SyncWebhookMessage

Raises:
  • HTTPException – Editing the message failed.

  • Forbidden – Edited a message that is not yours.

  • TypeError – You specified both embed and embeds or file and files

  • ValueError – The length of embeds was invalid

  • InvalidArgument – There was no token associated with this webhook.

delete(*, delay=None)[source]#

Deletes the message.

Parameters:

delay (Optional[float]) – If provided, the number of seconds to wait before deleting the message. This blocks the thread.

Raises:
  • Forbidden – You do not have proper permissions to delete the message.

  • NotFound – The message was deleted already.

  • HTTPException – Deleting the message failed.

Return type:

None