mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-25 13:56:08 +00:00
feat: 增加多管理员支持 (#178)
This commit is contained in:
@@ -38,6 +38,8 @@ openai_config = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# [必需] 管理员QQ号,用于接收报错等通知及执行管理员级别指令
|
# [必需] 管理员QQ号,用于接收报错等通知及执行管理员级别指令
|
||||||
|
# 支持多个管理员,可以使用list形式设置,例如:
|
||||||
|
# admin_qq = [12345678, 87654321]
|
||||||
admin_qq = 0
|
admin_qq = 0
|
||||||
|
|
||||||
# 情景预设(机器人人格)
|
# 情景预设(机器人人格)
|
||||||
|
|||||||
@@ -158,7 +158,7 @@ def plugin_operation(cmd, params, is_admin):
|
|||||||
|
|
||||||
|
|
||||||
def process_command(session_name: str, text_message: str, mgr, config,
|
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 = []
|
reply = []
|
||||||
try:
|
try:
|
||||||
logging.info(
|
logging.info(
|
||||||
@@ -280,9 +280,8 @@ def process_command(session_name: str, text_message: str, mgr, config,
|
|||||||
reply = [reply_str]
|
reply = [reply_str]
|
||||||
|
|
||||||
elif cmd == 'plugin':
|
elif cmd == 'plugin':
|
||||||
reply = plugin_operation(cmd, params, True
|
reply = plugin_operation(cmd, params, is_admin)
|
||||||
if (launcher_type == 'person' and launcher_id == config.admin_qq)
|
|
||||||
else False)
|
|
||||||
elif cmd == 'default':
|
elif cmd == 'default':
|
||||||
if len(params) == 0:
|
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 += "\n当前默认情景预设:{}\n".format(dprompt.get_current())
|
||||||
reply_str += "请使用!default <情景预设>来设置默认情景预设"
|
reply_str += "请使用!default <情景预设>来设置默认情景预设"
|
||||||
reply = [reply_str]
|
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
|
import pkg.openai.dprompt as dprompt
|
||||||
try:
|
try:
|
||||||
@@ -304,12 +303,12 @@ def process_command(session_name: str, text_message: str, mgr, config,
|
|||||||
reply = ["[bot]err: 未找到情景预设:{}".format(params[0])]
|
reply = ["[bot]err: 未找到情景预设:{}".format(params[0])]
|
||||||
else:
|
else:
|
||||||
reply = ["[bot]err: 仅管理员可设置默认情景预设"]
|
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():
|
def reload_task():
|
||||||
pkg.utils.reloader.reload_all()
|
pkg.utils.reloader.reload_all()
|
||||||
|
|
||||||
threading.Thread(target=reload_task, daemon=True).start()
|
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():
|
def update_task():
|
||||||
try:
|
try:
|
||||||
if pkg.utils.updater.update_all():
|
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()
|
threading.Thread(target=update_task, daemon=True).start()
|
||||||
|
|
||||||
reply = ["[bot]正在更新,请耐心等待,请勿重复发起更新..."]
|
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)
|
reply = config_operation(cmd, params)
|
||||||
else:
|
else:
|
||||||
if cmd.startswith("~") and launcher_type == 'person' and launcher_id == config.admin_qq:
|
if cmd.startswith("~") and is_admin:
|
||||||
config_item = cmd[1:]
|
config_item = cmd[1:]
|
||||||
params = [config_item] + params
|
params = [config_item] + params
|
||||||
reply = config_operation("cfg", params)
|
reply = config_operation("cfg", params)
|
||||||
|
|||||||
+17
-6
@@ -303,14 +303,25 @@ class QQBotManager:
|
|||||||
# 通知系统管理员
|
# 通知系统管理员
|
||||||
def notify_admin(self, message: str):
|
def notify_admin(self, message: str):
|
||||||
config = pkg.utils.context.get_config()
|
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))
|
logging.info("通知管理员:{}".format(message))
|
||||||
send_task = self.bot.send_friend_message(config.admin_qq, "[bot]{}".format(message))
|
if type(config.admin_qq) == int:
|
||||||
threading.Thread(target=asyncio.run, args=(send_task,)).start()
|
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):
|
def notify_admin_message_chain(self, message):
|
||||||
config = pkg.utils.context.get_config()
|
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))
|
logging.info("通知管理员:{}".format(message))
|
||||||
send_task = self.bot.send_friend_message(config.admin_qq, message)
|
if type(config.admin_qq) == int:
|
||||||
threading.Thread(target=asyncio.run, args=(send_task,)).start()
|
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()
|
||||||
|
|||||||
+11
-2
@@ -29,6 +29,15 @@ import pkg.qqbot.ignore as ignore
|
|||||||
processing = []
|
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,
|
def process_message(launcher_type: str, launcher_id: int, text_message: str, message_chain: MessageChain,
|
||||||
sender_id: int) -> MessageChain:
|
sender_id: int) -> MessageChain:
|
||||||
global processing
|
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],
|
'command': text_message[1:].strip().split(' ')[0],
|
||||||
'params': text_message[1:].strip().split(' ')[1:],
|
'params': text_message[1:].strip().split(' ')[1:],
|
||||||
'text_message': text_message,
|
'text_message': text_message,
|
||||||
'is_admin': sender_id == config.admin_qq,
|
'is_admin': is_admin(sender_id),
|
||||||
}
|
}
|
||||||
event = plugin_host.emit(plugin_models.PersonCommandSent
|
event = plugin_host.emit(plugin_models.PersonCommandSent
|
||||||
if launcher_type == 'person'
|
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():
|
if not event.is_prevented_default():
|
||||||
reply = pkg.qqbot.command.process_command(session_name, text_message,
|
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: # 消息
|
else: # 消息
|
||||||
# 限速丢弃检查
|
# 限速丢弃检查
|
||||||
|
|||||||
Reference in New Issue
Block a user