Files
LangBot/pkg/qqbot/cmds/plugin/plugin.py

199 lines
7.0 KiB
Python
Raw Normal View History

2023-11-13 21:59:23 +08:00
from ....plugin import host as plugin_host
from ....utils import updater
from .. import aamgr
2023-03-30 11:11:39 +00:00
2023-11-13 21:59:23 +08:00
@aamgr.AbstractCommandNode.register(
2023-03-30 11:11:39 +00:00
parent=None,
name="plugin",
description="插件管理",
2023-04-22 17:40:41 +08:00
usage="!plugin\n!plugin get <插件仓库地址>\n!plugin update\n!plugin del <插件名>\n!plugin on <插件名>\n!plugin off <插件名>",
2023-03-30 11:11:39 +00:00
aliases=[],
2023-07-31 15:23:42 +08:00
privilege=1
2023-03-30 11:11:39 +00:00
)
2023-11-13 21:59:23 +08:00
class PluginCommand(aamgr.AbstractCommandNode):
2023-03-30 11:11:39 +00:00
@classmethod
2023-11-13 21:59:23 +08:00
def process(cls, ctx: aamgr.Context) -> tuple[bool, list]:
2023-03-30 11:11:39 +00:00
reply = []
plugin_list = plugin_host.__plugins__
if len(ctx.params) == 0:
# 列出所有插件
reply_str = "[bot]所有插件({}):\n".format(len(plugin_host.__plugins__))
idx = 0
for key in plugin_host.iter_plugins_name():
plugin = plugin_list[key]
reply_str += "\n#{} {} {}\n{}\nv{}\n作者: {}\n"\
.format((idx+1), plugin['name'],
"[已禁用]" if not plugin['enabled'] else "",
plugin['description'],
plugin['version'], plugin['author'])
if updater.is_repo("/".join(plugin['path'].split('/')[:-1])):
remote_url = updater.get_remote_url("/".join(plugin['path'].split('/')[:-1]))
if remote_url != "https://github.com/RockChinQ/QChatGPT" and remote_url != "https://gitee.com/RockChin/QChatGPT":
reply_str += "源码: "+remote_url+"\n"
idx += 1
reply = [reply_str]
return True, reply
else:
return False, []
2023-11-13 21:59:23 +08:00
@aamgr.AbstractCommandNode.register(
2023-03-30 11:11:39 +00:00
parent=PluginCommand,
name="get",
description="安装插件",
usage="!plugin get <插件仓库地址>",
aliases=[],
privilege=2
)
2023-11-13 21:59:23 +08:00
class PluginGetCommand(aamgr.AbstractCommandNode):
2023-03-30 11:11:39 +00:00
@classmethod
2023-11-13 21:59:23 +08:00
def process(cls, ctx: aamgr.Context) -> tuple[bool, list]:
2023-03-30 11:11:39 +00:00
import threading
import logging
import pkg.utils.context
if len(ctx.crt_params) == 0:
reply = ["[bot]err: 请提供插件仓库地址"]
return True, reply
reply = []
def closure():
try:
plugin_host.install_plugin(ctx.crt_params[0])
2024-01-12 16:48:47 +08:00
pkg.utils.context.get_qqbot_manager().notify_admin("插件安装成功,请发送 !reload 命令重载插件")
2023-03-30 11:11:39 +00:00
except Exception as e:
logging.error("插件安装失败:{}".format(e))
pkg.utils.context.get_qqbot_manager().notify_admin("插件安装失败:{}".format(e))
threading.Thread(target=closure, args=()).start()
reply = ["[bot]正在安装插件..."]
return True, reply
2023-11-13 21:59:23 +08:00
@aamgr.AbstractCommandNode.register(
2023-03-30 11:11:39 +00:00
parent=PluginCommand,
name="update",
2023-11-12 12:05:04 +08:00
description="更新指定插件或全部插件",
2023-03-30 11:11:39 +00:00
usage="!plugin update",
aliases=[],
privilege=2
)
2023-11-13 21:59:23 +08:00
class PluginUpdateCommand(aamgr.AbstractCommandNode):
2023-03-30 11:11:39 +00:00
@classmethod
2023-11-13 21:59:23 +08:00
def process(cls, ctx: aamgr.Context) -> tuple[bool, list]:
2023-03-30 11:11:39 +00:00
import threading
import logging
plugin_list = plugin_host.__plugins__
reply = []
if len(ctx.crt_params) > 0:
def closure():
try:
import pkg.utils.context
updated = []
if ctx.crt_params[0] == 'all':
for key in plugin_list:
plugin_host.update_plugin(key)
updated.append(key)
else:
plugin_path_name = plugin_host.get_plugin_path_name_by_plugin_name(ctx.crt_params[0])
2023-11-12 11:30:10 +08:00
if plugin_path_name is not None:
plugin_host.update_plugin(ctx.crt_params[0])
updated.append(ctx.crt_params[0])
else:
raise Exception("未找到插件: {}".format(ctx.crt_params[0]))
pkg.utils.context.get_qqbot_manager().notify_admin("已更新插件: {}, 请发送 !reload 重载插件".format(", ".join(updated)))
except Exception as e:
logging.error("插件更新失败:{}".format(e))
pkg.utils.context.get_qqbot_manager().notify_admin("插件更新失败:{} 请使用 !plugin 命令确认插件名称或尝试手动更新插件".format(e))
reply = ["[bot]正在更新插件,请勿重复发起..."]
threading.Thread(target=closure).start()
else:
reply = ["[bot]请指定要更新的插件, 或使用 !plugin update all 更新所有插件"]
2023-03-30 11:11:39 +00:00
return True, reply
2023-11-13 21:59:23 +08:00
@aamgr.AbstractCommandNode.register(
2023-03-30 11:11:39 +00:00
parent=PluginCommand,
name="del",
description="删除插件",
usage="!plugin del <插件名>",
aliases=[],
privilege=2
)
2023-11-13 21:59:23 +08:00
class PluginDelCommand(aamgr.AbstractCommandNode):
2023-03-30 11:11:39 +00:00
@classmethod
2023-11-13 21:59:23 +08:00
def process(cls, ctx: aamgr.Context) -> tuple[bool, list]:
2023-03-30 11:11:39 +00:00
plugin_list = plugin_host.__plugins__
reply = []
if len(ctx.crt_params) < 1:
reply = ["[bot]err: 未指定插件名"]
else:
plugin_name = ctx.crt_params[0]
if plugin_name in plugin_list:
unin_path = plugin_host.uninstall_plugin(plugin_name)
reply = ["[bot]已删除插件: {} ({}), 请发送 !reload 重载插件".format(plugin_name, unin_path)]
else:
2024-01-12 16:48:47 +08:00
reply = ["[bot]err:未找到插件: {}, 请使用!plugin命令查看插件列表".format(plugin_name)]
2023-03-30 11:11:39 +00:00
return True, reply
2023-11-13 21:59:23 +08:00
@aamgr.AbstractCommandNode.register(
2023-03-30 11:11:39 +00:00
parent=PluginCommand,
name="on",
description="启用指定插件",
usage="!plugin on <插件名>",
aliases=[],
privilege=2
)
2023-11-13 21:59:23 +08:00
@aamgr.AbstractCommandNode.register(
2023-03-30 11:11:39 +00:00
parent=PluginCommand,
name="off",
description="禁用指定插件",
usage="!plugin off <插件名>",
aliases=[],
privilege=2
)
2023-11-13 21:59:23 +08:00
class PluginOnOffCommand(aamgr.AbstractCommandNode):
2023-03-30 11:11:39 +00:00
@classmethod
2023-11-13 21:59:23 +08:00
def process(cls, ctx: aamgr.Context) -> tuple[bool, list]:
2023-03-30 11:11:39 +00:00
import pkg.plugin.switch as plugin_switch
plugin_list = plugin_host.__plugins__
reply = []
print(ctx.params)
new_status = ctx.params[0] == 'on'
if len(ctx.crt_params) < 1:
reply = ["[bot]err: 未指定插件名"]
else:
plugin_name = ctx.crt_params[0]
if plugin_name in plugin_list:
plugin_list[plugin_name]['enabled'] = new_status
for func in plugin_host.__callable_functions__:
if func['name'].startswith(plugin_name+"-"):
func['enabled'] = new_status
2023-03-30 11:11:39 +00:00
plugin_switch.dump_switch()
reply = ["[bot]已{}插件: {}".format("启用" if new_status else "禁用", plugin_name)]
else:
2024-01-12 16:48:47 +08:00
reply = ["[bot]err:未找到插件: {}, 请使用!plugin命令查看插件列表".format(plugin_name)]
2023-03-30 11:11:39 +00:00
return True, reply