@oebot/core
Version:
能跑就行的 QQ 机器人框架,基于 oicq v2,改自KiviBot(R.I.P.)
268 lines (267 loc) • 11.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handlePluginCommand = exports.PluginMenu = void 0;
const config_1 = require("../config");
const logger_1 = require("../logger");
const plugin_1 = require("../plugin");
const utils_1 = require("../../utils");
const start_1 = require("../start");
exports.PluginMenu = `
〓 OEBot 插件 〓
/plugin list
简化:/p (ls|l)
/plugin add/rm <name>
简化:/p a/r <name>
/plugin on/off <name>
简化:/p t/f <name>
/plugin onall/offall
简化:/p ta/fa
/plugin reload <name>
简化:/p (rl|r) <name>
/plugin update <name?>
简化:/p (up|u) <name?>
`.trim();
async function handlePluginCommand(bot, params, reply) {
if (!params.length) {
return reply(exports.PluginMenu);
}
const [secondCmd, pname] = params;
switch (secondCmd) {
case 'list':
case 'ls':
case 'l':
{
const { plugins: allPlugins } = await (0, plugin_1.searchAllPlugins)();
const pinfo = allPlugins.map((pn) => {
const name = (0, plugin_1.getPluginNameByPath)(pn);
const plugin = start_1.plugins.get(name);
return `${plugin ? '●' : '○'} ${name}${plugin ? ` (${plugin.version})` : ''}`;
});
const message = `
〓 OEBot 插件列表 〓
${pinfo.join('\n')}
共 ${pinfo.length} 个,启用 ${start_1.plugins.size} 个
`.trim();
return reply(pinfo.length ? message : '〓 插件列表为空 〓');
break;
}
case 'onall':
case 'ta':
{
const { plugins: ps, cnts: { all } } = await (0, plugin_1.searchAllPlugins)();
if (all === 0) {
return reply('〓 插件列表为空 〓');
}
if (ps.length === start_1.plugins.size) {
return reply('〓 所有插件均已启用 〓');
}
let count = 0;
for (const path of ps) {
const i = ps.indexOf(path);
const pname = (0, plugin_1.getPluginNameByPath)(path);
if (start_1.plugins.has(pname)) {
// 过滤已经启用了的插件
return count++;
}
const res = await (0, plugin_1.enablePlugin)(bot, config_1.oeConf, path);
if (res === true) {
count++;
}
else {
await reply(`〓 ${pname} 启用失败 〓\n${res}`);
}
if (i + 1 === all) {
(0, config_1.saveOEConf)();
await reply(`〓 共启用 ${count} 个插件 〓`);
}
}
break;
}
case 'offall':
case 'fa':
{
const size = start_1.plugins.size;
if (!size) {
return reply('〓 所有插件均已禁用 〓');
}
// TODO: 将 forEach 用 for of 重写
Array.from(start_1.plugins.entries()).forEach(async ([pname, plugin], i) => {
const targetPluginPath = await (0, plugin_1.getPluginPathByName)(pname);
if (targetPluginPath) {
const res = await (0, plugin_1.disablePlugin)(bot, config_1.oeConf, plugin, targetPluginPath);
if (res !== true) {
await reply(`〓 ${pname} 禁用失败 〓\n${res}`);
}
start_1.plugins.delete(pname);
}
if (i + 1 === size) {
(0, config_1.saveOEConf)(start_1.plugins);
return reply('〓 已禁用所有插件 〓');
}
});
return;
break;
}
case 'update':
case 'up':
case 'u':
{
await reply('〓 正在更新插件... 〓');
const name = pname ? `${pname} ` : '';
try {
const upInfo = await (0, utils_1.update)(`oebot-plugin-${pname || '*'}`);
if (upInfo) {
const info = Object.entries(upInfo)
.map(([k, v]) => `${k.replace('oebot-plugin-', 'plugin: ')} => ${v.replace('^', '')}`)
.join('\n');
const updated = pname ? `〓 ${name}已是最新版本 〓` : '〓 所有插件均为最新版本 〓';
const msg = info ? `〓 插件更新成功 〓\n${info}\ntip: 需要重载插件才能生效` : updated;
await reply(msg);
}
else {
await reply(`〓 ${name}更新失败,详情查看日志 〓`);
}
}
catch (e) {
logger_1.OELogger.error((0, utils_1.stringifyError)(e));
await reply(`〓 ${name}更新失败 〓\n${(0, utils_1.stringifyError)(e)}`);
}
process.title = `OEBot ${start_1.pkg.version} ${config_1.oeConf.account}`;
return;
break;
}
case 'on':
case 't':
{
if (!pname) {
return reply('/plugin on <name>');
}
const targetPluginPath = await (0, plugin_1.getPluginPathByName)(pname);
if (!targetPluginPath) {
return reply(`〓 ${pname}: 插件不存在 〓`);
}
if (start_1.plugins.has(pname)) {
return reply(`〓 ${pname}: 插件已启用 〓`);
}
const res = await (0, plugin_1.enablePlugin)(bot, config_1.oeConf, targetPluginPath);
if (res === true) {
if ((0, config_1.saveOEConf)()) {
return reply(`〓 ${pname} 启用成功 〓`);
}
}
else {
return reply(`〓 ${pname} 启用失败 〓\n${res}`);
}
break;
}
case 'off':
case 'f':
{
if (!pname) {
return reply('/plugin off <name>');
}
const plugin = start_1.plugins.get(pname);
if (!plugin) {
return reply(`〓 ${pname}: 插件不存在 〓`);
}
const targetPluginPath = await (0, plugin_1.getPluginPathByName)(pname);
if (!targetPluginPath) {
return reply(`〓 ${pname}: 插件不存在 〓`);
}
const res = await (0, plugin_1.disablePlugin)(bot, config_1.oeConf, plugin, targetPluginPath);
if (res === true) {
start_1.plugins.delete(pname);
if ((0, config_1.saveOEConf)()) {
return reply(`〓 ${pname} 禁用成功 〓`);
}
}
else {
return reply(`〓 ${pname} 禁用失败 〓\n${res}`);
}
break;
}
case 'reload':
case 'rl':
case 'r':
{
if (!pname) {
return reply('/plugin reload <name>');
}
const plugin = start_1.plugins.get(pname);
const targetPluginPath = await (0, plugin_1.getPluginPathByName)(pname);
if (!targetPluginPath) {
return reply(`〓 ${pname}: 插件不存在 〓`);
}
let res;
if (!plugin) {
res = await (0, plugin_1.enablePlugin)(bot, config_1.oeConf, targetPluginPath);
}
else {
res = await (0, plugin_1.disablePlugin)(bot, config_1.oeConf, plugin, targetPluginPath);
res = res && (await (0, plugin_1.enablePlugin)(bot, config_1.oeConf, targetPluginPath));
}
if (res === true) {
if ((0, config_1.saveOEConf)()) {
return reply(`〓 ${pname} 重载成功 〓`);
}
}
else {
return reply(`〓 ${pname} 重载失败 〓\n${res}`);
}
break;
}
case 'add':
case 'a':
{
if (!pname) {
return reply('/plugin add <name>');
}
let shortName = pname;
if (/^oebot-plugin-/i.test(shortName)) {
shortName = shortName.replace(/^oebot-plugin-/i, '');
}
await reply(`〓 正在安装 ${pname}... 〓`);
try {
if (await (0, utils_1.install)(`oebot-plugin-${shortName}`)) {
await reply(`〓 ${pname} 安装成功 〓`);
}
else {
await reply(`〓 ${pname} 安装失败,详情查看日志 〓`);
}
}
catch (e) {
logger_1.OELogger.error((0, utils_1.stringifyError)(e));
await reply(`〓 ${pname} 安装失败 〓\n${(0, utils_1.stringifyError)(e)}`);
}
process.title = `OEBot ${start_1.pkg.version} ${config_1.oeConf.account}`;
break;
}
case 'rm':
case 'r':
{
if (!pname) {
return reply('/plugin rm <name>');
}
let shortName = pname;
if (/^oebot-plugin-/i.test(shortName)) {
shortName = shortName.replace(/^oebot-plugin-/i, '');
}
await reply(`〓 正在移除 ${pname}... 〓`);
try {
if (await (0, utils_1.install)(`oebot-plugin-${shortName}`, true)) {
await reply(`〓 ${pname} 移除成功 〓`);
}
else {
await reply(`〓 ${pname} 移除失败,详情查看日志 〓`);
}
}
catch (e) {
logger_1.OELogger.error((0, utils_1.stringifyError)(e));
await reply(`〓 ${pname} 移除失败 〓\n${(0, utils_1.stringifyError)(e)}`);
}
process.title = `OEBot ${start_1.pkg.version} ${config_1.oeConf.account}`;
break;
}
}
}
exports.handlePluginCommand = handlePluginCommand;