-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
dispatchers.py
45 lines (35 loc) · 1.48 KB
/
dispatchers.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
from __future__ import annotations
from inspect import isclass
from typing import TYPE_CHECKING
from graia.broadcast.entities.dispatcher import BaseDispatcher
from avilla.core._runtime import cx_protocol
from avilla.core.account import BaseAccount
from avilla.core.context import Context
from avilla.core.event import AvillaEvent
from avilla.core.protocol import BaseProtocol
if TYPE_CHECKING:
from graia.broadcast.interfaces.dispatcher import DispatcherInterface
from avilla.core.application import Avilla
class AvillaBuiltinDispatcher(BaseDispatcher):
avilla: Avilla
def __init__(self, avilla: Avilla) -> None:
self.avilla = avilla
async def catch(self, interface: DispatcherInterface[AvillaEvent]):
from avilla.core.application import Avilla
if interface.annotation is Avilla:
return self.avilla
if interface.annotation in self.avilla._protocol_map:
return self.avilla._protocol_map[interface.annotation]
if (
isclass(interface.annotation)
and issubclass(interface.annotation, BaseProtocol)
and isinstance(cx_protocol.get(None), interface.annotation)
):
return cx_protocol.get(None)
if (
isinstance(interface.event, AvillaEvent)
and isclass(interface.annotation)
and issubclass(interface.annotation, BaseAccount)
):
cx: Context = interface.local_storage["avilla_context"]
return cx.account