Handlers#
Some users might want to go beyond the boundaries and might want to handle everything by themself. Fortunately, library provides a set of classes that provide low level control over the API. Conventionally, we tend to call these low level classes, “Handlers”
Following are the main handlers provided by the library:
HTTPHandler#
- class luster.HTTPHandler(*, token: str, session: Optional[aiohttp.client.ClientSession] = None, bot: bool = True)#
A class that handles HTTP requests to Revolt API.
In most cases, you might not need to interact with this class.
Clientprovides a high level abstraction for this class.You should not initialize this class manually. Instead, use the
create_http_handler()helper function. In aClient, you can useClient.http_handlerto get the instance of this class.- Parameters
token (
str) – The bot or session token used for authenticating requests. This cannot be modified once initialized.bot (
bool) – Whether the passedtokenis a bot token. Set this toFalsewhen a session token is passed. Defaults toTrue.session (Optional[
aiohttp.ClientSession]) –The client session used for making HTTP requests.
If not provided, A session is created internally and would be closed automatically after usage. Note that when a session is provided by the user, It must be closed by the user. Library will not take it’s ownership.
- AUTUMN_BASE_URL: ClassVar[str] = 'https://autumn.revolt.chat'#
The base URL for Autumn file service.
- USER_AGENT: ClassVar[str] = 'Luster (0.1.0a3, https://github.com/izxxr/luster)'#
The user agent for attaching to requests.
This by default includes library name but could be modified to be different.
Danger
Sending user agent that are invalid or are in invalid format can result in failing HTTP requests. It is recommended to not change this attribute!
- async add_group_member(channel_id: str, user_id: str) types.AddGroupMemberResponse#
Adds a member to group.
- Parameters
- Returns
- Return type
- async change_username(json: types.ChangeUsernameJSON) types.ChangeUsernameResponse#
Changes the username of current user.
- Parameters
json (
types.EditUserJSON) – The JSON body for request.- Returns
- Return type
- async close() bool#
Closes the underlying client session, if possible.
- Returns
Indicates whether the session was closed.
- Return type
- async create_group(json: types.CreateGroupJSON) types.CreateGroupResponse#
Creates a group.
- Parameters
json (
types.CreateGroupJSON) – The JSON body for request.- Returns
- Return type
- async create_server(json: types.CreateServerJSON) types.CreateServerResponse#
Creates a new server.
- Parameters
json (
types.CreateServerJSON) – The JSON body for request.- Returns
- Return type
- async create_server_channel(server_id: str, json: types.CreateServerChannelJSON) types.CreateServerChannelResponse#
Creates a channel in a server.
- Parameters
server_id (
str) – The ID of server to create the channel in.json (
types.CreateServerChannelJSON) – The JSON body for request.
- Returns
- Return type
- async delete_channel(channel_id: str) types.DeleteChannelResponse#
Deletes the channel by it’s ID.
For server channel, This route deletes the channel and for direct messages and groups, This route leaves or closes the channel.
- Parameters
channel_id (
str) – The ID of channel to delete.- Returns
- Return type
- async delete_server(server_id: str) types.DeleteChannelResponse#
Deletes a server.
If the requesting user is not the server owner, The server is left by the user.
- Parameters
server_id (
str) – The ID of server to delete.- Returns
- Return type
- async edit_channel(channel_id: str, json: types.EditChannelJSON) types.EditChannelResponse#
Edits a channel.
- Parameters
channel_id (
str) – The ID of channel to delete.json (
types.EditChannelJSON) – The JSON body for request.
- Returns
- Return type
- async edit_server(server_id: str, json: types.EditServerJSON) types.EditServerResponse#
Edits a server.
- Parameters
server_id (
str) – The ID of server to edit.json (
types.EditServerJSON) – The JSON body for request.
- Returns
- Return type
- async edit_user(json: types.EditUserJSON) types.EditUserResponse#
Edits the current user.
- Parameters
json (
types.EditUserJSON) – The JSON body for request.- Returns
- Return type
- async fetch_channel(channel_id: str) types.FetchChannelResponse#
Fetches the channel by it’s ID.
- Parameters
channel_id (
str) – The ID of channel to fetch.- Returns
- Return type
- async fetch_direct_message_channels() types.FetchDirectMessageChannelsResponse#
Fetches the direct message channels.
This returns all currently open direct messages and group channels that user is part of.
- Returns
- Return type
- async fetch_group_members(channel_id: str) types.FetchGroupMembersResponse#
Fetches the members of a group.
- Parameters
channel_id (
str) – The ID of group to fetch members of.- Returns
- Return type
- async fetch_node_info() types.NodeInfo#
An alias of
query_node()method.
- async fetch_profile(user_id: str) types.FetchProfileResponse#
Fetch a user’s profile.
- Parameters
json (
types.EditUserJSON) – The JSON body for request.- Returns
- Return type
- async fetch_self() types.FetchSelfResponse#
Fetches the user information for current authenticated user.
- Returns
- Return type
- async fetch_server(server_id: str) types.FetchServerResponse#
Fetches a server.
- Parameters
server_id (
str) – The ID of server to fetch.- Returns
- Return type
- async fetch_user(user_id: str) types.FetchUserResponse#
Fetches the user of given ID.
- Parameters
user_id (
str) – The ID of user to fetch.- Returns
- Return type
- async mark_server_as_read(server_id: str) types.MarkServerAsReadResponse#
Marks a server as read.
- Parameters
server_id (
str) – The ID of server to mark as read.- Returns
- Return type
- async open_direct_message(user_id: str) types.OpenDirectMessageResponse#
Opens the direct message channel with a user.
This returns the opened direct message or saved messages channel if the given user ID belongs to ownself.
- Parameters
user_id (
str) – The ID of user to open direct message channel with.- Returns
- Return type
- async query_node() types.NodeInfo#
Fetches the information about current Revolt instance.
This route does not require authorization.
- Returns
- Return type
- async remove_group_member(channel_id: str, user_id: str) types.RemoveGroupMemberResponse#
Removes a member from a group.
- Parameters
- Returns
- Return type
- async request(method: str, route: str, base_url: Optional[str] = None, **kwargs: Any) Any#
Requests a certain route and returns the response data.
- Parameters
method (
str) – The HTTP method to use.base_url (
str) – Override the base URL per request. Defaults toBASE_URL.**kwargs –
The keyword arguments that are passed to
aiohttp.ClientSession.request(). It is worth noting that provided headers will be updated to include the proper authentication headers and you don’t have to add any authentication headers manually.Following headers will be overwritten:
X-Session-TokenX-Bot-Token
- property session: Optional[aiohttp.client.ClientSession]#
The client session used for making HTTP requests.
- Returns
- Return type
Optional[
aiohttp.ClientSession]
- async set_channel_default_permission(channel_id: str, json: types.SetChannelDefaultPermissionJSON) types.SetChannelDefaultPermissionResponse#
Sets permissions override for the default role in a server channel.
The channel ID must point to a server text or voice channel or a private group channel.
- Parameters
channel_id (
str) – The ID of server channel to set the role permission for.json (
types.SetChannelRolePermissionJSON) – The new permissions data.
- Returns
- Return type
- async set_channel_role_permission(channel_id: str, role_id: str, json: types.SetChannelRolePermissionJSON) types.SetChannelRolePermissionResponse#
Sets permissions override for a specific role in a server channel.
The channel ID must point to a server text or voice channel.
- Parameters
channel_id (
str) – The ID of server channel to set the role permission for.role_id (
str) – The ID of role whose permission is being set.json (
types.SetChannelRolePermissionJSON) – The new permissions data.
- Returns
- Return type
- async set_server_default_permission(server_id: str, json: types.SetServerDefaultPermissionJSON) types.SetServerDefaultPermissionResponse#
Sets permissions for the default role in a server.
- Parameters
server_id (
str) – The ID of server to set the role permission for.json (
types.SetServerRolePermissionJSON) – The new permissions data.
- Returns
- Return type
- async set_server_role_permission(server_id: str, role_id: str, json: types.SetServerRolePermissionJSON) types.SetServerRolePermissionResponse#
Sets permissions for a specific role in a server.
- Parameters
server_id (
str) – The ID of server to set the role permission for.role_id (
str) – The ID of role whose permission is being set.json (
types.SetServerRolePermissionJSON) – The new permissions data.
- Returns
- Return type
- property token: Optional[str]#
The token used for authenticating requests.
- Returns
- Return type
Optional[
str]
- async upload_file(file: io.BufferedReader, tag: types.FileTag) types.UploadFileResponse#
Uploads a file to Autumn file server.
- Parameters
file (
io.BufferedReader) – The file buffer to upload.tag (
types.FileTag) – The tag or bucket to upload this file to.
- Returns
The uploaded file.
- Return type
- luster.create_http_handler(*, cls: Literal[None] = 'None', token: str, session: Optional[aiohttp.client.ClientSession] = None, bot: bool = True) luster.http.HTTPHandler#
- luster.create_http_handler(*, cls: Optional[Type[luster.http.HTTPHandlerT]] = None, token: str, session: Optional[aiohttp.client.ClientSession] = None, bot: bool = True) luster.http.HTTPHandlerT
A helper for creating a
HTTPHandler.This function can either be awaited or be used in an asynchronous context manager.
Example:
async with luster.create_http_handler(token="...") as http: await http.fetch_api_info() # is equivalent of http = await luster.create_http_handler() await http.fetch_api_info() # After usage, don't forget to close! await http.close()
- Parameters
cls (Optional[Type[
HTTPHandler]]) – The class type ofHTTPHandlerto return. This can be used to return custom subclasses.token (
str) – The bot or session token used for authenticating requests. This cannot be modified once initialized.bot (
bool) – Whether the passedtokenis a bot token. Set this toFalsewhen a session token is passed. Defaults toTrue.session (Optional[
aiohttp.ClientSession]) –The client session used for making HTTP requests.
If not provided, A session is created internally and would be closed automatically after usage. Note that when a session is provided by the user, It must be closed by the user. Library will not take it’s ownership.
- Returns
The created HTTP handler.
- Return type
WebsocketHandler#
- class luster.WebsocketHandler(http_handler: HTTPHandler, version: types.WebsocketVersion = 1)#
A class that handles websocket connection with Revolt Events API.
This is a low level interface of
Clientthat provides interface for realtime communication with Revolt Events API using websocket.- Parameters
http_handler (
HTTPHandler) – The HTTP handler used for opening websocket connections.version (
types.WebsocketVersion) – The version of websocket protocol to use. Defaults to1and currently there are no other options than1
- async connect() None#
Connects the websocket.
This is a blocking coroutine that does not return until websocket connection is closed.
- async get_websocket_url(*, include_token: bool = True, transport_format: Optional[types.WebsocketFormat] = None) str#
Gets the websocket URL including relevant parameters.
This under the hood uses the
HTTPHandler.query_node()route to retrieve the websocket URL.- Parameters
include_token (
bool) – Whether to include token in the returned URL.transport_format (
types.WebsocketFormat) – The format to use for websocket packets transporation. By default or by omitting this parameter, The ideal format is automatically selected.
- Returns
The websocket URL used for connecting to Revolt websocket.
- Return type
- property http_handler: luster.http.HTTPHandler#
The HTTP handler associated to this websocket handler.
- Returns
- Return type
- property latency: float#
The current latency of websocket.
This is measured by delay between ping sent by client and it’s acknowledgement by the server.
- Returns
- Return type
- async on_websocket_event(type: types.EventTypeRecv, data: Dict[str, Any]) Any#
A hook that gets called whenever a websocket event is received.
By default, this does nothing. The subclasses can override this method to implement custom behaviour.
- Parameters
type (
types.EventTypeRecv) – The type of event that was received.data (
dict) – The received data.
- async send(type: types.EventTypeSend, data: Dict[str, Any]) None#
Sends an event via websocket.
- Parameters
type (
types.EventTypeSend) – The type of event to send.data (
dict) – The event data, excludingtype.
State#
- class luster.State(http_handler: HTTPHandler, websocket_handler: WebsocketHandler, cache: Cache)#
A class that manages state of a Revolt bot.
This class mainly serves the purpose of properly passing data and tracking state across different classes and specifically data models.
You should not worry about this class when using
Clientas it is automatically handled under the hood. However, when using various handlers to interact with Revolt API, you might need to manually initialize models in which case you will need this class.- Parameters
http_handler (
HTTPHandler) – The HTTP handler used for HTTP requests.websocket_handler (
WebsocketHandler) – The websocket handler used for websocket connections.cache (
Cache) – The cache handler used for caching entities.
- property http_handler: HTTPHandler#
The HTTP handler associated to this state.
- Returns
- Return type
- property websocket_handler: WebsocketHandler#
The websocket handler associated to this state.
- Returns
- Return type