Client¶
discord_http.client module¶
- class discord_http.client.Client(*, token, guild_id=None, sync=True, api_version=10, api_base_url=None, loop=None, allowed_mentions=None, enable_gateway=False, automatic_shards=True, max_pending_connections=128, playing_status=None, chunk_guilds_on_startup=False, guild_ready_timeout=2.0, gateway_cache=None, interaction_path=None, webhook_events_path=None, intents=None, gateway_capabilities=None, logging_level=20, disable_default_get_path=False, debug_events=False)¶
Bases:
objectThe main client class for discord.http.
- Parameters:
token (
str) – Discord bot tokenguild_id (
int|None) – Guild ID to sync commands to, if not provided, it will sync to globalsync (
bool) – Whether to sync commands on boot or not (defaults to True)max_pending_connections (
int) – The maximum number of queued connections passed to the interaction URL, by default 128. If your bot is receiving a lot of traffic, you might want to increase this value.api_version (
int) – API version to use for both HTTP and WS, if not provided, it will use the default (10)api_base_url (
str|None) – Base URL to use for the API, if not provided, it will use the default (https://discord.com/api)loop (
AbstractEventLoop|None) – Event loop to use, if not provided, it will use asyncio.get_running_loop()allowed_mentions (
AllowedMentions|None) – Allowed mentions to use, if not provided, it will use AllowedMentions.all()enable_gateway (
bool) – Whether to enable the gateway or not, which runs in the backgroundautomatic_shards (
bool) – Whether to automatically shard the bot or notplaying_status (
PlayingStatus|None) – The playing status to use, if not provided, it will use None. This is only used if enable_gateway is True.chunk_guilds_on_startup (
bool) – Whether to chunk guilds or not when booting, which will reduce the amount of requestsguild_ready_timeout (
float) – Gateway: How long to wait for last GUILD_CREATE to be recieved before triggering shard readygateway_cache (
GatewayCacheFlags|None) – How the gateway should cache, only used if enable_gateway is True. Leave empty to use no cache.intents (
Intents|None) – Intents to use, only used if enable_gateway is Truegateway_capabilities (
GatewayCapabilities|None) – Opt-in Gateway capabilities bitfield to send in the Identify payload, only used if enable_gateway is True. Currently only used to test upcoming, opt-in Gateway behavior.logging_level (
int) – Logging level to use, if not provided, it will use logging.INFOdebug_events (
bool) – Whether to log events or not, if not provided, on_raw_* events will not be useableinteraction_path (
str|None) – Path to the interaction endpoint, if not provided, it will use the default path which is just / (aka. root).webhook_events_path (
str|None) – Path to the webhook events endpoint (configured separately from the interaction endpoint in your app’s settings), if not provided, webhook events will not be served.disable_default_get_path (
bool) – Whether to disable the default GET path or not, if not provided, it will use False. The default GET path only provides information about the bot and when it was last rebooted. Usually a great tool to just validate that your bot is online.
- application: Application | None¶
The application object for the bot.
- gateway: GatewayClient | None¶
The gateway client, if enabled.
- interactions: dict[str, Interaction]¶
The interactions registered to the client.
- interactions_regex: dict[str, Interaction]¶
The interactions registered to the client with regex.
- cache: Cache¶
The cache for the client, used for caching guilds, users, etc.
- state: DiscordAPI¶
The state for the client, used for making HTTP requests.
- backend: DiscordHTTP¶
The backend for the client, used for serving HTTP requests.
- get_shard_by_guild_id(guild_id) int | None¶
Returns the shard ID of the shard that the guild is in.
- Parameters:
guild_id (
Snowflake|int) – The ID of the guild to get the shard ID of- Return type:
- Returns:
The shard ID of the guild, or None if not found
- Raises:
NotImplementedError – If the gateway is not available
- async query_members(guild_id, *, query=None, limit=0, presences=False, user_ids=None, shard_id=None) list[Member]¶
Query members in a guild.
- Parameters:
guild_id (
Snowflake|int) – The ID of the guild to query members inquery (
str|None) – The query to search for (defaults to an empty string, which will return all members)limit (
int) – The maximum amount of members to return (defaults to 0, which will return all members)presences (
bool) – Whether to include presences in the responseuser_ids (
list[Snowflake|int] |None) – The user IDs to fetch members forshard_id (
int|None) – The shard ID to query the members from
- Return type:
- Returns:
The members that matched the query
- Raises:
If shard_id is not provided - If shard_id is not valid
- async sync_commands() None¶
Make the bot fetch all current commands, to then sync them all to Discord API.
- Return type:
- property user: User¶
The bot’s user object.
- Raises:
If used before the bot is ready - If the application does not have a bot user associated with it
- property guilds: list[Guild | PartialGuild]¶
A list of all the guilds the bot is in.
Only useable if you are using gateway and caching
- get_guild(guild_id) Guild | PartialGuild | None¶
Get a guild object from the cache.
- Parameters:
guild_id (
int) – The ID of the guild to get.- Return type:
Guild|PartialGuild|None- Returns:
The guild object with the specified ID, or None if not found.
- set_context(*, cls=None) None¶
Get the context for a command, while allowing custom context as well.
Example of making one:
from discord_http import Context class CustomContext(Context): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) Client.set_context(cls=CustomContext)
- set_backend(*, cls=None) None¶
Set the backend to use for the bot.
Example of making one:
from discord_http import DiscordHTTP class CustomBackend(DiscordHTTP): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) Client.set_backend(cls=CustomBackend)
- async setup_hook() None¶
Runs before the bot is ready, to get variables set up.
You can overwrite this function to do your own setup
Example:
async def setup_hook(self) -> None: # Making database connection available through the bot self.pool = SQLite.Database()
- Return type:
- offline_run(func) None¶
Used to run Discord API instructions without booting the bot.
The function will be ran only once and then the script ends. Useful for when you just want to run simple things without needing to run a full bot.
- async wait_until_ready() None¶
Waits until the client is ready using asyncio.Event.wait().
- Return type:
- async wait_until_shards_ready() None¶
Waits until the client is ready using asyncio.Event.wait().
- Return type:
- async reload_extension(package) None¶
Reloads an extension.
- Parameters:
package (
str) – The package to reload the extension from.- Raises:
If the extension failed to reload, but the previous state was restored successfully - If the extension failed to reload, and the previous state failed to be restored
- Return type:
- async wait_for(event_name, *, check=None, timeout=None) Coroutine[Any, Any, Any]¶
Waits for an event to be dispatched.
This requires gateway to be enabled. Otherwise it will log a warning and return None.
- Parameters:
- Return type:
- Returns:
The event parameters
- Raises:
TimeoutError – If the event was not dispatched within the timeout
- command(name=None, *, description=None, guild_ids=None, guild_install=True, user_install=False) Callable¶
Used to register a command.
- Parameters:
name (
str|None) – Name of the command, if not provided, it will use the function namedescription (
str|None) – Description of the command, if not provided, it will use the function docstringguild_ids (
list[Snowflake|int] |None) – List of guild IDs to register the command inuser_install (
bool) – Whether the command can be installed by users or notguild_install (
bool) – Whether the command can be installed by guilds or not
- Return type:
- user_command(name=None, *, guild_ids=None, guild_install=True, user_install=False) Callable¶
Used to register a user command.
Example usage
@user_command() async def content(ctx, user: Union[Member, User]): await ctx.send(f"Target: {user.name}")
- Parameters:
name (
str|None) – Name of the command, if not provided, it will use the function nameguild_ids (
list[Snowflake|int] |None) – List of guild IDs to register the command inuser_install (
bool) – Whether the command can be installed by users or notguild_install (
bool) – Whether the command can be installed by guilds or not
- Return type:
- message_command(name=None, *, guild_ids=None, guild_install=True, user_install=False) Callable¶
Used to register a message command.
Example usage
@message_command() async def content(ctx, msg: Message): await ctx.send(f"Content: {msg.content}")
- Parameters:
name (
str|None) – Name of the command, if not provided, it will use the function nameguild_ids (
list[Snowflake|int] |None) – List of guild IDs to register the command inuser_install (
bool) – Whether the command can be installed by users or notguild_install (
bool) – Whether the command can be installed by guilds or not
- Return type:
- before_invoke() Callable¶
Decorator to register a function to be called before a command is ran globally.
If it returns anything other than True, the command will not be invoked. However if you raise a CheckFailure exception, it will fail and you can add a message to it.
- Parameters:
func (Callable) – The function to be called before the command is invoked.
- Return type:
- after_invoke() Callable¶
Decorator to register a function to be called after a command is ran globally.
This acts like before_invoke, however it only runs after the command has been invoked successfully.
- Parameters:
func (Callable) – The function to be called after the command is invoked.
- Return type:
- interaction(custom_id, *, regex=False) Callable¶
Used to register an interaction.
This does support regex, so you can use r”regex here” as the custom_id
- get_channel(channel_id, *, guild_id=None) BaseChannel | PartialChannel | None¶
Get a channel object from the cache.
- Parameters:
- Return type:
- Returns:
The channel object with the specified ID, or None if not found.
- get_partial_channel(channel_id, *, guild_id=None) PartialChannel¶
Creates a partial channel object.
- Parameters:
- Return type:
- Returns:
The partial channel object.
- create_public_thread_from_data(data) PublicThread¶
Creates a public thread object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the public thread object with.- Return type:
- Returns:
The public thread object.
- create_private_thread_from_data(data) PrivateThread¶
Creates a private thread object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the private thread object with.- Return type:
- Returns:
The private thread object.
- create_forum_thread_from_data(data) ForumThread¶
Creates a forum thread object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the forum thread object with.- Return type:
- Returns:
The forum thread object.
- async fetch_current_application() Application¶
Fetches and update cache of the current application object.
- Return type:
- create_application_from_data(data) Application¶
Creates an application object from raw API data.
- Parameters:
data (
dict) – The data to create the application object with.- Return type:
- Returns:
The application object.
- async fetch_voice_regions() list[VoiceRegion]¶
Fetches the list of voice regions available on Discord.
- Return type:
- async fetch_channel(channel_id, *, guild_id=None) BaseChannel¶
Fetches a channel object.
- Parameters:
- Return type:
- Returns:
The channel object.
- get_partial_automod_rule(rule_id, guild_id) PartialAutoModRule¶
Creates a partial automod object.
- Parameters:
- Return type:
- Returns:
The partial automod object
- async fetch_automod_rule(rule_id, guild_id) AutoModRule¶
Fetches a automod object.
- Parameters:
- Return type:
- Returns:
The automod object
- get_partial_invite(invite_code, *, channel_id=None, guild_id=None) PartialInvite¶
Creates a partial invite object.
- Parameters:
- Return type:
- Returns:
The partial invite object.
- get_partial_voice_state(member_id, *, guild_id=None, channel_id=None) PartialVoiceState¶
Creates a partial voice state object.
- Parameters:
- Return type:
- Returns:
The partial voice state object.
- async fetch_voice_state(member_id, guild_id=None) VoiceState¶
Fetches a voice state object.
- Parameters:
- Return type:
- Returns:
The voice state object.
- get_partial_emoji(emoji_id, *, guild_id=None) PartialEmoji¶
Creates a partial emoji object.
- Parameters:
- Return type:
- Returns:
The partial emoji object.
- create_emoji_from_data(data, *, guild=None) Emoji¶
Creates an emoji object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the emoji object with.guild (
PartialGuild|Guild|None) – The guild the emoji belongs to. If None, it’s an application-owned emoji.
- Return type:
- Returns:
The emoji object.
- get_partial_sticker(sticker_id, *, guild_id=None) PartialSticker¶
Creates a partial sticker object.
- Parameters:
- Return type:
- Returns:
The partial sticker object.
- create_sticker_from_data(data, *, guild=None) Sticker¶
Creates a sticker object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the sticker object with.guild (
PartialGuild|None) – The guild the sticker belongs to, if any.
- Return type:
- Returns:
The sticker object.
- get_partial_soundboard_sound(sound_id, *, guild_id=None) PartialSoundboardSound¶
Creates a partial sticker object.
- Parameters:
- Return type:
- Returns:
The partial soundboard sound object.
- create_soundboard_sound_from_data(data, *, guild=None) SoundboardSound¶
Creates a soundboard sound object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the soundboard sound object with.guild (
PartialGuild|Guild|None) – The guild the soundboard sound belongs to, if any.
- Return type:
- Returns:
The soundboard sound object.
- async fetch_soundboard_sound(sound_id, guild_id) SoundboardSound¶
Fetches a soundboard sound object.
- Parameters:
- Return type:
- Returns:
The soundboard sound object.
- get_partial_message(message_id, channel_id, guild_id=None) PartialMessage¶
Creates a partial message object.
- Parameters:
- Return type:
- Returns:
The partial message object.
- create_message_from_data(data, *, guild=None) Message¶
Creates a message object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the message object with.guild (
PartialGuild|None) – The guild the message was sent in, if any.
- Return type:
- Returns:
The message object.
- get_partial_webhook(webhook_id, *, webhook_token=None) PartialWebhook¶
Creates a partial webhook object.
- Parameters:
- Return type:
- Returns:
The partial webhook object.
- create_webhook_message_from_data(data, *, application_id, token) WebhookMessage¶
Creates a webhook message object from raw API data.
- Parameters:
- Return type:
- Returns:
The webhook message object.
- get_partial_user(user_id) PartialUser¶
Creates a partial user object.
- Parameters:
user_id (
int) – User ID to create the partial user object with.- Return type:
- Returns:
The partial user object.
- get_partial_member(user_id, guild_id) PartialMember¶
Creates a partial member object.
- Parameters:
- Return type:
- Returns:
The partial member object.
- create_member_from_data(data, *, guild) Member¶
Creates a member object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the member object with.guild (
Guild|PartialGuild) – The guild the member is in.
- Return type:
- Returns:
The member object.
- create_thread_member_from_data(data, *, guild) ThreadMember¶
Creates a thread member object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the thread member object with.guild (
PartialGuild) – The guild the thread is in.
- Return type:
- Returns:
The thread member object.
- create_partial_thread_member_from_data(data, *, guild_id) PartialThreadMember¶
Creates a partial thread member object from raw API/gateway data.
- get_partial_sku(sku_id) PartialSKU¶
Creates a partial SKU object.
- Return type:
- Returns:
The partial SKU object.
- Parameters:
sku_id (int)
- get_partial_entitlement(entitlement_id) PartialEntitlements¶
Creates a partial entitlement object.
- Parameters:
entitlement_id (
int) – Entitlement ID to create the partial entitlement object with.- Return type:
- Returns:
The partial entitlement object.
- async fetch_entitlement(entitlement_id) Entitlements¶
Fetches an entitlement object.
- Parameters:
entitlement_id (
int) – Entitlement ID to fetch the entitlement object with.- Return type:
- Returns:
The entitlement object.
- create_entitlements_from_data(data) Entitlements¶
Creates an entitlement object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the entitlement object with.- Return type:
- Returns:
The entitlement object.
- create_integration_from_data(data, *, guild) Integration¶
Creates an integration object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the integration object with.guild (
Guild|PartialGuild) – The guild the integration belongs to.
- Return type:
- Returns:
The integration object.
- async fetch_entitlement_list(*, user_id=None, sku_ids=None, before=None, after=None, limit=100, guild_id=None, exclude_ended=False) AsyncIterator[Entitlements]¶
Fetches a list of entitlement objects with optional filters.
- Parameters:
user_id (
int|None) – Show entitlements for a specific user ID.sku_ids (
list[int] |None) – Show entitlements for a specific SKU ID.before (
int|None) – Only show entitlements before this entitlement ID.after (
int|None) – Only show entitlements after this entitlement ID.limit (
int|None) – Limit the amount of entitlements to fetch. Use None to fetch all entitlements.guild_id (
int|None) – Show entitlements for a specific guild ID.exclude_ended (
bool) – Whether to exclude ended entitlements or not.
- Return type:
- Returns:
The entitlement objects.
- get_partial_scheduled_event(event_id, guild_id) PartialScheduledEvent¶
Creates a partial scheduled event object.
- Parameters:
- Return type:
- Returns:
The partial scheduled event object.
- async fetch_scheduled_event(event_id, guild_id) ScheduledEvent¶
Fetches a scheduled event object.
- Parameters:
- Return type:
- Returns:
The scheduled event object.
- get_partial_guild(guild_id) PartialGuild¶
Creates a partial guild object.
- Parameters:
guild_id (
int) – Guild ID to create the partial guild object with.- Return type:
- Returns:
The partial guild object.
- create_guild_from_data(data, *, populate_cache=True) Guild¶
Creates a guild object from raw API/gateway data.
- create_audit_log_entry_from_data(data, *, guild=None, users=None) AuditLogEntry¶
Creates an audit log entry object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the audit log entry object with.guild (
PartialGuild|None) – The guild the audit log entry belongs to, if already known.users (
dict[int,User] |None) – A mapping of user IDs to already-resolved User objects, used to avoid re-resolving users referenced multiple times in the same audit log page.
- Return type:
- Returns:
The audit log entry object.
- async create_guild(name, *, icon=None, reason=None) Guild¶
Create a guild.
Note that the bot must be in less than 10 guilds to use this endpoint
- get_partial_role(role_id, guild_id) PartialRole¶
Creates a partial role object.
- Parameters:
- Return type:
- Returns:
The partial role object.
- create_role_from_data(data, *, guild) Role¶
Creates a role object from raw API/gateway data.
- Parameters:
data (
dict) – The data to create the role object with.guild (
Guild|PartialGuild) – The guild the role belongs to.
- Return type:
- Returns:
The role object.
- find_interaction(custom_id) Interaction | None¶
Finds an interaction by its Custom ID.
- Parameters:
custom_id (
str) – The Custom ID to find the interaction with. Will automatically convert to regex matching if some interaction Custom IDs are regex.- Return type:
- Returns:
The interaction that was found if any.
- add_interaction(func) Interaction¶
Adds an interaction to the bot.
- Parameters:
func (
Interaction) – The interaction to add to the bot.- Return type:
- remove_interaction(func) None¶
Removes an interaction from the bot.
- Parameters:
func (
Interaction) – The interaction to remove from the bot.- Return type: