UNPKG

@kotori-bot/kotori-plugin-helper

Version:

helper and menu plugin

122 lines (121 loc) 5.43 kB
/** * @Package @kotori-bot/kotori-plugin-helper * @Version 1.4.0 * @Author Hotaru <me@hotaru.icu> * @Copyright 2024-2025 Hotaru. All rights reserved. * @License BAN-ZHINESE-USING * @Link https://github.com/kotorijs/kotori * @Date 16:13:14 */ "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var src_exports = {}; __export(src_exports, { config: () => config, default: () => src_default, lang: () => lang, main: () => main }); module.exports = __toCommonJS(src_exports); var import_kotori_bot = require("kotori-bot"); const config = import_kotori_bot.Tsu.Object({ alias: import_kotori_bot.Tsu.String().optional().describe("Menu command alias"), keywords: import_kotori_bot.Tsu.Array(import_kotori_bot.Tsu.String()).default(["\u83DC\u5355", "\u529F\u80FD"]).describe("Menu command shortcuts"), content: import_kotori_bot.Tsu.String().optional().describe("Custom menu command content") }); const lang = [__dirname, "../locales"]; function main(ctx, cfg) { if (cfg.content) { const cmd = ctx.command("menu - helper.descr.menu").shortcut(cfg.keywords).action((_, session) => session.format(String(cfg.content), { at: session.userId })); if (cfg.alias) cmd.alias(cfg.alias); } ctx.on("before_command", (data) => { if (data.command.meta.options.some((val) => val.realname === "help" || val.name === "H")) return; if (![" --help", " -H", " -h"].some((val) => data.raw.includes(val))) return; data.cancel(); ctx.emit( "on_message", Object.assign(data.session, { message: /* @__PURE__ */ kotoriHTs("text", null, `${data.session.api.adapter.config.commandPrefix}help ${data.command.meta.root}`) }) ); }); ctx.command("help [...command] - helper.descr.help").action((data, session) => { const args = data.args.join(" "); const filterResult = []; for (const command of ctx[import_kotori_bot.Symbols.command]) { if (command.meta.hide) continue; if (!command.meta.root.startsWith(args) && !command.meta.alias.some((alias) => alias.startsWith(args))) continue; filterResult.push(command.meta); } if (filterResult.length <= 0) return "helper.msg.descr.fail"; let commands = ""; for (const cmd of filterResult) { const alias = cmd.alias.length > 0 ? session.format("helper.template.alias", { content: cmd.alias.join(session.i18n.locale("helper.template.alias.delimiter")) }) : ""; let args2 = ""; let options = ""; const handle = (values) => { for (const value of values) { let defaultValue = ""; if ("rest" in value) { const valueType = typeof value.default; if (valueType === "string" || valueType === "number") { defaultValue = session.format("helper.template.default", { content: value.default }); } else if (valueType === "boolean") { defaultValue = session.format("helper.template.default", { content: value.default ? "true" : "false" }); } args2 += session.format(`helper.template.arg.${value.optional ? "optional" : "required"}`, { name: value.rest ? `...${value.name}` : value.name, type: value.type === "string" ? "" : session.format("helper.template.arg.type", { content: value.type }), default: defaultValue }); } if (!("realname" in value) || !("description" in value)) return; options += session.format("helper.template.option", { name: value.name, realname: value.realname, type: value.type === "string" ? "" : session.format("helper.template.arg.type", { content: value.type }), description: value.description ? session.format("helper.template.description", { content: session.i18n.locale(value.description) }) : "" }); } }; handle(cmd.args); handle(cmd.options); if (options) options = session.format("helper.template.options", { content: options }); commands += session.format("helper.msg.descr.command", { root: `${session.api.adapter.config.commandPrefix}${cmd.root}`, args: args2, description: cmd.description ? session.format("helper.template.description", { content: session.i18n.locale(cmd.description) }) : "", options, help: cmd.help ? session.format("helper.template.help", { content: session.i18n.locale(cmd.help) }) : "", alias }); } return session.format("helper.msg.help", { content: commands }); }); } var src_default = main; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { config, lang, main });