コード例 #1
0
class EventManager:
    """INTERNAL API"""
    bus = EventBus()

    @classmethod
    def add_event_handler(cls, handler: EventHandler) -> None:
        for event in handler.events:
            cls.bus.subscribe(event, handler.func)

    @classmethod
    def remove_event_handler(cls, handler: EventHandler) -> None:
        for event in handler.events:
            cls.bus.unsubscribe(event, handler.func)

    @classmethod
    def switch_event_handler_global(cls,
                                    handler: EventHandler,
                                    state: Optional[bool] = None) -> None:
        for event in handler.events:
            if handler.func in cls.bus._subscribers[event] and not state:
                cls.bus.unsubscribe(event, handler.func)
            elif handler.func not in cls.bus._subscribers[
                    event] and state is not False:
                cls.bus.subscribe(event, handler.func)
コード例 #2
0
from typing import Dict, Any, Optional, Callable, Union

from aiocqhttp import Error as CQHttpError
from aiocqhttp.bus import EventBus

from . import NoneBot
from .log import logger
from .session import BaseSession

_bus = EventBus()


def _make_event_deco(post_type: str) -> Callable:
    def deco_deco(arg: Optional[Union[str, Callable]] = None,
                  *events: str) -> Callable:
        def deco(func: Callable) -> Callable:
            if isinstance(arg, str):
                for e in [arg] + list(events):
                    _bus.subscribe(f'{post_type}.{e}', func)
            else:
                _bus.subscribe(post_type, func)
            return func

        if isinstance(arg, Callable):
            return deco(arg)
        return deco

    return deco_deco


on_notice = _make_event_deco('notice')