🐛 修复更新会卡住和群聊学习报错问题

This commit is contained in:
CMHopeSunshine 2022-09-12 11:35:46 +08:00
parent 9545983396
commit 8c711b4302
5 changed files with 46 additions and 18 deletions

View File

@ -1,5 +1,5 @@
import os
import subprocess
import asyncio
import sys
from pathlib import Path
@ -7,11 +7,15 @@ from nonebot import on_command, get_bot
from nonebot.permission import SUPERUSER
from nonebot.plugin import PluginMetadata
from nonebot.rule import to_me
from nonebot.adapters.onebot.v11 import MessageEvent
from nonebot.params import CommandArg, ArgPlainText
from nonebot.typing import T_State
from nonebot.adapters.onebot.v11 import Message, MessageEvent
from nonebot.adapters.onebot.v11.helpers import convert_chinese_to_bool
from LittlePaimon import NICKNAME, DRIVER, SUPERUSERS, __version__
update_cmd = on_command('更新', permission=SUPERUSER, rule=to_me(), priority=1)
reboot_cmd = on_command('重启', permission=SUPERUSER, rule=to_me(), priority=1)
update_cmd = on_command('更新', permission=SUPERUSER, rule=to_me(), priority=1, block=True)
reboot_cmd = on_command('重启', permission=SUPERUSER, rule=to_me(), priority=1, block=True)
run_cmd = on_command('cmd', permission=SUPERUSER, rule=to_me(), priority=1, block=True)
__plugin_meta__ = PluginMetadata(
name='机器人管理',
@ -27,19 +31,39 @@ __plugin_meta__ = PluginMetadata(
@update_cmd.handle()
async def _(event: MessageEvent):
await update_cmd.send(f'{NICKNAME}开始执行git pull更新', at_sender=True)
p = subprocess.run(['git', 'pull'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
results = (p.stdout if p.returncode == 0 else p.stderr).decode('utf-8').split('\n')
p = await asyncio.subprocess.create_subprocess_shell('git pull', stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
stdout, stderr = await p.communicate()
results = (stdout or stderr).decode('utf-8').split('\n')
result_msg = ''.join(result.split('|')[0].strip(' ') + '\n' for result in results)
await update_cmd.finish(f'更新结果:{result_msg}')
@reboot_cmd.handle()
@reboot_cmd.got('confirm', prompt='确定要重启吗?(是|否)')
async def _(event: MessageEvent):
await reboot_cmd.send(f'{NICKNAME}开始执行重启,请等待{NICKNAME}的归来', at_sender=True)
(Path() / 'rebooting.json').open('w').close()
if sys.argv[0].endswith('nb'):
sys.argv[0] = 'bot.py'
os.execv(sys.executable, ['python'] + sys.argv)
if convert_chinese_to_bool(event.message):
await reboot_cmd.send(f'{NICKNAME}开始执行重启,请等待{NICKNAME}的归来', at_sender=True)
(Path() / 'rebooting.json').open('w').close()
if sys.argv[0].endswith('nb'):
sys.argv[0] = 'bot.py'
os.execv(sys.executable, ['python'] + sys.argv)
else:
await reboot_cmd.finish('取消重启')
@run_cmd.handle()
async def _(event: MessageEvent, state: T_State, cmd: Message = CommandArg()):
if cmd:
state['cmd'] = cmd
@run_cmd.got('cmd', prompt='你输入你要运行的命令')
async def _(event: MessageEvent, cmd: str = ArgPlainText('cmd')):
await run_cmd.send(f'开始执行{cmd}...', at_sender=True)
p = await asyncio.subprocess.create_subprocess_shell(cmd, stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
stdout, stderr = await p.communicate()
await run_cmd.finish(f'{cmd}\n运行结果:\n{(stdout or stderr).decode("utf-8")}')
@DRIVER.on_bot_connect

View File

@ -92,7 +92,7 @@ def create_guess_matcher(group_id, role_name, game_time):
re_str = '|'.join(alias_list)
guess_matcher = on_regex(re_str, temp=True, rule=Rule(check_group),
expire_time=datetime.timedelta(seconds=game_time))
guess_matcher.plugin_name = "Genshin_voice"
guess_matcher.plugin_name = "Genshin_Voice"
@guess_matcher.handle()
async def _(event: GroupMessageEvent):

View File

@ -39,6 +39,8 @@ async def chat_rule(event: GroupMessageEvent, state: T_State) -> bool:
return False
if event.user_id in config_manager.config.ban_users:
return False
if not event.raw_message:
return False
if any(w in event.raw_message for w in config_manager.config.ban_words):
return False
to_learn = True

View File

@ -91,7 +91,7 @@ class MessageData:
class LearningChat:
reply_cache = defaultdict(lambda: defaultdict(list))
"""回复的消息缓存"""
message_cache = defaultdict(list)
message_cache = {}
"""群消息缓存"""
_reply_lock = threading.Lock()
@ -271,7 +271,7 @@ class LearningChat:
'reply_keywords': '[PallasBot: Speak]',
})
speak_list = [Message(speak)]
speak_list = [speak]
while random.random() < config.speak_continuously_probability and len(
speak_list) < config.speak_continuously_max_len:
pre_msg = str(speak_list[-1])
@ -287,7 +287,7 @@ class LearningChat:
if random.random() < config.speak_poke_probability:
target_id = random.choice(LearningChat.message_cache[group_id])['user_id']
speak_list.append(Message(f'[CQ:poke,qq={target_id}]'))
speak_list.append(f'[CQ:poke,qq={target_id}]')
return bot_id, group_id, speak_list
@ -483,6 +483,8 @@ class LearningChat:
async def _update_message(self):
with LearningChat._message_lock:
if self.message.group_id not in LearningChat.message_cache:
LearningChat.message_cache[self.message.group_id] = []
LearningChat.message_cache[self.message.group_id].append(
{
'group_id': self.message.group_id,

View File

@ -7,7 +7,7 @@
<p align="center">
<a href="https://cdn.jsdelivr.net/gh/CMHopeSunshine/LittlePaimon@master/LICENSE"><img src="https://img.shields.io/github/license/CMHopeSunshine/LittlePaimon" alt="license"></a>
<img src="https://img.shields.io/badge/Python-3.8+-yellow" alt="python">
<img src="https://img.shields.io/badge/Version-3.0.0beta4-green" alt="version">
<img src="https://img.shields.io/badge/Version-3.0.0beta5-green" alt="version">
<a href="https://qun.qq.com/qqweb/qunpro/share?_wv=3&_wwv=128&inviteCode=MmWrI&from=246610&biz=ka"><img src="https://img.shields.io/badge/QQ频道交流-尘世闲游-blue?style=flat-square" alt="QQ guild"></a>
</p>
@ -15,7 +15,7 @@
原神多功能机器人通过米游社接口查询uid的游戏信息并提供WIKI查询和各种各样的好玩的功能。
该分支正在积极**开发中**,尚未发布正式版本,欢迎帮助测试和提出宝贵意见!
该分支正在积极**开发中**,尚未发布正式版本,欢迎帮助测试和提出宝贵意见!文档正在编写~~摸鱼~~中。
## | 功能示例
<details>