API Documentation#
Naming#
Generally, functions like get_xxxx use cached values and functions like fetch_xxxx block while waiting for a response from the server. Functions like list_xxxx generally return multiple items, usually but not always from cache.
Classes#
- class asterpy.Client(username, password)#
Asterpy client that can be connected to multiple servers
Attributes
Methods
- @event(self, fn)#
- Register an event handler with the client. Used as a decorator. Possible event handlers are:
on_message: Called when any message is received in any channel.
fnmust take one argument of typeMessageon_packet: Called when any packet of any kind is received.
fnmust take one argument of typedicton_ready: Called when the client is finished initialising.
fnmust take no arguments.
- __init__(username, password)#
- Parameters:
username (
str) – the default username to use for connecting to serverspassword (
str) – the default password to use for connecting to servers
- add_server(ip, port, *, username=None, password=None, uuid=None, connect_mode=ConnectionMode.LOGIN)#
Add a server to the list of servers to connect to.
- Parameters:
ip (
str) – the IP to connect to.port (
int) – the port to connect to.uuid (
int) – User ID to log in with. Prefer specifying this over specifying the username, as the UUID will not change even if you change the username.username (
str) – The username to log in with. If neitheruuidorusernameare specified, the username passed to the constructor will be used.password (
str) – The password to log in with. If no password is provided, the password passed to the constructor will be used.login – Whether or not to log in to this server.
register – Whether or not to register an account with this server.
- get_user(uuid)#
Get the
Userobject corrosponding to the given UUID. Prefer using theServer.get_user()method if you already have theServerobject.
- get_channel(uuid)#
Get the
Channelobject associated with the given ID. Prefer using theServer.get_channel()method if you already have theServerobject.- Parameters:
uuid (
int) – The ID of the channel.- Return type:
Optional[Channel]- Returns:
The channel, or
Noneif it doesn’t exist
- get_channel_by_name(name)#
Get the
Channelobject by referring to its name. Generally, prefer using the ID to reference channels rather than the name if possible. Prefer using theServer.get_channel_by_name()method if you already have theServerobject.- Parameters:
name (
str) – The name of the channel.- Return type:
Optional[Channel]- Returns:
The channel, or
Noneif it doesn’t exist.
- async connect(init_commands=None)#
Connect to all servers.
- class asterpy.Server(ip, port, *, username=None, password=None, uuid=None, connect_mode=ConnectionMode.LOGIN)#
Represents a client connection to one server
Attributes
-
name:
str= ''# Server name
-
icon:
bytes= b''# PNG-encoded server picture
- self_uuid#
UUID of logged in account on this server
Methods
- __init__(ip, port, *, username=None, password=None, uuid=None, connect_mode=ConnectionMode.LOGIN)#
- async connect(init_commands=None)#
Connect to the server and listen for packets. This function blocks until
Client.disconnect()is called.- Parameters:
init_commands (
Optional[List[dict]]) – Optional list of packets to send to the server after connecting.
- async disconnect()#
Disconnect from the server.
- get_channel_by_name(name)#
Get the
Channelobject by referring to its name. Generally, prefer using the ID to reference channels rather than the name if possible, as the name could change.- Parameters:
name (
str) – The name of the channel.- Return type:
Optional[Channel]- Returns:
The channel, or
Noneif it doesn’t exist.
- async fetch_user(uuid)#
Fetch a
Userfresh from the server. Send a new packet and get the result instead of using cached data. :type uuid:int:param uuid: The UUID of the user. :rtype:Optional[User] :returns: TheUserobject, orNoneif the user doesn’t exist or another error occurred.
- async send(message)#
Send a packet to the server.
- Parameters:
message (
dict[any]) – The packet to send, as a dictionary.
- async get_response(packet)#
Send a packet of data to the server and wait for a response. :type packet:
dict:param packet: Data to be sent to the server as python dictionary, converted to JSON before sending. :rtype:dict:returns: The data from the server, decoded to a python dictionary.
-
name:
- class asterpy.ConnectionMode(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#
How to authenticate with the aster server.
Attributes
- LOGIN = 1#
- REGISTER = 2#
- NEITHER = 3#
- class asterpy.Channel(client, name, uuid)#
Represents an aster channel.
Attributes
- name#
- uuid#
Methods
- async send(message, reply_to=None)#
Send a text message to the channel.
- Parameters:
message (
str) – The text to be sentreply_to (
Optional[int]) – The UUID of the message to reply to, orNoneif not replying.
- Returns:
The
Messageobject that has been sent
- async fetch_history(count=100, init_message=None)#
Fetch the last
countmessages from a given channel.- Parameters:
channel – The channel from which to fetch the messages.
count (
int) – The number of messages to fetch. (defeault: 100)init_message (
Message) – Fetchcountmessages before this message. If init_message == None, then fetch the lastcountmessages.
- Return type:
list[Message]- Returns:
A list of messages.
- class asterpy.User(uuid, username, pfp=None)#
Represents a user on the aster server
Attributes
- uuid#
- username#
- pfp#
PNG-compressed image data
- class asterpy.Message(content, user, channel, server, date, uuid, reply_uuid=None)#
Represents a message in a channel on the server
Attributes
- content#
- author#
- channel#
- date#
UNIX timestamp
- reply_uuid#
UUID of the message this is replying to
- async edit(new_content)#
Edit a message. The message must have been sent by the account attempting to edit it.
- Parameters:
new_content (
str) – The new body text of the message.
- async delete()#
Delete this message. This message must be sent by the account that’s deleting it.
- async reply(content)#
Reply to this message. Equivalent to sending a new message with the reply field set to this message’s UUID.
- Returns:
The new
Messageobject that was sent in reply.
- class asterpy.Emoji(name, uuid, data)#
Represents a custom emoji
Attributes
- data#
PNG-compressed image data as bytes.
- name#
- uuid#
- class asterpy.SyncServer(ip, port, name, pfp, uuid)#
Represents a server entry in the SyncData.
Attributes
- ip#
- port#
- pfp#
PNG-compressed image data
- name#
- uuid#
Methods
- from_json()#
Create a SyncServer from an entry in the
sync_get_serverspacket.- Parameters:
value (
dict) – The entry to deserialise.- Return type:
- class asterpy.SyncData(uname, pfp, servers)#
Represents the data used to sync an aster account across multiple devices. This data can be stored on any aster server which the user designates their sync server. It contains information such as the current username, profile picture, and server list so they can be consistant across clients.
Attributes
- uname#
- pfp#
- servers#
Methods
- from_json(servers)#
Create a SyncData object from a
sync_getpacket and async_get_serverspacket.- Parameters:
value (
dict) – A dictionary containing sync data.servers (
dict) – A dictionary containing a list of sync server data.
- Returns:
The SyncData object, or
Noneif the packet’s status is 404 (no sync data on the server).- Return type:
Optional[SyncData]