QChatGPT/pkg/qqbot/command.py

66 lines
1.8 KiB
Python
Raw Normal View History

# 指令处理模块
import logging
import json
import datetime
2023-01-17 12:30:45 +08:00
import os
import threading
import traceback
import pkg.openai.session
import pkg.openai.manager
import pkg.utils.reloader
import pkg.utils.updater
import pkg.utils.context
import pkg.qqbot.message
import pkg.utils.credit as credit
2023-03-28 11:12:19 +08:00
# import pkg.qqbot.cmds.model as cmdmodel
import pkg.qqbot.cmds.aamgr as cmdmgr
from mirai import Image
2023-01-17 00:11:07 +08:00
def process_command(session_name: str, text_message: str, mgr, config,
2023-02-25 15:39:31 +08:00
launcher_type: str, launcher_id: int, sender_id: int, is_admin: bool) -> list:
reply = []
try:
logging.info(
"[{}]发起指令:{}".format(session_name, text_message[:min(20, len(text_message))] + (
"..." if len(text_message) > 20 else "")))
cmd = text_message[1:].strip().split(' ')[0]
params = text_message[1:].strip().split(' ')[1:]
2023-04-02 14:43:34 +08:00
# 把!~开头的转换成!cfg
2023-03-20 20:06:02 +08:00
if cmd.startswith('~'):
params = [cmd[1:]] + params
cmd = 'cfg'
2023-03-28 11:12:19 +08:00
# 包装参数
context = cmdmgr.Context(
command=cmd,
crt_command=cmd,
params=params,
2023-03-30 19:11:39 +08:00
crt_params=params[:],
2023-03-28 11:12:19 +08:00
session_name=session_name,
text_message=text_message,
launcher_type=launcher_type,
launcher_id=launcher_id,
sender_id=sender_id,
is_admin=is_admin,
privilege=2 if is_admin else 1, # 普通用户1管理员2
)
try:
reply = cmdmgr.execute(context)
except cmdmgr.CommandPrivilegeError as e:
reply = ["{}".format(e)]
2023-03-20 20:06:02 +08:00
return reply
except Exception as e:
mgr.notify_admin("{}指令执行失败:{}".format(session_name, e))
logging.exception(e)
reply = ["[bot]err:{}".format(e)]
return reply