Client¶
discord_http.client module¶
- class discord_http.client.Client(*, token, application_id=None, public_key=None, guild_id=None, sync=False, api_version=10, loop=None, allowed_mentions=None, enable_gateway=False, automatic_shards=True, playing_status=None, chunk_guilds_on_startup=False, guild_ready_timeout=2.0, gateway_cache=None, intents=None, logging_level=20, call_after_delay=0.1, disable_default_get_path=False, debug_events=False)¶
Bases:
object
- Parameters:
token (str)
application_id (int | None)
public_key (str | None)
guild_id (int | None)
sync (bool)
api_version (int)
loop (AbstractEventLoop | None)
allowed_mentions (AllowedMentions | None)
enable_gateway (bool)
automatic_shards (bool)
playing_status (PlayingStatus | None)
chunk_guilds_on_startup (bool)
guild_ready_timeout (float)
gateway_cache (GatewayCacheFlags | None)
intents (Intents | None)
logging_level (int)
disable_default_get_path (bool)
debug_events (bool)
- add_interaction(func) Interaction ¶
Adds an interaction to the bot.
- Parameters:
func (
Interaction
) – The interaction to add to the bot.- 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:
- 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:
- 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:
- 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
- async fetch_automod_rule(rule_id, guild_id) AutoModRule ¶
Fetches a automod object.
- Parameters:
- Return type:
- Returns:
The automod object
- async fetch_channel(channel_id, *, guild_id=None) BaseChannel ¶
Fetches a channel object.
- Parameters:
- Return type:
- Returns:
The channel 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.
- 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.
- async fetch_scheduled_event(event_id, guild_id) ScheduledEvent ¶
Fetches a scheduled event object.
- Parameters:
- Return type:
- Returns:
The scheduled event object.
- async fetch_soundboard_sound(sound_id, guild_id) SoundboardSound ¶
Fetches a soundboard sound object.
- Parameters:
- Return type:
- Returns:
The soundboard sound object.
- async fetch_voice_state(member_id, guild_id=None) VoiceState ¶
Fetches a voice state object.
- Parameters:
- Return type:
- Returns:
The voice state 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.
- get_channel(channel_id) 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_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.
- get_partial_automod_rule(rule_id, guild_id) PartialAutoModRule ¶
Creates a partial automod object.
- Parameters:
- Return type:
- Returns:
The partial automod object
- get_partial_channel(channel_id, *, guild_id=None) PartialChannel ¶
Creates a partial channel object.
- Parameters:
- Return type:
- Returns:
The partial channel object.
- get_partial_emoji(emoji_id, *, guild_id=None) PartialEmoji ¶
Creates a partial emoji object.
- Parameters:
- Return type:
- Returns:
The partial emoji object.
- 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.
- 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.
- 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_member(user_id, guild_id) PartialMember ¶
Creates a partial member object.
- Parameters:
- Return type:
- Returns:
The partial member 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.
- get_partial_role(role_id, guild_id) PartialRole ¶
Creates a partial role object.
- Parameters:
- Return type:
- Returns:
The partial role object.
- get_partial_scheduled_event(event_id, guild_id) PartialScheduledEvent ¶
Creates a partial scheduled event object.
- Parameters:
- Return type:
- Returns:
The partial scheduled event object.
- get_partial_sku(sku_id) PartialSKU ¶
Creates a partial SKU object.
- Return type:
- Returns:
The partial SKU object.
- Parameters:
sku_id (int)
- get_partial_soundboard_sound(sound_id, *, guild_id=None) PartialSoundboardSound ¶
Creates a partial sticker object.
- Parameters:
- Return type:
- Returns:
The partial soundboard sound object.
- get_partial_sticker(sticker_id, *, guild_id=None) PartialSticker ¶
Creates a partial sticker object.
- Parameters:
- Return type:
- Returns:
The partial sticker 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_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.
- get_partial_webhook(webhook_id, *, webhook_token=None) PartialWebhook ¶
Creates a partial webhook object.
- Parameters:
- Return type:
- Returns:
The partial webhook object.
- 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
- property guilds: list[Guild | PartialGuild]¶
Returns a list of all the guilds the bot is in.
Only useable if you are using gateway and caching
- 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
- 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:
- 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 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 inlimit (
int
) – The maximum amount of members to returnpresences (
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
- remove_interaction(func) None ¶
Removes an interaction from the bot.
- Parameters:
func (
Interaction
) – The interaction to remove from the bot.- Return type:
- 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)
- 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)
- 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:
- async sync_commands() None ¶
Make the bot fetch all current commands, to then sync them all to Discord API.
- Return type:
- property user: UserClient¶
Returns the bot’s user object.
- Return type:
The bot’s user object
- Raises:
AttributeError – If used before the bot is ready
- 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:
- 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:
- Raises:
TimeoutError – If the event was not dispatched within the timeout
- Return type:
- Returns:
The event parameters