Backend

discord_http.backend module

class discord_http.backend.DiscordHTTP(*, client)

Bases: Application

Serves as the fundemental HTTP server for Discord Interactions.

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

Parameters:

client (Client)

uptime: datetime

The time when the HTTP server was started.

bot: Client

The bot instance that is using this HTTP server.

debug_events

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

verify_key: VerifyKey | None

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

ATTRS = frozenset({'_cleanup_ctx', '_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
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

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
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

Parameters:
  • request – The incoming request object (not used by default)

  • _request (Request)

Return type:

Response

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) – 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 with multipart data 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