feat: 增加多管理员支持 (#178)

This commit is contained in:
Rock Chin
2023-02-25 15:39:31 +08:00
parent 29819668e3
commit 173f05a8ae
4 changed files with 38 additions and 17 deletions

View File

@@ -38,6 +38,8 @@ openai_config = {
}
# [必需] 管理员QQ号用于接收报错等通知及执行管理员级别指令
# 支持多个管理员可以使用list形式设置例如
# admin_qq = [12345678, 87654321]
admin_qq = 0
# 情景预设(机器人人格)

View File

@@ -158,7 +158,7 @@ def plugin_operation(cmd, params, is_admin):
def process_command(session_name: str, text_message: str, mgr, config,
launcher_type: str, launcher_id: int, sender_id: int) -> list:
launcher_type: str, launcher_id: int, sender_id: int, is_admin: bool) -> list:
reply = []
try:
logging.info(
@@ -280,9 +280,8 @@ def process_command(session_name: str, text_message: str, mgr, config,
reply = [reply_str]
elif cmd == 'plugin':
reply = plugin_operation(cmd, params, True
if (launcher_type == 'person' and launcher_id == config.admin_qq)
else False)
reply = plugin_operation(cmd, params, is_admin)
elif cmd == 'default':
if len(params) == 0:
# 输出目前所有情景预设
@@ -294,7 +293,7 @@ def process_command(session_name: str, text_message: str, mgr, config,
reply_str += "\n当前默认情景预设:{}\n".format(dprompt.get_current())
reply_str += "请使用!default <情景预设>来设置默认情景预设"
reply = [reply_str]
elif len(params) >0 and launcher_type == 'person' and launcher_id == config.admin_qq:
elif len(params) >0 and is_admin:
# 设置默认情景
import pkg.openai.dprompt as dprompt
try:
@@ -304,12 +303,12 @@ def process_command(session_name: str, text_message: str, mgr, config,
reply = ["[bot]err: 未找到情景预设:{}".format(params[0])]
else:
reply = ["[bot]err: 仅管理员可设置默认情景预设"]
elif cmd == 'reload' and launcher_type == 'person' and launcher_id == config.admin_qq:
elif cmd == 'reload' and is_admin:
def reload_task():
pkg.utils.reloader.reload_all()
threading.Thread(target=reload_task, daemon=True).start()
elif cmd == 'update' and launcher_type == 'person' and launcher_id == config.admin_qq:
elif cmd == 'update' and is_admin:
def update_task():
try:
if pkg.utils.updater.update_all():
@@ -324,10 +323,10 @@ def process_command(session_name: str, text_message: str, mgr, config,
threading.Thread(target=update_task, daemon=True).start()
reply = ["[bot]正在更新,请耐心等待,请勿重复发起更新..."]
elif cmd == 'cfg' and launcher_type == 'person' and launcher_id == config.admin_qq:
elif cmd == 'cfg' and is_admin:
reply = config_operation(cmd, params)
else:
if cmd.startswith("~") and launcher_type == 'person' and launcher_id == config.admin_qq:
if cmd.startswith("~") and is_admin:
config_item = cmd[1:]
params = [config_item] + params
reply = config_operation("cfg", params)

View File

@@ -303,14 +303,25 @@ class QQBotManager:
# 通知系统管理员
def notify_admin(self, message: str):
config = pkg.utils.context.get_config()
if hasattr(config, "admin_qq") and config.admin_qq != 0:
if hasattr(config, "admin_qq") and config.admin_qq != 0 and config.admin_qq != []:
logging.info("通知管理员:{}".format(message))
send_task = self.bot.send_friend_message(config.admin_qq, "[bot]{}".format(message))
threading.Thread(target=asyncio.run, args=(send_task,)).start()
if type(config.admin_qq) == int:
send_task = self.bot.send_friend_message(config.admin_qq, "[bot]{}".format(message))
threading.Thread(target=asyncio.run, args=(send_task,)).start()
else:
for adm in config.admin_qq:
send_task = self.bot.send_friend_message(adm, "[bot]{}".format(message))
threading.Thread(target=asyncio.run, args=(send_task,)).start()
def notify_admin_message_chain(self, message):
config = pkg.utils.context.get_config()
if hasattr(config, "admin_qq") and config.admin_qq != 0:
if hasattr(config, "admin_qq") and config.admin_qq != 0 and config.admin_qq != []:
logging.info("通知管理员:{}".format(message))
send_task = self.bot.send_friend_message(config.admin_qq, message)
threading.Thread(target=asyncio.run, args=(send_task,)).start()
if type(config.admin_qq) == int:
send_task = self.bot.send_friend_message(config.admin_qq, message)
threading.Thread(target=asyncio.run, args=(send_task,)).start()
else:
for adm in config.admin_qq:
send_task = self.bot.send_friend_message(adm, message)
threading.Thread(target=asyncio.run, args=(send_task,)).start()

View File

@@ -29,6 +29,15 @@ import pkg.qqbot.ignore as ignore
processing = []
def is_admin(qq: int) -> bool:
"""兼容list和int类型的管理员判断"""
import config
if type(config.admin_qq) == list:
return qq in config.admin_qq
else:
return qq == config.admin_qq
def process_message(launcher_type: str, launcher_id: int, text_message: str, message_chain: MessageChain,
sender_id: int) -> MessageChain:
global processing
@@ -83,7 +92,7 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
'command': text_message[1:].strip().split(' ')[0],
'params': text_message[1:].strip().split(' ')[1:],
'text_message': text_message,
'is_admin': sender_id == config.admin_qq,
'is_admin': is_admin(sender_id),
}
event = plugin_host.emit(plugin_models.PersonCommandSent
if launcher_type == 'person'
@@ -98,7 +107,7 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
if not event.is_prevented_default():
reply = pkg.qqbot.command.process_command(session_name, text_message,
mgr, config, launcher_type, launcher_id, sender_id)
mgr, config, launcher_type, launcher_id, sender_id, is_admin(sender_id))
else: # 消息
# 限速丢弃检查