Skip to content
Snippets Groups Projects
Commit ef70591d authored by Iori Ichinose's avatar Iori Ichinose :speech_balloon:
Browse files

add pipelines

parent 745db10d
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ from nonebot.adapters import Bot, Event
import random
ping = on_command('ping')
echo = on_command('echo', block=True)
replies = ['pong!', '我还在睡喵...', 'Zzzz...']
......@@ -11,3 +12,14 @@ replies = ['pong!', '我还在睡喵...', 'Zzzz...']
@ping.handle()
async def ping_handler(bot: Bot, event: Event, state: T_State):
await ping.finish(random.choice(replies))
@echo.handle()
async def echo_handler(bot: Bot, event: Event, state: T_State):
msg = event.get_plaintext().strip()
state['_send'] = msg
await echo.finish()
@echo.got('_send')
async def _(bot: Bot, event: Event, state: T_State):
pass
\ No newline at end of file
......@@ -32,8 +32,15 @@ async def poke_handler(bot: Bot, event: Event) -> Optional[MessageSegment]:
return CQGen.poke(configs['user_id'])
async def approve_handler(bot: Bot, event: Event) -> Optional[Message]:
configs = event.dict()
if configs['user_id'] == configs['self_id']:
return None
return CQGen.at(configs['user_id']) + ' 欢迎新椰叶!'
events = {
'poke': poke_handler,
'ban': ban_handler,
'lift_ban': ban_handler,
'approve': approve_handler
}
......@@ -2,4 +2,4 @@ from .recorder import recorder
from .face_table import FACT_TABLE
from .cq_messages import CQGen, CQParse
from .message import Message
import commands
from . import commands
import re
from nonebot.message import event_preprocessor, event_postprocessor
from nonebot.adapters.cqhttp import Bot, Event, MessageEvent, Message
from nonebot.message import event_preprocessor, run_postprocessor
from nonebot.matcher import Matcher
from nonebot.typing import T_State
from .pipeline import parser
from typing import Optional
re_pipeline = re.compile(r'(.*)[|](.*)')
......@@ -11,20 +14,22 @@ re_pipeline = re.compile(r'(.*)[|](.*)')
async def cmd_grep(bot: Bot, event: Event, state: T_State):
if not isinstance(event, MessageEvent):
return
text = event.get_plaintext()
text = event.dict()['raw_message']
if m := re_pipeline.match(text):
msg, command = m.group(0).strip(), m.group(1).strip()
msg, command = m.group(1).strip(), m.group(2).strip()
print('msg =', msg, 'command =', command)
event.message = Message(msg)
event.raw_message = msg
state['_command'] = command
state['_send'] = msg
@event_postprocessor
async def sender(bot: Bot, event: Event, state: T_State):
if '_send' not in state:
@run_postprocessor
async def sender(matcher: Matcher, exception: Optional[Exception], bot: Bot, event: Event, state: T_State):
if '_send' not in matcher.state:
return
send = state['_send']
if '_command' in state:
pass
send = matcher.state['_send']
if type(send) is str and '_command' in matcher.state:
c = matcher.state['_command'].split()
command, params = c[0], c[1:]
send = parser(send, command, params)
await bot.send(event, send)
def pipeline_grep(text: str, _keyword: list[str]):
keyword = _keyword[0]
return text.replace(keyword, f'<{keyword}>')
commands = {
'grep': {
'params': 1,
'handler': pipeline_grep,
},
'echo': {
'params': 0,
'handler': None,
},
'hoshii': {
'params': 2,
'handler': None,
}
}
def parser(text: str, command: str, params: list[str]):
if command not in commands:
return f'command: {command} not found'
if len(params) != commands[command]['params']:
return f'{command}: invalid params number.'
return commands[command]['handler'](text, params)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment