feat(plugin): 支持多个事件

This commit is contained in:
Rock Chin
2023-01-14 22:36:48 +08:00
parent 56664f9fbc
commit 6d81821557
7 changed files with 128 additions and 36 deletions
+24 -11
View File
@@ -4,6 +4,8 @@ import openai
import pkg.utils.context
import pkg.openai.session
import pkg.plugin.host as plugin_host
import pkg.plugin.models as plugin_models
def process_normal_message(text_message: str, mgr, config, launcher_type: str, launcher_id: int) -> list:
session_name = f"{launcher_type}_{launcher_id}"
@@ -26,18 +28,29 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str, l
pkg.utils.context.get_openai_manager().key_mgr.using_key
)
pkg.utils.context.get_openai_manager().key_mgr.set_current_exceeded()
switched, name = pkg.utils.context.get_openai_manager().key_mgr.auto_switch()
if not switched:
mgr.notify_admin(
"api-key调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format(
current_key_name))
reply = ["[bot]err:API调用额度超额,请联系作者,或等待修复"]
else:
openai.api_key = pkg.utils.context.get_openai_manager().key_mgr.get_using_key()
mgr.notify_admin("api-key调用额度超限({}),接口报错,已切换到{}".format(current_key_name, name))
reply = ["[bot]err:API调用额度超额,已自动切换,请重新发送消息"]
continue
# 触发插件事件
args = {
'key_name': current_key_name,
'usage': pkg.utils.context.get_openai_manager().audit_mgr
.get_usage(pkg.utils.context.get_openai_manager().key_mgr.get_using_key_md5()),
'exceeded_keys': pkg.utils.context.get_openai_manager().key_mgr.exceeded,
}
event = plugin_host.emit(plugin_models.KeyExceeded, **args)
if not event.is_prevented_default():
switched, name = pkg.utils.context.get_openai_manager().key_mgr.auto_switch()
if not switched:
mgr.notify_admin(
"api-key调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format(
current_key_name))
reply = ["[bot]err:API调用额度超额,请联系作者,或等待修复"]
else:
openai.api_key = pkg.utils.context.get_openai_manager().key_mgr.get_using_key()
mgr.notify_admin("api-key调用额度超限({}),接口报错,已切换到{}".format(current_key_name, name))
reply = ["[bot]err:API调用额度超额,已自动切换,请重新发送消息"]
continue
except openai.error.InvalidRequestError as e:
mgr.notify_admin("{}API调用参数错误:{}\n\n这可能是由于config.py中的prompt_submit_length参数或"
"completion_api_params中的max_tokens参数数值过大导致的,请尝试将其降低".format(
+35 -8
View File
@@ -1,14 +1,10 @@
# 此模块提供了消息处理的具体逻辑的接口
import asyncio
import datetime
import json
import threading
from func_timeout import func_set_timeout
import logging
import openai
from mirai import Image, MessageChain, Plain
from mirai import MessageChain, Plain
# 这里不使用动态引入config
# 因为在这里动态引入会卡死程序
@@ -23,6 +19,9 @@ import pkg.utils.context
import pkg.qqbot.message
import pkg.qqbot.command
import pkg.plugin.host as plugin_host
import pkg.plugin.models as plugin_models
processing = []
@@ -68,11 +67,39 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
try:
if text_message.startswith('!') or text_message.startswith(""): # 指令
reply = pkg.qqbot.command.process_command(session_name, text_message,
mgr, config, launcher_type, launcher_id)
# 触发插件事件
args = {
'launcher_type': launcher_type,
'launcher_id': launcher_id,
'sender_id': sender_id,
'command': text_message[1:].strip().split(' ')[0],
'params': text_message[1:].strip().split(' ')[1:],
'text_message': text_message,
'is_admin': sender_id is config.admin_qq,
}
event = plugin_host.emit(plugin_models.PersonCommand
if launcher_type == 'person'
else plugin_models.GroupCommand, **args)
if not event.is_prevented_default():
reply = pkg.qqbot.command.process_command(session_name, text_message,
mgr, config, launcher_type, launcher_id)
else: # 消息
reply = pkg.qqbot.message.process_normal_message(text_message, mgr, config, launcher_type, launcher_id)
# 触发插件事件
args = {
"launcher_type": launcher_type,
"launcher_id": launcher_id,
"sender_id": sender_id,
"text_message": text_message,
}
event = plugin_host.emit(plugin_models.PersonNormalMessage
if launcher_type == 'person'
else plugin_models.GroupNormalMessage, **args)
if not event.is_prevented_default():
reply = pkg.qqbot.message.process_normal_message(text_message,
mgr, config, launcher_type, launcher_id)
if reply is not None and type(reply[0]) == str:
logging.info(