mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 03:57:00 +08:00
Wow
This commit is contained in:
parent
ae2d177d5a
commit
89788474d6
@ -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,
|
||||
)
|
||||
|
@ -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
|
||||
|
@ -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:
|
||||
|
@ -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)
|
63
none/notice_request.py
Normal file
63
none/notice_request.py
Normal file
@ -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)
|
@ -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))
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user