berconpy.EventDispatcher¶
- class berconpy.EventDispatcher¶
Bases:
objectProvides an event handler system for use with asyncio.
To listen for one of the following events, use the
add_listener()method, thelisten()decorator, or the type-safeon_*decorators:dispatch = EventDispatcher() dispatch.add_listener("on_login", event) @dispatch.listen() async def on_login(): ... @dispatch.listen("on_login") async def my_listener(): ... @dispatch.on_login async def my_listener(): ...
Methods
add_listener(event, func)Adds a listener for a given event, e.g.
"on_login".listen([event])A decorator shorthand to add a listener for a given event, e.g.
"on_login".remove_listener(event, func)Removes a listener from a given event, e.g.
"on_login".wait_for(event, *[, check, timeout])Waits for a specific event to occur and returns the result.
Attributes
on_command(response, /)Fired after receiving any command response from the server.
on_login()Fired after a successful login to the server.
on_message(message, /)Fired for messages sent by the server, e.g. player connections.
on_raw_event(packet, /)Fired for every parsable packet received by the server.
- add_listener(event, func)¶
Adds a listener for a given event, e.g.
"on_login".
- listen(event=None)¶
A decorator shorthand to add a listener for a given event, e.g.
"on_login".
- remove_listener(event, func)¶
Removes a listener from a given event, e.g.
"on_login".This method should be a no-op if the given event and function does not match any registered listener.
- async wait_for(event, *, check=None, timeout=None)¶
Waits for a specific event to occur and returns the result.
This allows handling one-shot events in a simpler manner than with persistent listeners.
- Parameters:
event (
str) – The event to wait for, e.g."login"and"on_login".check (
Optional[Callable[...,Union[Any,Awaitable[Any]]]]) – An optional predicate function to use as a filter. This can be either a regular or an asynchronous function. The function should accept the same arguments that the event normally takes.timeout (
Union[float,int,None]) – An optional timeout for the function. If this is provided and the function times out, anasyncio.TimeoutErroris raised.
- Returns:
The same arguments received in the event.
- Raises:
asyncio.TimeoutError – The timeout was exceeded while waiting.
- on_command(response, /)¶
Fired after receiving any command response from the server.
This should only be used for debugging purposes as the
send_command()method already returns the server’s response.
- on_message(message, /)¶
Fired for messages sent by the server, e.g. player connections.
More specific events such as
on_admin_login()are dispatched from this event.- Parameters:
response – The message that was sent by the server.
- Return type:
- on_raw_event(packet, /)¶
Fired for every parsable packet received by the server.
- Parameters:
packet (
ServerPacket) – The packet that was received.- Return type: