From 6491f274fbaf44363bcfde427fe97c78cfcae569 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Sat, 22 Dec 2018 20:21:31 +0800 Subject: [PATCH] Add "switch" builtin command --- none/message.py | 21 +++++++++++---------- none/plugins/base.py | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/none/message.py b/none/message.py index ce99298d..d9cabcd9 100644 --- a/none/message.py +++ b/none/message.py @@ -26,17 +26,18 @@ async def handle_message(bot: NoneBot, ctx: Context_T) -> None: if coros: await asyncio.wait(coros) - if ctx['message_type'] != 'private': - # group or discuss - ctx['to_me'] = False - first_message_seg = ctx['message'][0] - if first_message_seg == MessageSegment.at(ctx['self_id']): + if 'to_me' not in ctx: + if ctx['message_type'] != 'private': + # group or discuss + ctx['to_me'] = False + first_message_seg = ctx['message'][0] + if first_message_seg == MessageSegment.at(ctx['self_id']): + ctx['to_me'] = True + del ctx['message'][0] + if not ctx['message']: + ctx['message'].append(MessageSegment.text('')) + else: ctx['to_me'] = True - del ctx['message'][0] - if not ctx['message']: - ctx['message'].append(MessageSegment.text('')) - else: - ctx['to_me'] = True while True: try: diff --git a/none/plugins/base.py b/none/plugins/base.py index 7ef4b6ad..5342a8df 100644 --- a/none/plugins/base.py +++ b/none/plugins/base.py @@ -1,5 +1,8 @@ +import asyncio + from none import on_command, CommandSession, permission as perm -from none.message import unescape +from none.command import kill_current_session +from none.message import unescape, Message, handle_message @on_command('echo') @@ -11,3 +14,15 @@ async def echo(session: CommandSession): async def _(session: CommandSession): await session.send( unescape(session.get_optional('message') or session.current_arg)) + + +@on_command('switch', privileged=True) +async def _(session: CommandSession): + kill_current_session(session.bot, session.ctx) + + msg = Message(session.current_arg) + await session.send(msg) + + ctx = session.ctx.copy() + ctx['message'] = msg + asyncio.ensure_future(handle_message(session.bot, ctx))