Pools¶
Client pools for multiple clients.
Client pools work exactly like the other clients, but internally they use more than one client (preferably to multiple Andesite nodes).
-
RegionGuildComparator (Callable[[int, Optional[str]], int] (Type alias) Function which takes a guild id and the node region of an Andesite node and returns an integer to indicate how well the guild is suited for the region.
-
PoolScoringFunction¶ (Type alias) Function which takes
ScoringDataas its only argument and returns a comparable object. The function may also be a coroutine. The return value is only compared to return values of the same function and the comparisons are bigger than (>) and less than (<). Equality (==) is implied if something is neither bigger nor smaller than the other value. A bigger return value (by comparison) implies that theScoringDatais better (ex: more suitable for the given guild).- Type
Callable[[ScoringData], Union[Any, Awaitable[Any]]]
-
NodeDetails¶ (Type alias) Tuple containing the uri and the password of an Andesite node.
-
exception
PoolException(pool)[source]¶ Pool exceptions.
-
pool¶ Pool which raised the error
- Type
-
with_traceback()¶ Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
-
-
exception
PoolEmptyError(pool)[source]¶ Raised when a pool is empty but shouldn’t be.
-
with_traceback()¶ Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
-
-
class
PoolClientAddEvent(pool, client)[source]¶ Event for when a new client is added to a pool.
-
pool¶ Pool the client was added to
- Type
-
client¶ Client that was added to the pool
- Type
CT
-
-
class
PoolClientRemoveEvent(pool, client)[source]¶ Event for when a client is removed from a pool.
-
pool¶ Pool the client was added to
- Type
-
client¶ Client that was added to the pool
- Type
CT
-
-
class
ClientPool(events=None)[source]¶ Andesite client pool.
-
async
reset()[source]¶ Reset all underlying clients so they may be used again.
This has the opposite effect of the
closemethod making the clients usable again.- Return type
None
-
abstract
add_client(client)[source]¶ Add a new client to the pool.
- Parameters
client (~CT) – Client to add.
- Raises
ValueError – If the client is already in the pool
Dispatches the
PoolClientAddEventevent.- Return type
None
-
abstract
remove_client(client)[source]¶ Remove a client from the pool.
- Parameters
client (~CT) – Client to remove.
- Raises
ValueError – If the client isn’t in the pool
Dispatches the
PoolClientRemoveEventevent.- Return type
None
-
add_child(emitter)¶ Add a child emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Child emitter to add.- Raises
ValueError – If the child emitter already is a child of this emitter, or the emitter is a child of the child emitter. The second case is only possible if the child emitter is itself an instance of
EmitterABC.- Return type
None
-
emit(event)¶ Emit an event to all observers.
The order in which the observers receive the events isn’t defined and shouldn’t be depended upon.
If a listener raises an error, a
ListenerErrorevent is emitted. If an error occurs while handling theListenerErrorevent, it is ignored.-
event¶ Event object to emit
- Return type
Awaitable[None]- Returns
asyncio.Futurewhich resolves toNoneafter all observers have handled the event.
-
-
has_child(emitter)¶ Check whether the child emitter is a child of the emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Emitter to check.- Return type
- Returns
Whether or not the given emitter is either a direct, or an indirect (child of a child) child of this emitter. Note that every emitter is a child of itself.
-
off(event=None, callback=None)¶ Disable an event listener or remove all listeners from an event.
This can also be used to disable a listener that was added using
oncebefore it is called.To remove all listeners from an event, pass the event selector to the
eventkeyword argument:class MyEvent: ... observable.off(event=MyEvent)
To remove a listener which is listening to all events, pass the listener to the
callbackkeyword argument:def on_any_event(event): print(event) observable.off(callback=on_any_event)
- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to remove the listeners for.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Event listener. This is the same callable that was passed toonoronce.
- Return type
None
-
on(event=None, callback=None)¶ Add a listener to the given event(s).
If you want to listen to all events you can pass the callback to the
callbackkeyword argument:def on_any_event(event): print(event) observable.on(callback=on_any_event)
- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to listen for. The latter case is equivalent to callingonfor each event separately.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Callable to call when the selected events are emitted. It is called with the event instance as its only argument. If it returns anAwaitable(or is a coroutine) it is awaited before the emission “completes” (SeeChildEmitterABC.emit).
- Return type
None
-
once(event=None, callback=None)¶ Add a listener which is only called once.
This is basically the same as adding the listener using
onand then callingoffonce the listener has been called.- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to listen for.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Callable to call when the selected events are emitted. It is called with the event instance as its only argument. If it returns anAwaitable(or is a coroutine) it is awaited before the emission “completes” (SeeChildEmitterABC.emit).
- Return type
None
-
remove_child(emitter)¶ Remove a child emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Child emitter to remove.- Raises
ValueError – If the emitter isn’t a direct child.
- Return type
None
-
subscribe(event=None)¶ Subscribe to the given event(s).
-
async
-
class
HTTPPoolBase(clients, *, timeout=None, max_penalties=5, penalty_time_frame=60)[source]¶ Andesite HTTP client pool.
- Parameters
clients (
Iterable[AbstractHTTP]) – HTTP clients to initialise the pool withmax_penalties (
int) – Sets themax_penaltiesattribute.penalty_time_frame (
float) – Sets thepenalty_time_frameattribute.
The pool uses a circular buffer which is rotated after every request.
-
timeout¶ Time in seconds to wait before starting the request on the next client. Note that the previous request isn’t cancelled, if it succeeds after the next attempt has been started it is still accepted and returned.
-
max_penalties¶ Max number of penalties a client may receive in the
penalty_time_framebefore being removed from the pool. A penalty is added to a client each time it raises an unexpected error.
-
penalty_time_frame¶ Number of seconds before a penalty expires and no long counts toward a client’s total number of penalties.
-
add_client(client)[source]¶ Add a new client to the pool.
- Parameters
client (
AbstractHTTP) – Client to add.- Raises
ValueError – If the client is already in the pool
- Return type
None
-
remove_client(client)[source]¶ Remove a client from the pool.
- Parameters
client (
AbstractHTTP) – Client to remove.- Raises
ValueError – If the client isn’t in the pool
- Return type
None
-
get_current_client()[source]¶ Get the current http client.
- Return type
- Returns
The current client or
Noneif there are no clients.
-
get_next_client()[source]¶ Move to the next http client and return it.
In reality this returns the next client that seems to be working. (i.e. not closed)
- Return type
- Returns
Next available client.
Noneif no clients are available.
-
async
request(method, path, **kwargs)[source]¶ Perform a request on the one of the clients.
See also
AbstractHTTP.requestfor the documentation.- Raises
PoolEmptyError – If no clients are available
- Return type
-
add_child(emitter)¶ Add a child emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Child emitter to add.- Raises
ValueError – If the child emitter already is a child of this emitter, or the emitter is a child of the child emitter. The second case is only possible if the child emitter is itself an instance of
EmitterABC.- Return type
None
-
async
close()¶ Close all clients in the pool.
- Return type
None
-
emit(event)¶ Emit an event to all observers.
The order in which the observers receive the events isn’t defined and shouldn’t be depended upon.
If a listener raises an error, a
ListenerErrorevent is emitted. If an error occurs while handling theListenerErrorevent, it is ignored.-
event¶ Event object to emit
- Return type
Awaitable[None]- Returns
asyncio.Futurewhich resolves toNoneafter all observers have handled the event.
-
-
has_child(emitter)¶ Check whether the child emitter is a child of the emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Emitter to check.- Return type
- Returns
Whether or not the given emitter is either a direct, or an indirect (child of a child) child of this emitter. Note that every emitter is a child of itself.
-
off(event=None, callback=None)¶ Disable an event listener or remove all listeners from an event.
This can also be used to disable a listener that was added using
oncebefore it is called.To remove all listeners from an event, pass the event selector to the
eventkeyword argument:class MyEvent: ... observable.off(event=MyEvent)
To remove a listener which is listening to all events, pass the listener to the
callbackkeyword argument:def on_any_event(event): print(event) observable.off(callback=on_any_event)
- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to remove the listeners for.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Event listener. This is the same callable that was passed toonoronce.
- Return type
None
-
on(event=None, callback=None)¶ Add a listener to the given event(s).
If you want to listen to all events you can pass the callback to the
callbackkeyword argument:def on_any_event(event): print(event) observable.on(callback=on_any_event)
- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to listen for. The latter case is equivalent to callingonfor each event separately.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Callable to call when the selected events are emitted. It is called with the event instance as its only argument. If it returns anAwaitable(or is a coroutine) it is awaited before the emission “completes” (SeeChildEmitterABC.emit).
- Return type
None
-
once(event=None, callback=None)¶ Add a listener which is only called once.
This is basically the same as adding the listener using
onand then callingoffonce the listener has been called.- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to listen for.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Callable to call when the selected events are emitted. It is called with the event instance as its only argument. If it returns anAwaitable(or is a coroutine) it is awaited before the emission “completes” (SeeChildEmitterABC.emit).
- Return type
None
-
remove_child(emitter)¶ Remove a child emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Child emitter to remove.- Raises
ValueError – If the emitter isn’t a direct child.
- Return type
None
-
async
reset()¶ Reset all underlying clients so they may be used again.
This has the opposite effect of the
closemethod making the clients usable again.- Return type
None
-
subscribe(event=None)¶ Subscribe to the given event(s).
-
class
HTTPPool(clients, *, timeout=None, max_penalties=5, penalty_time_frame=60)[source]¶ Andesite HTTP client pool.
This is just a wrapper around
HTTPPoolBasewhich adds theHTTPInterfacemethods.Please see the documentation of
HTTPPoolBasefor more details.-
add_child(emitter)¶ Add a child emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Child emitter to add.- Raises
ValueError – If the child emitter already is a child of this emitter, or the emitter is a child of the child emitter. The second case is only possible if the child emitter is itself an instance of
EmitterABC.- Return type
None
-
add_client(client)¶ Add a new client to the pool.
- Parameters
client (
AbstractHTTP) – Client to add.- Raises
ValueError – If the client is already in the pool
- Return type
None
-
async
close()¶ Close all clients in the pool.
- Return type
None
-
async
decode_track(track)¶ Get the
TrackInfofrom the encoded track data.Notes
If you find yourself using this method a lot, you might want to use lptrack which can decode and encode the track data locally.
- Parameters
track (
str) – base 64 encoded track data to decode.- Return type
- Returns
TrackInfoof the provided data,Noneif the data is invalid. Note that this method doesn’t raiseHTTPError! If you need theHTTPErrorto be raised, usedecode_tracks.
See also
Please use
decode_tracksif you need to decode multiple encoded strings at once!
-
async
decode_tracks(tracks)¶ Get the
TrackInfofrom multiple encoded track data strings.
-
emit(event)¶ Emit an event to all observers.
The order in which the observers receive the events isn’t defined and shouldn’t be depended upon.
If a listener raises an error, a
ListenerErrorevent is emitted. If an error occurs while handling theListenerErrorevent, it is ignored.-
event¶ Event object to emit
- Return type
Awaitable[None]- Returns
asyncio.Futurewhich resolves toNoneafter all observers have handled the event.
-
-
get_current_client()¶ Get the current http client.
- Return type
- Returns
The current client or
Noneif there are no clients.
-
get_next_client()¶ Move to the next http client and return it.
In reality this returns the next client that seems to be working. (i.e. not closed)
- Return type
- Returns
Next available client.
Noneif no clients are available.
-
async
get_stats()¶ Get the node’s statistics.
-
has_child(emitter)¶ Check whether the child emitter is a child of the emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Emitter to check.- Return type
- Returns
Whether or not the given emitter is either a direct, or an indirect (child of a child) child of this emitter. Note that every emitter is a child of itself.
-
async
load_tracks(identifier)¶ Load tracks.
- Parameters
identifier (
str) – Identifier to load. The identifier isn’t handled in any way so it supports the search syntax for example.- Raises
HTTPError – If Andesite returns an error.
See also
search_tracksto search for a track using a query.- Return type
-
async
load_tracks_safe(uri)¶ Load tracks from url.
This is different from
load_tracksinsofar that it ignores special markers such as “ytsearch:” and treats the given uri as nothing but that.See also
load_tracksto load a track using an identifier.search_tracksto search for a track using a query.- Return type
-
off(event=None, callback=None)¶ Disable an event listener or remove all listeners from an event.
This can also be used to disable a listener that was added using
oncebefore it is called.To remove all listeners from an event, pass the event selector to the
eventkeyword argument:class MyEvent: ... observable.off(event=MyEvent)
To remove a listener which is listening to all events, pass the listener to the
callbackkeyword argument:def on_any_event(event): print(event) observable.off(callback=on_any_event)
- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to remove the listeners for.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Event listener. This is the same callable that was passed toonoronce.
- Return type
None
-
on(event=None, callback=None)¶ Add a listener to the given event(s).
If you want to listen to all events you can pass the callback to the
callbackkeyword argument:def on_any_event(event): print(event) observable.on(callback=on_any_event)
- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to listen for. The latter case is equivalent to callingonfor each event separately.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Callable to call when the selected events are emitted. It is called with the event instance as its only argument. If it returns anAwaitable(or is a coroutine) it is awaited before the emission “completes” (SeeChildEmitterABC.emit).
- Return type
None
-
once(event=None, callback=None)¶ Add a listener which is only called once.
This is basically the same as adding the listener using
onand then callingoffonce the listener has been called.- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to listen for.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Callable to call when the selected events are emitted. It is called with the event instance as its only argument. If it returns anAwaitable(or is a coroutine) it is awaited before the emission “completes” (SeeChildEmitterABC.emit).
- Return type
None
-
remove_child(emitter)¶ Remove a child emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Child emitter to remove.- Raises
ValueError – If the emitter isn’t a direct child.
- Return type
None
-
remove_client(client)¶ Remove a client from the pool.
- Parameters
client (
AbstractHTTP) – Client to remove.- Raises
ValueError – If the client isn’t in the pool
- Return type
None
-
async
request(method, path, **kwargs)¶ Perform a request on the one of the clients.
See also
AbstractHTTP.requestfor the documentation.- Raises
PoolEmptyError – If no clients are available
- Return type
-
async
reset()¶ Reset all underlying clients so they may be used again.
This has the opposite effect of the
closemethod making the clients usable again.- Return type
None
-
async
search_tracks(query, *, searcher=<Searcher.YOUTUBE: 'ytsearch'>)¶ Search tracks.
- Parameters
- Raises
HTTPError – If Andesite returns an error.
Notes
This is a utility method for the
load_tracksmethod. A search query is just an identifier with the format “<searcher>:<query>”.- Return type
-
subscribe(event=None)¶ Subscribe to the given event(s).
-
-
class
ScoringData(pool, client, node_region, region_comparator, guild_ids, guild_id)[source]¶ Data passed to the web socket scoring scoring function.
-
pool¶ - Type
-
client¶ Client to be evaluated
- Type
-
region_comparator¶ Function to compare node region with guild id.
- Type
Optional[RegionGuildComparator]
-
-
default_scoring_function(data)[source]¶ Calculate the default score.
- Return type
- Returns
3-tuple with the first element representing the connection status -1 for closed, 0 for disconnected, and 1 for connected. If the client doesn’t have a
connectedattribute it is still set to 1.The second element is the result of the region comparator or 0 if no region comparator was passed.
The third element is the amount of guilds negative. So it will favour clients with less guilds.
-
class
WebSocketPoolBase(clients, *, state=None, scoring_function=None, region_comparator=None)[source]¶ Base implementation of a web socket pool.
- Parameters
clients (
Iterable[AbstractWebSocket]) – Web socket clients to initialise the pool with.state (
Union[AbstractState,bool,None]) – State handler to use for this pool. The state is used to migrate nodes and defaults to the in-memoryState. You can however disable the state by passingFalse.scoring_function (
Optional[Callable[[ScoringData],Union[Any,Awaitable[Any]]]]) – Scoring function to evaluate clients with. This is a function which takesScoringDataas its only argument and returns a comparable object. The function can be a coroutine. Defaults todefault_scoring_function.region_comparator (
Optional[Callable[[int,Optional[str]],int]]) – Region comparator to use for comparing regions.
The pool uses different clients on a guild to guild level.
If the pool uses a state, all clients within the pool are forced to use the same state as well. Trying to add a client with a state to a pool with a state will result in an error.
-
property
state¶ State handler for the client.
You may manually set this value to a different state handler which implements the
AbstractState. You can also disable state handling by setting it toFalse. Note that this won’t apply the state to the client! You can use theload_player_statemethod do load individual player states.If not state is set the getter either returns the current instance, if it happens to implement
AbstractState, otherwise it returnsNone.- Return type
-
property
scoring_function¶ Scoring function used by the pool.
-
add_child(emitter)¶ Add a child emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Child emitter to add.- Raises
ValueError – If the child emitter already is a child of this emitter, or the emitter is a child of the child emitter. The second case is only possible if the child emitter is itself an instance of
EmitterABC.- Return type
None
-
async
close()¶ Close all clients in the pool.
- Return type
None
-
emit(event)¶ Emit an event to all observers.
The order in which the observers receive the events isn’t defined and shouldn’t be depended upon.
If a listener raises an error, a
ListenerErrorevent is emitted. If an error occurs while handling theListenerErrorevent, it is ignored.-
event¶ Event object to emit
- Return type
Awaitable[None]- Returns
asyncio.Futurewhich resolves toNoneafter all observers have handled the event.
-
-
property
event_target¶ Event target to send events to.
If no event target is set, but the instance is itself an event target, self is set as the new event target and returned, otherwise a new event target is created.
- Return type
Observable[~T]
-
has_child(emitter)¶ Check whether the child emitter is a child of the emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Emitter to check.- Return type
- Returns
Whether or not the given emitter is either a direct, or an indirect (child of a child) child of this emitter. Note that every emitter is a child of itself.
-
async
load_player_state(player_state)¶ Load a player state.
- Parameters
player_state (
AbstractPlayerState) – State to load.- Return type
None
-
off(event=None, callback=None)¶ Disable an event listener or remove all listeners from an event.
This can also be used to disable a listener that was added using
oncebefore it is called.To remove all listeners from an event, pass the event selector to the
eventkeyword argument:class MyEvent: ... observable.off(event=MyEvent)
To remove a listener which is listening to all events, pass the listener to the
callbackkeyword argument:def on_any_event(event): print(event) observable.off(callback=on_any_event)
- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to remove the listeners for.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Event listener. This is the same callable that was passed toonoronce.
- Return type
None
-
on(event=None, callback=None)¶ Add a listener to the given event(s).
If you want to listen to all events you can pass the callback to the
callbackkeyword argument:def on_any_event(event): print(event) observable.on(callback=on_any_event)
- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to listen for. The latter case is equivalent to callingonfor each event separately.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Callable to call when the selected events are emitted. It is called with the event instance as its only argument. If it returns anAwaitable(or is a coroutine) it is awaited before the emission “completes” (SeeChildEmitterABC.emit).
- Return type
None
-
once(event=None, callback=None)¶ Add a listener which is only called once.
This is basically the same as adding the listener using
onand then callingoffonce the listener has been called.- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to listen for.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Callable to call when the selected events are emitted. It is called with the event instance as its only argument. If it returns anAwaitable(or is a coroutine) it is awaited before the emission “completes” (SeeChildEmitterABC.emit).
- Return type
None
-
property
region_comparator¶ Region comparator used to compare guild region with Andesite node region.
-
remove_child(emitter)¶ Remove a child emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Child emitter to remove.- Raises
ValueError – If the emitter isn’t a direct child.
- Return type
None
-
async
reset()¶ Reset all underlying clients so they may be used again.
This has the opposite effect of the
closemethod making the clients usable again.- Return type
None
-
async
send_operation(guild_id, operation)¶ Send a
SendOperation.- Parameters
guild_id (
int) – Target guild idoperation (
SendOperation) – Operation to send
Notes
Using
SendOperationinstances to send messages is slightly less efficient than calling the respectiveWebSocketmethods directly.- Return type
None
-
subscribe(event=None)¶ Subscribe to the given event(s).
-
add_client(client)[source]¶ Add a new client to the pool.
- Parameters
client (
AbstractWebSocket) – Client to add.- Raises
ValueError – If the client is already in the pool or if the client has a state and the pool also has a different state.
- Return type
None
-
remove_client(client)[source]¶ Remove a web socket client from the pool.
- Parameters
client (
AbstractWebSocket) – WebSocket client to remove- Raises
ValueError – If the client isn’t in the pool
Notes
This method removes the client without performing node migration. If you want to properly remove a client use the
pull_clientmethod.- Return type
None
-
async
pull_client(client)[source]¶ Remove a client from the pool and migrate its state to another node.
- Parameters
client (
AbstractWebSocket) – WebSocket client to remove- Raises
ValueError – If the client isn’t in the pool
- Return type
None
-
get_client(guild_id)[source]¶ Get the andesite web socket client which is used for the given guild.
- Return type
-
iter_client_guild_ids()[source]¶ Iterate over all clients with their assigned guild ids.
- Return type
-
async
find_best_client(guild_id)[source]¶ Determine the best client for the given guild.
If there are multiple clients with the same score, a random one is returned.
- Return type
-
async
assign_client(guild_id)[source]¶ Assign a client to the given guild.
If the guild already has a client, it is simply returned.
- Return type
-
get_guild_ids(client)[source]¶ Get the guild ids which the client is assigned to.
- Parameters
client (
AbstractWebSocket) – Client to get guild ids for.- Return type
- Returns
Guild ids set which are assigned to the client. Mutating the returned set won’t affect the internal state, the returned set is a shallow copy of the internal one.
-
async
send(guild_id, op, payload)[source]¶ Send a payload.
The guild_id and op are written to the payload before it is converted to JSON.
If sending the message fails because the connection is closed, the client attempts to connect and then sends the message again.
Regardless of whether the payload was actually sent or not a
RawMsgSendEvent(raw_msg_send) event is dispatched.
-
class
WebSocketPool(clients, *, state=None, scoring_function=None, region_comparator=None)[source]¶ Pool of Andesite web socket connections.
Please refer to
WebSocketPoolBasefor the documentation.-
add_child(emitter)¶ Add a child emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Child emitter to add.- Raises
ValueError – If the child emitter already is a child of this emitter, or the emitter is a child of the child emitter. The second case is only possible if the child emitter is itself an instance of
EmitterABC.- Return type
None
-
add_client(client)¶ Add a new client to the pool.
- Parameters
client (
AbstractWebSocket) – Client to add.- Raises
ValueError – If the client is already in the pool or if the client has a state and the pool also has a different state.
- Return type
None
-
async
assign_client(guild_id)¶ Assign a client to the given guild.
If the guild already has a client, it is simply returned.
- Return type
-
async
close()¶ Close all clients in the pool.
- Return type
None
-
async
destroy(guild_id)¶ Destroy a player.
- Parameters
guild_id (
int) – ID of the guild for which to destroy the player- Return type
None
-
emit(event)¶ Emit an event to all observers.
The order in which the observers receive the events isn’t defined and shouldn’t be depended upon.
If a listener raises an error, a
ListenerErrorevent is emitted. If an error occurs while handling theListenerErrorevent, it is ignored.-
event¶ Event object to emit
- Return type
Awaitable[None]- Returns
asyncio.Futurewhich resolves toNoneafter all observers have handled the event.
-
-
property
event_target¶ Event target to send events to.
If no event target is set, but the instance is itself an event target, self is set as the new event target and returned, otherwise a new event target is created.
- Return type
Observable[~T]
-
async
filters(guild_id, filter_update=None, *, equalizer=None, karaoke=None, timescale=None, tremolo=None, vibrato=None, volume=None, **custom_filters)¶ Configure the filters of a player.
- Parameters
guild_id (
int) – ID of the guild for which to configure the filtersfilter_update (
Optional[FilterMap]) – Instead of specifying the other keyword arguments you may provide aFilterMapoperation which will be used instead.volume (
Optional[VolumeFilter]) – Volume filter settings**custom_filters – Ability to specify additional filters that aren’t supported by the library.
- Return type
None
-
async
find_best_client(guild_id)¶ Determine the best client for the given guild.
If there are multiple clients with the same score, a random one is returned.
- Return type
-
get_client(guild_id)¶ Get the andesite web socket client which is used for the given guild.
- Return type
-
get_guild_ids(client)¶ Get the guild ids which the client is assigned to.
- Parameters
client (
AbstractWebSocket) – Client to get guild ids for.- Return type
- Returns
Guild ids set which are assigned to the client. Mutating the returned set won’t affect the internal state, the returned set is a shallow copy of the internal one.
-
async
get_player(guild_id)¶ Get the player.
-
async
get_stats(guild_id)¶ Get the Andesite stats.
-
has_child(emitter)¶ Check whether the child emitter is a child of the emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Emitter to check.- Return type
- Returns
Whether or not the given emitter is either a direct, or an indirect (child of a child) child of this emitter. Note that every emitter is a child of itself.
-
iter_client_guild_ids()¶ Iterate over all clients with their assigned guild ids.
- Return type
-
async
load_player_state(player_state)¶ Load a player state.
- Parameters
player_state (
AbstractPlayerState) – State to load.- Return type
None
-
async
mixer(guild_id, enable=None, **players)¶ Configure the mixer player.
-
off(event=None, callback=None)¶ Disable an event listener or remove all listeners from an event.
This can also be used to disable a listener that was added using
oncebefore it is called.To remove all listeners from an event, pass the event selector to the
eventkeyword argument:class MyEvent: ... observable.off(event=MyEvent)
To remove a listener which is listening to all events, pass the listener to the
callbackkeyword argument:def on_any_event(event): print(event) observable.off(callback=on_any_event)
- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to remove the listeners for.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Event listener. This is the same callable that was passed toonoronce.
- Return type
None
-
on(event=None, callback=None)¶ Add a listener to the given event(s).
If you want to listen to all events you can pass the callback to the
callbackkeyword argument:def on_any_event(event): print(event) observable.on(callback=on_any_event)
- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to listen for. The latter case is equivalent to callingonfor each event separately.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Callable to call when the selected events are emitted. It is called with the event instance as its only argument. If it returns anAwaitable(or is a coroutine) it is awaited before the emission “completes” (SeeChildEmitterABC.emit).
- Return type
None
-
once(event=None, callback=None)¶ Add a listener which is only called once.
This is basically the same as adding the listener using
onand then callingoffonce the listener has been called.- Parameters
event (
Union[Type[~T],Tuple[Type[~T], …],None]) – Event selector. This can be the type of a single event or a tuple of multiple events to listen for.callback (
Optional[Callable[[~T],Union[None,Awaitable[None]]]]) – Callable to call when the selected events are emitted. It is called with the event instance as its only argument. If it returns anAwaitable(or is a coroutine) it is awaited before the emission “completes” (SeeChildEmitterABC.emit).
- Return type
None
-
async
pause(guild_id, pause)¶ Pause a player.
If the player is already paused or unpaused, this is a no-op.
-
async
ping(guild_id)¶ Ping the Andesite server.
-
async
play(guild_id, track, *, start=None, end=None, pause=None, volume=None, no_replace=False)¶ Play a track on the guild.
Instead of providing all the fields you can also pass a
Playoperation.- Parameters
guild_id (
int) – ID of the guild for which to playtrack (
Union[str,Play]) – Either aPlayoperation or a base64 encoded lavaplayer track. If you pass aPlayoperation you must not set any of the keyword arguments.start (
Optional[float]) – timestamp, in seconds, to start the trackend (
Optional[float]) – timestamp, in seconds, to end the trackno_replace (
bool) – if True and a track is already playing/paused, this command is ignored (Default: False)
- Return type
None
-
async
pull_client(client)¶ Remove a client from the pool and migrate its state to another node.
- Parameters
client (
AbstractWebSocket) – WebSocket client to remove- Raises
ValueError – If the client isn’t in the pool
- Return type
None
-
property
region_comparator¶ Region comparator used to compare guild region with Andesite node region.
-
remove_child(emitter)¶ Remove a child emitter.
- Parameters
emitter (
ChildEmitterABC[~T]) – Child emitter to remove.- Raises
ValueError – If the emitter isn’t a direct child.
- Return type
None
-
remove_client(client)¶ Remove a web socket client from the pool.
- Parameters
client (
AbstractWebSocket) – WebSocket client to remove- Raises
ValueError – If the client isn’t in the pool
Notes
This method removes the client without performing node migration. If you want to properly remove a client use the
pull_clientmethod.- Return type
None
-
async
reset()¶ Reset all underlying clients so they may be used again.
This has the opposite effect of the
closemethod making the clients usable again.- Return type
None
-
property
scoring_function¶ Scoring function used by the pool.
-
async
seek(guild_id, position)¶ Seek to a position.
-
async
send(guild_id, op, payload)¶ Send a payload.
The guild_id and op are written to the payload before it is converted to JSON.
If sending the message fails because the connection is closed, the client attempts to connect and then sends the message again.
Regardless of whether the payload was actually sent or not a
RawMsgSendEvent(raw_msg_send) event is dispatched.
-
async
send_operation(guild_id, operation)¶ Send a
SendOperation.- Parameters
guild_id (
int) – Target guild idoperation (
SendOperation) – Operation to send
Notes
Using
SendOperationinstances to send messages is slightly less efficient than calling the respectiveWebSocketmethods directly.- Return type
None
-
property
state¶ State handler for the client.
You may manually set this value to a different state handler which implements the
AbstractState. You can also disable state handling by setting it toFalse. Note that this won’t apply the state to the client! You can use theload_player_statemethod do load individual player states.If not state is set the getter either returns the current instance, if it happens to implement
AbstractState, otherwise it returnsNone.- Return type
-
async
stop(guild_id)¶ Stop a player.
- Parameters
guild_id (
int) – ID of the guild for which to stop- Return type
None
-
subscribe(event=None)¶ Subscribe to the given event(s).
-
async
update(guild_id, update=None, *, pause=None, position=None, volume=None, filters=None)¶ Send an update.
You may either provide the given keyword arguments, or pass an
Updateoperation which will be used instead.- Parameters
guild_id (
int) – ID of the guild for which to update.update (
Optional[Update]) – You can provide anUpdateoperation instead of the keyword arguments.position (
Optional[float]) – timestamp to set the current track to, in secondsfilters (
Union[FilterMap,Dict[str,Union[Filter,Dict[str,Any]]],None]) – configuration for the filters
- Return type
None
-
async
voice_server_update(guild_id, session_id, event)¶ Provide a voice server update.
- Parameters
Notes
If you wish to send a
VoiceServerUpdateoperation, please use thesend_operationmethod directly.- Return type
None
-
-
create_pool(http_nodes, web_socket_nodes, *, user_id, state=None, http_pool_kwargs=None, web_socket_pool_kwargs=None)[source]¶ Create an
Clientwith client pools.Uses
HTTPBaseandWebSocketBasewhich are contained inHTTPPoolBaseandWebSocketPoolBasepools respectively.- Parameters
http_nodes (
Iterable[Tuple[Union[str,URL],Optional[str]]]) – Tuples of [uri, password] for each REST node to connect to.web_socket_nodes (
Iterable[Tuple[Union[str,URL],Optional[str]]]) – Tuples of [uri, password] for each WebSocket node to connect to.user_id (
int) – Bot’s user id.state (
Union[AbstractState,bool,None]) – State handler to use for the pools. Defaults toState, because a state handler is required for node migration to work. You can passFalseto disable state handling though.http_pool_kwargs (
Optional[Mapping[str,Any]]) – Additional keyword arguments to pass to the http pool constructor.web_socket_pool_kwargs (
Optional[Mapping[str,Any]]) – Additional keyword arguments to pass to the web socket pool constructor.
- Return type
- Returns
A combined Andesite client operation on an http pool and a web socket pool.