koishi-plugin-kbot
Version:
A muti-function qq bot for koishi
69 lines (68 loc) • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.commandRoutes = void 0;
const __1 = require("..");
function getChildren(command) {
if (command.children.length === 0) {
return [];
}
else {
return command.children.map((child) => {
return {
name: child.name,
children: getChildren(child),
parent: command.name,
disable: false,
};
});
}
}
function commands(context) {
return async (ctx) => {
const commandsObject = {};
context.$commander._commands.forEach((command) => {
if (!command.parent && !commandsObject[command.name]) {
commandsObject[command.name] = {
name: command.name,
children: getChildren(command),
parent: command.parent?.name ?? '',
disable: false,
hasChildren: command.children.length > 0,
};
}
});
// ctx.body = [...context.$commander._commands.keys()].filter(key => !key.includes('.'))
ctx.body = commandsObject;
};
}
function getDisabledCommands(context) {
return async (ctx) => {
const { guildId } = ctx.query;
const channel = await context.database.get('channel', { id: guildId, platform: 'onebot', guildId });
ctx.body = channel[0]?.disable || [];
};
}
function switchCommands(context) {
return async (ctx) => {
const { guildId, commands } = ctx.query;
try {
await context.database.upsert('channel', [{ id: guildId, platform: 'onebot', guildId: guildId, disable: commands }]);
ctx.body = true;
}
catch (err) {
__1.logger.error(err);
ctx.body = false;
}
};
}
exports.commandRoutes = {
'/commands': function (context) {
return commands(context);
},
'/disabledCommands': function (context) {
return getDisabledCommands(context);
},
'/switchCommands': function (context) {
return switchCommands(context);
},
};