This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflow.py
53 lines (41 loc) · 1.58 KB
/
flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Application Flow APIs.
:Copyright: Copyright (C) 2021-2021 cscs181
:License: AGPL-3.0 or later. See `LICENSE`_ for detail.
.. _LICENSE:
https://github.com/cscs181/CAI/blob/master/LICENSE
"""
from typing import TYPE_CHECKING, Callable, Awaitable
from cai.log import logger
from cai.client import HANDLERS, Event, Session, Command, IncomingPacket
from .base import BaseAPI
if TYPE_CHECKING:
from .client import Client as Client_T
class Events(BaseAPI):
def add_event_listener(
self, listener: Callable[["Client_T", Event], Awaitable[None]]
):
"""Add event listener.
Args:
listener (Callable[[Session, Event], Awaitable[None]]): Event listener.
"""
self.session.add_event_listener(lambda _, e: listener(self, e)) # type: ignore
def register_packet_handler(
self,
cmd: str,
packet_handler: Callable[[Session, IncomingPacket], Awaitable[Command]],
) -> None:
"""Register custom packet handler.
Note:
This function is a low-level api to mock default behavior.
Be aware of what you are doing!
Args:
cmd (str): Command name of the packet.
packet_handler (Callable[[Session, IncomingPacket], Awaitable[Command]]):
Asynchronous packet handler. A :obj:`~cai.session.command.Command`
object should be returned.
"""
if cmd in HANDLERS:
logger.warning(
f"You are overwriting an existing handler for command {cmd}!"
)
HANDLERS[cmd] = packet_handler