Backend

discord_http.backend module

class discord_http.backend.DiscordHTTP(*, client)

Bases: Application

Serves as the fundamental HTTP server for Discord Interactions.

We recommend to not touch this class, unless you know what you’re doing

Parameters:

client (Client)

bot: Client

The bot instance that is using this HTTP server.

debug_events: bool

Whether to dispatch debug events for interactions received, defaults to False.

host: str | None

The host the HTTP server listens on, None until it is started.

port: int | None

The port the HTTP server listens on, None until it is started.

interaction_path: str

The path interactions are received on, always starting with a slash.

verify_key: Ed25519PublicKey | None

The verify key used to validate incoming requests, if not set, it will try to fetch from the bot’s public key.

async handle_gateway_interaction(data, *, replayed=False) → None

Handles an interaction received over the gateway, used when the application has no interactions endpoint URL.

It runs the exact same way as an HTTP interaction, but the response is sent to Discord’s callback endpoint instead of being the HTTP response body.

Parameters:
  • data (dict) – The raw interaction payload from INTERACTION_CREATE

  • replayed (bool) – Whether the interaction was replayed by Discord after a resume, expired ones are skipped instead of running the command

Return type:

None

ATTRS = frozenset({'_cleanup_ctx', '_client_max_fields', '_client_max_size', '_debug', '_frozen', '_handler_args', '_has_legacy_middlewares', '_loop', '_middlewares', '_middlewares_handlers', '_on_cleanup', '_on_response_prepare', '_on_shutdown', '_on_startup', '_pre_frozen', '_router', '_run_middlewares', '_state', '_subapps', 'logger'})
add_domain(domain, subapp) → MatchedSubAppResource
Return type:

MatchedSubAppResource

Parameters:
  • domain (str)

  • subapp (Application)

add_routes(routes) → list[AbstractRoute]
Return type:

list[AbstractRoute]

Parameters:

routes (Iterable[AbstractRouteDef])

add_subapp(prefix, subapp) → PrefixedSubAppResource
Return type:

PrefixedSubAppResource

Parameters:
  • prefix (str)

  • subapp (Application)

async cleanup() → None

Causes on_cleanup signal

Should be called after shutdown()

Return type:

None

property cleanup_ctx: CleanupContext
clear() → None.  Remove all items from D.
property debug: bool
freeze() → None
Return type:

None

property frozen: bool
get(key, default=None) → Any
Return type:

Any

Parameters:
  • key (str | AppKey[_T])

  • default (Any)

items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
property loop: AbstractEventLoop
make_handler(*, loop=None, access_log_class=<class 'aiohttp.web_log.AccessLogger'>, **kwargs) → Server
Return type:

Server

Parameters:
property middlewares: FrozenList
property on_cleanup: Signal
property on_response_prepare: Signal
property on_shutdown: Signal
property on_startup: Signal
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

pre_freeze() → None
Return type:

None

property pre_frozen: bool
property router: UrlDispatcher
setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
async shutdown() → None

Causes on_shutdown signal

Should be called before cleanup()

Return type:

None

async startup() → None

Causes on_startup signal

Should be called in the event loop along with the request handler.

Return type:

None

update([E, ]**F) → None.  Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() → an object providing a view on D's values
error_messages(ctx, e) → MessageResponse | None

Used to return error messages to Discord.

By default, it will only cover CheckFailed errors. You can overwrite this function to return your own error messages.

Parameters:
  • ctx (Context) – The context of the command

  • e (Exception) – The exception that was raised

Return type:

MessageResponse | None

Returns:

The message response provided by the library error handler

async index_ping(_request) → Response

Used to ping the interaction url, to check if it’s working.

You can overwrite this function to return your own data as well. Remember that it must return dict

Return type:

Response

Parameters:

_request (Request)

jsonify(data, *, status=200) → Response

Respond with JSON data in a standardized way using orjson.

Serves as the replacement for aiohttp’s built-in json response.

Parameters:
  • data (dict | list) – The data to respond with

  • status (int) – The status code to respond with

Return type:

Response

Returns:

The response object

multipart_response(body, *, status=200) → Response

Respond to an interaction in a standardized way.

Parameters:
  • body (BaseResponse | None) – The body to respond with

  • status (int) – The status code to respond with

Return type:

Response

Returns:

The response object

start(*, host='127.0.0.1', port=8080) → None

Start the HTTP server.

Parameters:
  • host (str) – Host to use, if not provided, it will use 127.0.0.1

  • port (int) – Port to use, if not provided, it will use 8080

Return type:

None