2018-06-27 22:50:01 +08:00
|
|
|
from typing import Dict, Any, Optional, Callable, Union
|
|
|
|
|
2018-07-04 09:28:31 +08:00
|
|
|
from aiocqhttp import Error as CQHttpError
|
2018-06-27 22:50:01 +08:00
|
|
|
from aiocqhttp.bus import EventBus
|
|
|
|
|
2018-07-04 09:28:31 +08:00
|
|
|
from . import NoneBot
|
2018-06-27 22:50:01 +08:00
|
|
|
from .log import logger
|
2018-07-02 16:54:29 +08:00
|
|
|
from .session import BaseSession
|
2018-06-27 22:50:01 +08:00
|
|
|
|
|
|
|
_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')
|
|
|
|
on_request = _make_event_deco('request')
|
|
|
|
|
|
|
|
|
|
|
|
class NoticeSession(BaseSession):
|
|
|
|
__slots__ = ()
|
|
|
|
|
2018-07-04 09:28:31 +08:00
|
|
|
def __init__(self, bot: NoneBot, ctx: Dict[str, Any]):
|
2018-06-27 22:50:01 +08:00
|
|
|
super().__init__(bot, ctx)
|
|
|
|
|
|
|
|
|
|
|
|
class RequestSession(BaseSession):
|
|
|
|
__slots__ = ()
|
|
|
|
|
2018-07-04 09:28:31 +08:00
|
|
|
def __init__(self, bot: NoneBot, ctx: Dict[str, Any]):
|
2018-06-27 22:50:01 +08:00
|
|
|
super().__init__(bot, ctx)
|
|
|
|
|
2018-06-30 18:26:48 +08:00
|
|
|
async def approve(self, remark: str = ''):
|
|
|
|
try:
|
2018-07-02 23:29:37 +08:00
|
|
|
await self.bot.call_action(
|
|
|
|
action='.handle_quick_operation_async',
|
|
|
|
self_id=self.ctx.get('self_id'),
|
|
|
|
context=self.ctx,
|
|
|
|
operation={'approve': True, 'remark': remark}
|
|
|
|
)
|
2018-06-30 18:26:48 +08:00
|
|
|
except CQHttpError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def reject(self, reason: str = ''):
|
|
|
|
try:
|
2018-07-02 23:29:37 +08:00
|
|
|
await self.bot.call_action(
|
|
|
|
action='.handle_quick_operation_async',
|
|
|
|
self_id=self.ctx.get('self_id'),
|
|
|
|
context=self.ctx,
|
|
|
|
operation={'approve': False, 'reason': reason}
|
|
|
|
)
|
2018-06-30 18:26:48 +08:00
|
|
|
except CQHttpError:
|
|
|
|
pass
|
2018-06-27 22:50:01 +08:00
|
|
|
|
|
|
|
|
2018-07-04 09:28:31 +08:00
|
|
|
async def handle_notice_or_request(bot: NoneBot, ctx: Dict[str, Any]) -> None:
|
2018-06-27 22:50:01 +08:00
|
|
|
post_type = ctx['post_type'] # "notice" or "request"
|
|
|
|
detail_type = ctx[f'{post_type}_type']
|
|
|
|
event = f'{post_type}.{detail_type}'
|
|
|
|
if ctx.get('sub_type'):
|
|
|
|
event += f'.{ctx["sub_type"]}'
|
|
|
|
|
|
|
|
if post_type == 'notice':
|
2018-07-21 00:46:34 +08:00
|
|
|
_log_notice(ctx)
|
2018-06-27 22:50:01 +08:00
|
|
|
session = NoticeSession(bot, ctx)
|
2018-07-01 20:01:05 +08:00
|
|
|
else: # must be 'request'
|
2018-07-21 00:46:34 +08:00
|
|
|
_log_request(ctx)
|
2018-06-27 22:50:01 +08:00
|
|
|
session = RequestSession(bot, ctx)
|
|
|
|
|
|
|
|
logger.debug(f'Emitting event: {event}')
|
|
|
|
await _bus.emit(event, session)
|
2018-07-21 00:46:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _log_notice(ctx: Dict[str, Any]) -> None:
|
|
|
|
logger.info(f'Notice: {ctx}')
|
|
|
|
|
|
|
|
|
|
|
|
def _log_request(ctx: Dict[str, Any]) -> None:
|
|
|
|
logger.info(f'Request: {ctx}')
|