Http

Warning

This module is not meant to be used directly, it is used internally by discord_http.Client.

discord_http.http module

class discord_http.http.DiscordAPI(*, client)

Bases: object

The main class for interacting with the Discord API.

Parameters:

client (Client)

bot: Client

A reference to the client’s cache for easy access within HTTP methods.

Type:

The client instance that owns this HTTP client. cache

token: str

The bot token used for authentication with the Discord API.

api_version: int

The version of the Discord API to use for requests.

base_url

The base URL for the Discord API.

api_url: str

//discord.com/api/v10”).

Type:

The full API URL including the version (e.g., “https

http: HTTPClient

The HTTP client used to make requests to the Discord API.

get_ratelimit(key) Ratelimit

Get a ratelimit object from the bucket.

Parameters:

key (str) – The key to get the ratelimit for

Return type:

Ratelimit

Returns:

The ratelimit object for the given key

create_jitter() float

Simply returns a random float between 0 and 1.

Return type:

float

async query(method, path, *, res_method='json', retry_codes=None, **kwargs) HTTPResponse

Make a request to the Discord API.

Parameters:
  • method (Literal['GET', 'POST', 'DELETE', 'PUT', 'HEAD', 'PATCH', 'OPTIONS']) – Which HTTP method to use

  • path (str) – The path to make the request to

  • res_method (Literal['text', 'read', 'json']) – The method to use to get the response

  • retry_codes (list[int] | None) – The HTTP codes to retry regardless of the response

  • **kwargs – The keyword arguments to pass to the aiohttp.ClientSession.request method

Return type:

HTTPResponse

Returns:

The response from the request

Raises:
async me() Application

Fetches the bot’s user information.

Return type:

Application

Returns:

The bot’s user object

Raises:

RuntimeError

  • If the bot token is not valid - If the bot is not allowed to use the some intents

async update_commands(data, guild_id=None) dict

Updates the commands for the bot.

Parameters:
  • data (list[dict] | dict) – The JSON data to send to Discord API

  • guild_id (int | None) – The guild ID to update the commands for (if None, commands will be global)

Return type:

dict

Returns:

The response from the request

async fetch_commands(guild_id=None) dict

Fetches the commands for the bot.

Parameters:

guild_id (int | None) – The guild ID to fetch the commands for (if None, commands will be global)

Return type:

dict

Returns:

The response from the request

Raises:

HTTPException – If the request returned anything other than 200. Typically this means the guild is not found.

class discord_http.http.HTTPResponse(*, status, response, reason, res_method, headers)

Bases: Generic[ResponseT]

Represents a response from the HTTP request.

Parameters:
  • status (int)

  • response (ResponseT)

  • reason (str | None)

  • res_method (Literal['text', 'read', 'json'])

  • headers (CIMultiDictProxy[str])

status

The HTTP status code of the response.

response

The response data, which can be of type str, bytes, or dict depending on the request.

res_method

The method used to retrieve the response data.

reason

The reason phrase returned by the server, if any.

headers

The headers of the response, as a CIMultiDictProxy.