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

add grep

parent 41634e60
No related branches found
No related tags found
No related merge requests found
......@@ -7,9 +7,12 @@ from nonebot.permission import SUPERUSER
from src.utils.recorder import recorder
from src.utils.message import Message
from .grep import at_re, count
record = on_message(priority=5)
log = on_command('log', permission=SUPERUSER, priority=1, block=True)
rank = on_command('rank', priority=1, block=True)
grep = on_command('grep', priority=1, block=True)
@record.handle()
......@@ -71,3 +74,23 @@ async def handle_count(bot: Bot, event: Event, state: T_State):
async def handle_rank(bot: Bot, event: Event, state: T_State):
group_id = event.dict()['group_id']
await rank.finish(recorder.get_recent_rank(group_id))
@grep.handle()
async def handle_grep(bot: Bot, event: Event, state: T_State):
group_id = event.dict()['group_id']
text = event.dict()['raw_message']
print(text)
if m := at_re.findall(text):
user_id = int(m[0])
else:
await grep.finish()
keyword = text.split(maxsplit=2)[-1]
state['args'] = (user_id, group_id, keyword)
@grep.got('args')
async def handle_args(bot: Bot, event: Event, state: T_State):
uid, gid, keyword = state['args']
print(state['args'])
await grep.finish(count(gid, uid, keyword))
from src.utils.recorder import recorder
import re
import random
at_re = re.compile(r'\[CQ:at,qq=(\d+)\]')
def count(gid: int, uid: int, keyword: str) -> str:
group_id = str(gid)
user_id = str(uid)
messages = recorder.messages
if group_id not in messages or user_id not in messages[group_id]:
return 'grep: no matches found'
greped = [m for m in messages[group_id][user_id] if keyword in m]
count = len(greped)
if count != 0:
ret = [f'您在本群的语料中共出现{count}{keyword}']
count = min(5, count)
ret += random.sample(greped, k=count)
return '\n'.join(ret)
else:
return 'grep: no matches found'
\ No newline at end of file
......@@ -117,8 +117,6 @@ class Recorder:
for user, count in sorted(
ret.items(), key=lambda x: x[1], reverse=True)[:10]:
strs.append(f'{user}{count}')
print(messages)
print(strs)
return '\n'.join(strs)
......
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