Handlers¶
Base Handler¶
-
class
cactusbot.handler.
Handler
¶ Parent class to all event handlers.
Examples
>>> class TestingHandler: ... def on_message(self, packet): ... self.logger.info(packet) ...
Handler Controller¶
-
class
cactusbot.handler.
Handlers
(*handlers)¶ Evented controller for individual handlers.
For a method to have the ability to be used as an event handler, it must be prefixed with on_, and then followed by the event name. This method gets a single argument of packet.
Packet can be the following types:
Event Packet Type message MessagePacket
follow EventPacket
subscribe EventPacket
host EventPacket
join EventPacket
leave EventPacket
repeat MessagePacket
config Packet
username_update Packet
Other events will be of the packet type Packet.
Parameters: handlers ( Handler
) – Tuple of handlers that contain events.Examples
>>> class TestingHandler(Handler): ... async def on_message(self, packet): ... self.logger.info(packet) ... >>> handlers = Handlers(TestingHandler) >>> async def handle(): ... await handlers.handle("message", MessagePacket("Message!")) ...
-
handle
(event, packet)¶ Handle incoming data.
Parameters: - event (
str
) – The event that should be handled - packet (
Packet
) – The packet to send to the handler function
Examples
>>> async def handle(): ... await handlers.handle("message", MessagePacket("Message!"))
- event (
-
translate
(packet, handler)¶ Translate
Handler
responses toPacket
.Parameters: - packet (
Packet
,str
,tuple
,list
,StopIteration
, orNone
) –- The packet to turn the handler response into
Packet
is immediately yielded.str
is converted into a text field in aMessagePacket
.tuple
orlist
is iterated over, passing each item throughtranslate()
again.StopIteration
signifies that no future packets should be yielded, stopping the chain.None
is ignored, and is never yielded.
- handler (
Handler
) – The handler response to turn into a packet
Examples
>>> handlers = Handlers() >>> translated = handlers.translate("Hello!", Handler()) >>> [(item.__class__.__name__, item.text) for item in translated] [('MessagePacket', 'Hello!')]
>>> handlers = Handlers() >>> translated = handlers.translate(["Potato?", "Potato!"], Handler()) >>> [(item.__class__.__name__, item.text) for item in translated] [('MessagePacket', 'Potato?'), ('MessagePacket', 'Potato!')]
>>> handlers = Handlers() >>> translated = handlers.translate( ... ["Stop spamming.", StopIteration, "Nice message!"], ... Handler() ... ) >>> [(item.__class__.__name__, item.text) for item in translated] [('MessagePacket', 'Stop spamming.')]
- packet (
-