State¶
State handler for Andesite clients.
If you want to store your state externally for example in a database, there
are two approaches you can use. You can either implement the
AbstractState
and use the default AndesitePlayerState
(which can be
easily converted to and from JSON), or you can use the default State
with a custom AbstractPlayerState
implementation.
Both approaches have their advantages, but you should always consider that while
the get operations are often performed together (especially during state
migration), the set operations are not.
-
StateArgumentType
¶ (Type alias) types which can be passed as a state handler to a client.
- Type
Union[AbstractState, bool, None]
-
player_to_raw
(player)[source]¶ Convert the given player to a JSON-serialisable dict.
- Parameters
player (
Player
) – Player to convert.- Return type
- Returns
A JSON-Serialisable dict containing all the data of the player. This data can be passed to
player_from_raw
which creates aPlayer
instance again.
-
voice_server_update_to_raw
(update)[source]¶ Convert the given voice server update to a JSON-serialisable dict.
- Parameters
update (
VoiceServerUpdate
) – Voice server update to convert.- Return type
- Returns
A JSON-Serialisable dict containing all the data of the voice server update. This data can be passed to
voice_server_update_from_raw
which creates aVoiceServerUpdate
instance again.
-
voice_server_update_from_raw
(data)[source]¶ Recreate a voice server update from the converted dict.
- Parameters
data (
Dict
[str
,Any
]) – Data returned byvoice_server_update_to_raw
to convert.- Raises
Exception – If the data can’t be used to create a voice server update.
- Return type
- Returns
Created
VoiceServerUpdate
instance.
-
class
AbstractPlayerState
[source]¶ State of a single Andesite player.
Notes
Unless you’re doing something weird you don’t need to call the setter functions. If the player state is managed by an
AbstractState
which is connected to a client then everything is done for you.See also
AndesitePlayerState
for an in-memory implementation.-
abstract async
get_voice_server_update
()[source]¶ Get the last voice server update that was sent to the player.
- Return type
- Returns
The last voice server update or
None
if none exists.
-
abstract async
set_voice_server_update
(update)[source]¶ Set the last voice server update.
- Parameters
update (
Optional
[VoiceServerUpdate
]) – Voice server update to set.- Return type
None
-
abstract async
-
class
PlayerState
(guild_id)[source]¶ Default player state storing the state in memory.
The player state can be converted to a JSON-serialisable dict using
to_raw
. A state can also be created from said dict using the classmethodfrom_raw
. These methods exist to make it easy to implement a customAbstractState
which loads and stores serialised player states.-
async
get_voice_server_update
()[source]¶ Get the last voice server update that was sent to the player.
- Return type
- Returns
The last voice server update or
None
if none exists.
-
async
set_voice_server_update
(update)[source]¶ Set the last voice server update.
- Parameters
update (
Optional
[VoiceServerUpdate
]) – Voice server update to set.- Return type
None
-
classmethod
from_raw
(data)[source]¶ Create an
AbstractPlayerState
from the raw data.- Parameters
data (
Dict
[str
,Any
]) – Raw player state data as returned byto_raw
- Return type
- Returns
A new instance of
PlayerState
describing the same state as the data.
-
async
-
class
AbstractState
[source]¶ Andesite state handler.
Keeps track of the state of an Andesite node.
-
abstract async
handle_player_update
(update)[source]¶ Handle a player update.
- Parameters
update (
PlayerUpdate
) – Update that was received.- Return type
None
-
abstract async
handle_andesite_event
(event)[source]¶ Handle an Andesite event.
- Parameters
event (
AndesiteEvent
) – Andesite event that was received.- Return type
None
-
abstract async
handle_voice_server_update
(guild_id, update)[source]¶ Handle a voice server update.
- Parameters
guild_id (
int
) – Guild the update applies to.update (
VoiceServerUpdate
) – Voice server update.
- Return type
None
-
async
on_handle_message_error
(message, exc)[source]¶ Called when an error occurs during message handling.
- Parameters
message (
ReceiveOperation
) – Message that caused the errorexc (
Exception
) – Exception that was raised.
- Return type
None
-
async
on_handle_sent_message_error
(guild_id, op, payload, exc)[source]¶ Called when an error occurs during sent message handling.
-
abstract async
-
class
State
(*, state_factory=<class 'andesite.state.PlayerState'>, keep_states=True)[source]¶ Default implementation of
AbstractState
.Stores the player states in memory, unless explicitly suppressed.
- Parameters
state_factory (
Callable
[[int
], ~PST]) – Callable which when called with a guild id, creates anAbstractPlayerState
. The default isPlayerState
.keep_states (
bool
) – Whether or not the player states should be stored when they’re created. This is required for in-memory storage as otherwise the data would be lost.
-
player_states
¶ Mapping from
- Type
Optional[Dict[int, AbstractPlayerState]]
-
guild id to the corresponding player state. `None` if `keep_states`
-
was `False`.
-
player_states
-
async
handle_player_update
(update)[source]¶ Handle a player update.
- Parameters
update (
PlayerUpdate
) – Update that was received.- Return type
None
-
async
on_handle_message_error
(message, exc)¶ Called when an error occurs during message handling.
- Parameters
message (
ReceiveOperation
) – Message that caused the errorexc (
Exception
) – Exception that was raised.
- Return type
None
-
async
on_handle_sent_message_error
(guild_id, op, payload, exc)¶ Called when an error occurs during sent message handling.
-
async
handle_andesite_event
(event)[source]¶ Handle an Andesite event.
- Parameters
event (
AndesiteEvent
) – Andesite event that was received.- Return type
None
-
async
handle_voice_server_update
(guild_id, update)[source]¶ Handle a voice server update.
- Parameters
guild_id (
int
) – Guild the update applies to.update (
VoiceServerUpdate
) – Voice server update.
- Return type
None