From 89788474d67ec964c26a5c506b3c1da3bae0cc8a Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Wed, 27 Jun 2018 22:50:01 +0800 Subject: [PATCH] Wow --- none/__init__.py | 16 +++++---- none/helpers.py | 21 +----------- none/{logger.py => log.py} | 0 none/message.py | 2 +- none/notice.py | 28 ---------------- none/notice_request.py | 63 +++++++++++++++++++++++++++++++++++ none/plugins/base.py | 8 ++--- none_demo/plugins/greeting.py | 5 ++- 8 files changed, 79 insertions(+), 64 deletions(-) rename none/{logger.py => log.py} (100%) delete mode 100644 none/notice.py create mode 100644 none/notice_request.py diff --git a/none/__init__.py b/none/__init__.py index d2bf2b1d..057be8e6 100644 --- a/none/__init__.py +++ b/none/__init__.py @@ -9,8 +9,8 @@ from aiocqhttp import CQHttp from aiocqhttp.message import Message from .message import handle_message -from .notice import handle_notice -from .logger import logger +from .notice_request import handle_notice_or_request +from .log import logger def create_bot(config_object: Any = None): @@ -34,12 +34,11 @@ def create_bot(config_object: Any = None): @bot.on_notice async def _(ctx): - asyncio.ensure_future(handle_notice(bot, ctx)) + asyncio.ensure_future(handle_notice_or_request(bot, ctx)) @bot.on_request async def _(ctx): - pass - # asyncio.ensure_future(plugin.handle_request(bot, ctx)) + asyncio.ensure_future(handle_notice_or_request(bot, ctx)) return bot @@ -75,5 +74,8 @@ def load_builtin_plugins(): load_plugins(plugin_dir, 'none.plugins') -from .command import on_command -from .notice import on_notice +from .command import on_command, CommandSession +from .notice_request import ( + on_notice, NoticeSession, + on_request, RequestSession, +) diff --git a/none/helpers.py b/none/helpers.py index a8b580e5..326198e9 100644 --- a/none/helpers.py +++ b/none/helpers.py @@ -1,7 +1,6 @@ -from typing import Dict, Any, Union, List, Sequence, Callable, Optional +from typing import Dict, Any, Union, List, Sequence, Callable from aiocqhttp import CQHttp, Error as CQHttpError -from aiocqhttp.bus import EventBus from . import expression @@ -42,21 +41,3 @@ async def send_expr(bot: CQHttp, ctx: Dict[str, Any], expr: Union[str, Sequence[str], Callable], **kwargs): return await send(bot, ctx, expression.render(expr, **kwargs)) - - -def make_event_deco(post_type: str, bus: EventBus) -> 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 diff --git a/none/logger.py b/none/log.py similarity index 100% rename from none/logger.py rename to none/log.py diff --git a/none/message.py b/none/message.py index d1827966..c5a49b65 100644 --- a/none/message.py +++ b/none/message.py @@ -4,7 +4,7 @@ from aiocqhttp import CQHttp from aiocqhttp.message import MessageSegment from .command import handle_command -from .logger import logger +from .log import logger async def handle_message(bot: CQHttp, ctx: Dict[str, Any]) -> None: diff --git a/none/notice.py b/none/notice.py deleted file mode 100644 index 809fa19b..00000000 --- a/none/notice.py +++ /dev/null @@ -1,28 +0,0 @@ -from typing import Dict, Any - -from aiocqhttp import CQHttp -from aiocqhttp.bus import EventBus - -from .session import BaseSession -from .helpers import make_event_deco -from .logger import logger - -_bus = EventBus() -on_notice = make_event_deco('notice', _bus) - - -class NoticeSession(BaseSession): - __slots__ = () - - def __init__(self, bot: CQHttp, ctx: Dict[str, Any]): - super().__init__(bot, ctx) - - -async def handle_notice(bot: CQHttp, ctx: Dict[str, Any]) -> None: - event = f'notice.{ctx["notice_type"]}' - if ctx.get('sub_type'): - event += f'.{ctx["sub_type"]}' - - session = NoticeSession(bot, ctx) - logger.debug(f'Emitting event: {event}') - await _bus.emit(event, session) diff --git a/none/notice_request.py b/none/notice_request.py new file mode 100644 index 00000000..63937bdb --- /dev/null +++ b/none/notice_request.py @@ -0,0 +1,63 @@ +from typing import Dict, Any, Optional, Callable, Union + +from aiocqhttp import CQHttp +from aiocqhttp.bus import EventBus + +from .session import BaseSession +from .log import logger + +_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__ = () + + def __init__(self, bot: CQHttp, ctx: Dict[str, Any]): + super().__init__(bot, ctx) + + +class RequestSession(BaseSession): + __slots__ = () + + def __init__(self, bot: CQHttp, ctx: Dict[str, Any]): + super().__init__(bot, ctx) + + # TODO: 添加 approve、deny 等方法 + + +async def handle_notice_or_request(bot: CQHttp, ctx: Dict[str, Any]) -> None: + 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': + session = NoticeSession(bot, ctx) + else: + session = RequestSession(bot, ctx) + + logger.debug(f'Emitting event: {event}') + await _bus.emit(event, session) diff --git a/none/plugins/base.py b/none/plugins/base.py index 71a2806a..5bb83f36 100644 --- a/none/plugins/base.py +++ b/none/plugins/base.py @@ -1,15 +1,13 @@ from aiocqhttp.message import unescape -import none -from none import permissions as perm -from none.command import CommandSession +from none import on_command, CommandSession, permissions as perm -@none.on_command('echo') +@on_command('echo') async def echo(session: CommandSession): await session.send(session.current_arg) -@none.on_command('say', permission=perm.SUPERUSER) +@on_command('say', permission=perm.SUPERUSER) async def _(session: CommandSession): await session.send(unescape(session.current_arg)) diff --git a/none_demo/plugins/greeting.py b/none_demo/plugins/greeting.py index a05cd473..a90ce170 100644 --- a/none_demo/plugins/greeting.py +++ b/none_demo/plugins/greeting.py @@ -1,7 +1,6 @@ from aiocqhttp import Error as CQHttpError -import none -from none.notice import NoticeSession +from none import on_notice, NoticeSession GROUP_GREETING = ( '欢迎新同学 {name}[]![CQ:face,id=63][CQ:face,id=63][CQ:face,id=63]', @@ -11,7 +10,7 @@ GROUP_GREETING = ( ) -@none.on_notice('group_increase') +@on_notice('group_increase') async def _(session: NoticeSession): try: info = await session.bot.get_group_member_info(**session.ctx,