hikari.impl.rest_bot
#
Standard implementations of a Interaction based REST-only bot.
RESTBot
#
RESTBot(
token: Union[str, TokenStrategy],
token_type: Union[TokenType, str, None] = None,
public_key: Union[bytes, str, None] = None,
*,
allow_color: bool = True,
banner: Optional[str] = "hikari",
suppress_optimization_warning: bool = False,
executor: Optional[Executor] = None,
force_color: bool = False,
http_settings: Optional[HTTPSettings] = None,
logs: Union[None, str, int, Dict[str, Any], PathLike[str]] = "INFO",
max_rate_limit: float = 300.0,
max_retries: int = 3,
proxy_settings: Optional[ProxySettings] = None,
rest_url: Optional[str] = None
)
Bases: RESTBotAware
, InteractionServer
Basic implementation of an interaction based REST-only bot.
PARAMETER | DESCRIPTION |
---|---|
token |
The bot or bearer token.
TYPE:
|
token_type |
The type of token in use. This should only be passed when This should be left as |
PARAMETER | DESCRIPTION |
---|---|
allow_color |
Whether to enable coloured console logs on any platform that is a TTY.
Setting a Users should consider this an advice to the application on whether it is
safe to show colours if possible or not. Since some terminals can be
awkward or not support features in a standard way, the option to
explicitly disable this is provided. See
TYPE:
|
banner |
The package to search for a Setting this to |
suppress_optimization_warning |
By default, Hikari warns you if you are not running your bot using
optimizations (
TYPE:
|
executor |
If non- While mainly supporting the |
force_color |
If This will take precedence over
TYPE:
|
http_settings |
Optional custom HTTP configuration settings to use. Allows you to
customise functionality such as whether SSL-verification is enabled,
what timeouts
TYPE:
|
logs |
The flavour to set the logging to. This can be If you pass a If you pass a If you pass a Note that |
max_rate_limit |
The max number of seconds to backoff for when rate limited. Anything greater than this will instead raise an error. This defaults to five minutes if left to the default value. This is to stop potentially indefinitely waiting on an endpoint, which is almost never what you want to do if giving a response to a user. You can set this to Note that this only applies to the REST API component that communicates with Discord, and will not affect sharding or third party HTTP endpoints that may be in use.
TYPE:
|
max_retries |
Maximum number of times a request will be retried if
it fails with a Defaults to 3 if set to |
proxy_settings |
Custom proxy settings to use with network-layer logic in your application to get through an HTTP-proxy.
TYPE:
|
public_key |
|
rest_url |
Defaults to the Discord REST API URL if |
RAISES | DESCRIPTION |
---|---|
ValueError
|
|
Examples:
Simple logging setup:
hikari.RESTBot("TOKEN", logs="INFO") # Registered logging level
# or
hikari.RESTBot("TOKEN", logs=20) # Logging level as an int
File config:
# See https://docs.python.org/3/library/logging.config.html#configuration-file-format for more info
hikari.RESTBot("TOKEN", logs="path/to/file.ini")
Setting up logging through a dict config:
# See https://docs.python.org/3/library/logging.config.html#dictionary-schema-details for more info
hikari.RESTBot(
"TOKEN",
logs={
"version": 1,
"incremental": True, # In incremental setups, the default stream handler will be setup
"loggers": {
"hikari.gateway": {"level": "DEBUG"},
"hikari.ratelimits": {"level": "TRACE_HIKARI"},
},
}
)
entity_factory
property
#
entity_factory: EntityFactory
Entity factory implementation for this object.
executor
property
#
interaction_server
property
#
interaction_server: InteractionServer
Interaction server this app is bound to.
is_alive
property
#
is_alive: bool
Whether the application is running or not.
This is useful as some functions might raise
hikari.errors.ComponentStateConflictError
if this is
False
.
on_shutdown
property
#
Sequence of the bot's asynchronous shutdown callbacks.
on_startup
property
#
Sequence of the bot's asynchronous startup callbacks.
add_shutdown_callback
#
Add an asynchronous callback to be called when the bot shuts down.
PARAMETER | DESCRIPTION |
---|---|
callback |
The asynchronous shutdown callback to add.
TYPE:
|
add_startup_callback
#
Add an asynchronous callback to be called when the bot starts up.
PARAMETER | DESCRIPTION |
---|---|
callback |
The asynchronous startup callback to add.
TYPE:
|
get_listener
#
get_listener(
interaction_type: Type[_InteractionT_co],
) -> Optional[ListenerT[_InteractionT_co, InteractionResponseBuilder]]
Get the listener registered for an interaction.
PARAMETER | DESCRIPTION |
---|---|
interaction_type |
Type of the interaction to get the registered listener for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
typing.Optional[ListenersT[hikari.interactions.base_interactions.PartialInteraction, hikari.api.special_endpoints.InteractionResponseBuilder]
|
The callback registered for the provided interaction type if found,
else |
join
async
#
Wait indefinitely until the application closes.
This can be placed in a task and cancelled without affecting the application runtime itself. Any exceptions raised by shards will be propagated to here.
on_interaction
async
#
Handle an interaction received from Discord as a REST server.
PARAMETER | DESCRIPTION |
---|---|
body |
The interaction payload.
TYPE:
|
signature |
Value of the
TYPE:
|
timestamp |
Value of the
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Response
|
Instructions on how the REST server calling this should respond to the interaction request. |
print_banner
staticmethod
#
print_banner(
banner: Optional[str],
allow_color: bool,
force_color: bool,
extra_args: Optional[Dict[str, str]] = None,
) -> None
Print the banner.
This allows library vendors to override this behaviour, or choose to inject their own "branding" on top of what hikari provides by default.
Normal users should not need to invoke this function, and can simply
change the banner
argument passed to the constructor to manipulate
what is displayed.
PARAMETER | DESCRIPTION |
---|---|
banner |
The package to find a |
allow_color |
A flag that allows advising whether to allow color if supported or
not. Can be overridden by setting a
TYPE:
|
force_color |
A flag that allows forcing color to always be output, even if the
terminal device may not support it. Setting the This will take precedence over
TYPE:
|
extra_args |
If provided, extra $-substitutions to use when printing the banner. Default substitutions can not be overwritten. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
remove_shutdown_callback
#
Remove an asynchronous shutdown callback from the bot.
PARAMETER | DESCRIPTION |
---|---|
callback |
The shutdown callback to remove.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the callback was not registered. |
remove_startup_callback
#
Remove an asynchronous startup callback from the bot.
PARAMETER | DESCRIPTION |
---|---|
callback |
The asynchronous startup callback to remove.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the callback was not registered. |
run
#
run(
asyncio_debug: bool = False,
backlog: int = 128,
check_for_updates: bool = True,
close_loop: bool = True,
close_passed_executor: bool = False,
coroutine_tracking_depth: Optional[int] = None,
enable_signal_handlers: Optional[bool] = None,
host: Optional[Union[str, Sequence[str]]] = None,
path: Optional[str] = None,
port: Optional[int] = None,
propagate_interrupts: bool = False,
reuse_address: Optional[bool] = None,
reuse_port: Optional[bool] = None,
shutdown_timeout: float = 60.0,
socket: Optional[socket] = None,
ssl_context: Optional[SSLContext] = None,
) -> None
Open this REST server and block until it closes.
PARAMETER | DESCRIPTION |
---|---|
asyncio_debug |
If
TYPE:
|
backlog |
The number of unaccepted connections that the system will allow before refusing new connections.
TYPE:
|
check_for_updates |
If
TYPE:
|
close_loop |
If This will wait until all hikari-owned
TYPE:
|
close_passed_executor |
If
TYPE:
|
coroutine_tracking_depth |
If an integer value and supported by the interpreter, then this many nested coroutine calls will be tracked with their call origin state. This allows you to determine where non-awaited coroutines may originate from, but generally you do not want to leave this enabled for performance reasons. |
enable_signal_handlers |
Defaults to If on a non-Windows OS with builtin support for kernel-level
POSIX signals, then setting this to |
host |
TCP/IP host or a sequence of hosts for the HTTP server. |
port |
TCP/IP port for the HTTP server. |
path |
File system path for HTTP server unix domain socket. |
reuse_address |
Tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire. |
reuse_port |
Tells the kernel to allow this endpoint to be bound to the same port as other existing endpoints are also bound to. |
socket |
A pre-existing socket object to accept connections on. |
shutdown_timeout |
A delay, in seconds, to wait for graceful server shut down before forcefully disconnecting all open client sockets.
TYPE:
|
ssl_context |
SSL context for HTTPS servers.
TYPE:
|
set_listener
#
set_listener(
interaction_type: Type[_InteractionT_co],
listener: Optional[ListenerT[_InteractionT_co, InteractionResponseBuilder]],
/,
*,
replace: bool = False,
) -> None
Set the listener callback for this interaction server.
PARAMETER | DESCRIPTION |
---|---|
interaction_type |
The type of interaction this listener should be registered for.
TYPE:
|
listener |
The asynchronous listener callback to set or An asynchronous listener can be either a normal coroutine or an async generator which should yield exactly once. This allows sending an initial response to the request, while still later executing further logic.
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
replace |
Whether this call should replace the previously set listener or not.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
TypeError
|
If |
start
async
#
start(
backlog: int = 128,
check_for_updates: bool = True,
host: Optional[Union[str, Sequence[str]]] = None,
port: Optional[int] = None,
path: Optional[str] = None,
reuse_address: Optional[bool] = None,
reuse_port: Optional[bool] = None,
socket: Optional[socket] = None,
shutdown_timeout: float = 60.0,
ssl_context: Optional[SSLContext] = None,
) -> None
Start the bot and wait for the internal server to startup then return.
Note
For more information on the other parameters such as defaults see AIOHTTP's documentation.
PARAMETER | DESCRIPTION |
---|---|
backlog |
The number of unaccepted connections that the system will allow before refusing new connections.
TYPE:
|
check_for_updates |
If
TYPE:
|
host |
TCP/IP host or a sequence of hosts for the HTTP server. |
port |
TCP/IP port for the HTTP server. |
path |
File system path for HTTP server unix domain socket. |
reuse_address |
Tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire. |
reuse_port |
Tells the kernel to allow this endpoint to be bound to the same port as other existing endpoints are also bound to. |
socket |
A pre-existing socket object to accept connections on. |
shutdown_timeout |
A delay, in seconds, to wait for graceful server shut down before forcefully disconnecting all open client sockets.
TYPE:
|
ssl_context |
SSL context for HTTPS servers.
TYPE:
|