API Reference¶
The following section outlines the API of dhooks.
Webhook¶
-
class
dhooks.
Webhook
(url: str = '', session: Union[aiohttp.client.ClientSession, requests.sessions.Session, None] = None, is_async: bool = False, **options)[source]¶ Class that represents a Discord webhook.
Parameters: - url (str, optional) –
The webhook URL that the client will send requests to.
Note: the URL should contain the
id
andtoken
of the webhook in the form of:https://discord.com/api/webhooks/webhooks/{id}/{token}
- session (requests.Session or aiohttp.ClientSession, optional) – The HTTP session that will be used to make requests to the API. If
session
is not provided, a newrequests.Session
oraiohttp.ClientSession
will be created, depending onis_async
. - is_async (bool, optional) – Defaults to
False
. Whether or not to the API methods in the class should be asynchronous. If set toTrue
, all methods will have the same interfaces, but returns a coroutine. - **id (int, optional) – The Discord ID of the webhook. If not provided, it will be extracted from the webhook URL.
- **token (str, optional) – The Discord token of the webhook. If not provided, it will be extracted from the webhook URL.
- **username (str, optional) – The username that will override the default name of the webhook every time you send a message.
- **avatar_url (str, optional) – The URL of the avatar that will override the default avatar of the webhook every time you send a message.
-
username
¶ The username that will override the default name of the webhook every time you send a message. If
username
is not provided, the default name is used.Type: str
-
avatar_url
¶ The avatar URL that will override the default avatar of the webhook every time you send a message. If
avatar_url
is not provided, the default avatar is used.Type: str
-
is_async
¶ Whether or not to the API methods in the class should be asynchronous. If set to
True
, all methods will have the same interfaces, but returns a coroutine.Type: bool
-
session
¶ The HTTP session that will be used to make requests to the API.
session
will be arequests.Session
oraiohttp.ClientSession
depending onis_async
.Type: requests.Session or aiohttp.ClientSession
-
default_name
¶ Warning
In order for some attributes to be filled,
get_info()
must be called prior.The default name of the webhook, this can be changed via
modify()
or directly through discord server settings.Type: str
-
default_avatar
¶ The avatar string of the webhook.
Type: str
-
classmethod
Async
(url: str = '', session: Optional[aiohttp.client.ClientSession] = None, **options) → dhooks.client.Webhook[source]¶ Returns a new instance of Webhook with
is_async
set toTrue
.Equivalent to:
Webhook(url, session=session, is_async=True, **options)
-
execute
(content: str = '', embed: Optional[dhooks.embed.Embed] = None, embeds: Optional[List[dhooks.embed.Embed]] = None, file: Optional[dhooks.file.File] = None, username: str = '', avatar_url: str = '', tts: bool = False) → dhooks.client.Webhook¶ Alias for
send()
.
-
get_info
() → dhooks.client.Webhook[source]¶ Updates
Webhook
with fresh data retrieved from discord.The following attributes are retrieved and updated:
-
modify
(name: str = '', avatar: bytes = b'') → dhooks.client.Webhook[source]¶ Edits the webhook.
Parameters:
-
send
(content: str = '', embed: Optional[dhooks.embed.Embed] = None, embeds: Optional[List[dhooks.embed.Embed]] = None, file: Optional[dhooks.file.File] = None, username: str = '', avatar_url: str = '', tts: bool = False) → dhooks.client.Webhook[source]¶ Sends a message to discord through the webhook.
Parameters: - content (str, optional) – The message contents (up to 2000 characters)
- embed (
Embed
, optional) – Single embedded rich content. - embeds (List[
Embed
], optional) – List of embedded rich content. - file (
File
, optional) – The file that will be uploaded. - tts (bool, optional) – Defaults to
False
. Whether or not the message will use text-to-speech. - username (str, optional) – Defaults to
username
. Override the default username of the webhook. - avatar_url (str, optional) – Defaults to
avatar_url
. Override the default avatar of the webhook.
- url (str, optional) –
File¶
-
class
dhooks.
File
(fp: Union[BinaryIO, str], name: str = '')[source]¶ Data class that represents a file that can be sent to discord.
Parameters: - fp (str or
io.BytesIO
) – A file path or a binary stream that is the file. If a file path is provided, this class will open and close the file for you. - name (str, optional) – The name of the file that discord will use, if not provided, defaults to the file name or the binary stream’s name.
- fp (str or
Embed¶
-
class
dhooks.
Embed
(**kwargs)[source]¶ Class that represents a discord embed.
Parameters: - **title (str, optional) – Defaults to
None
. The title of the embed. - **description (str, optional) – Defaults to
None
. The description of the embed. - **url (str, optional) – URL of the embed. It requires
title
to be set. - **timestamp (str, optional) –
ISO 8601
timestamp of the embed. If set to a “now”, the current time is set as the timestamp. - **color (int (or hex), optional) – Color of the embed.
- **image_url (str, optional) – URL of the image.
- **thumbnail_url (str, optional) – URL of the thumbnail.
-
add_field
(name: str, value: str, inline: bool = True) → None[source]¶ Adds an embed field.
Parameters:
-
del_field
(index: int) → None[source]¶ Deletes a field by index.
Parameters: index (int) – Index of the field to delete.
Sets the author of the embed.
Parameters:
Sets the footer of the embed.
Parameters:
-
set_image
(url: str) → None[source]¶ Sets the image of the embed.
Parameters: url (str) – URL of the image.
-
set_thumbnail
(url: str) → None[source]¶ Sets the thumbnail of the embed.
Parameters: url (str) – URL of the thumbnail.
-
set_timestamp
(time: Union[str, datetime.datetime] = None, now: bool = False) → None[source]¶ Sets the timestamp of the embed.
Parameters: - time (str or
datetime.datetime
) – TheISO 8601
timestamp from the embed. - now (bool) – Defaults to
False
. If set toTrue
the current time is used for the timestamp.
- time (str or
- **title (str, optional) – Defaults to