Răsfoiți Sursa

feat(插件): 添加简易命令支持以简化命令调用

在插件配置接口中新增 `easycmd` 选项,允许插件启用简易命令模式。启用后,命令将注册为 `#<命令名称>`,而不是 `#<插件名称> <命令名称>`。同时,在命令查找和执行逻辑中添加对简易命令的支持,确保系统能够正确处理简易命令的调用和权限检查。
枫林 3 luni în urmă
părinte
comite
dad3872a4c
3 a modificat fișierele cu 55 adăugiri și 13 ștergeri
  1. 1 0
      src/interface/plugin.ts
  2. 53 13
      src/lib/Plugins.ts
  3. 1 0
      src/plugins/test.ts

+ 1 - 0
src/interface/plugin.ts

@@ -12,6 +12,7 @@ export interface Plugin {
 
 // 修改插件装饰器配置接口
 export interface PluginConfig {
+    easycmd?: boolean;
     name: string;// 插件名称
     version?: string;// 插件版本
     describe?: string;// 插件描述

+ 53 - 13
src/lib/Plugins.ts

@@ -59,7 +59,9 @@ function createReplyMessage(messageId: number | string): ReplySegment {
 function findPlugin(pluginId: string): Plugin | undefined {
     return commandList.find((p: Plugin) => p.id === pluginId);
 }
-
+function findeasycmdPlugin(commandId: string): Plugin | undefined {
+    return commandList.find((p: Plugin) => p.config.easycmd === true && p.commands.find((c: Command) => c.cmd === commandId || c.aliases?.includes(commandId)));
+}
 // 修改命令查找函数
 function findCommand(plugin: Plugin, cmdName: string): Command | undefined {
     return plugin.commands.find((cmd: Command) => {
@@ -311,9 +313,43 @@ export async function runplugins() {
                 const plugin = findPlugin(pluginId);
                 if (!plugin) {
                     botlogger.info(`插件未找到: ${pluginId}`);
+                    //尝试easycmd开启的插件
+                    const easyplugin = findeasycmdPlugin(pluginId)
+                    if (!easyplugin) {
+                        botlogger.info(`插件未找到: ${pluginId}`);
+                        return;
+                    }
+                    let command: Command | undefined = undefined;
+                    command = findCommand(easyplugin, pluginId);
+                    if (!command) {
+                        botlogger.info(`命令未找到: ${pluginId}`);
+                        return;
+                    }
+                    //指令权限检查
+                    if (context.message_type === 'private') {
+                        if (!await IsPermission(context.user_id, easyplugin.id, command.cmd)) {
+                            botlogger.info(`[${context.user_id}]无权限执行命令: ${CMD_PREFIX}${easyplugin.id} ${command.cmd}`);
+                            context.quick_action([{
+                                type: 'text',
+                                data: { text: `你没有权限执行此命令` }
+                            }]);
+                            return;
+                        }
+                    }
+                    if (context.message_type === 'group') {
+                        if (!await IsPermission(context.group_id, easyplugin.id, command.cmd)) {
+                            botlogger.info(`[${context.group_id}]无权限执行命令: ${CMD_PREFIX}${easyplugin.id} ${command.cmd}`);
+                            context.quick_action([{
+                                type: 'text',
+                                data: { text: `你没有权限执行此命令` }
+                            }])
+                            return;
+                        }
+                    }
+                    // 执行命令
+                    await handleCommand(context, easyplugin, command, parts.slice(1), true);
                     return;
                 }
-
                 botlogger.info(`找到插件[${plugin.id}]: ${plugin.name}`);
 
                 // 显示可用命令
@@ -387,14 +423,14 @@ export async function runplugins() {
 }
 
 // 修改 handleCommand 函数
-async function handleCommand(context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage, plugin: Plugin, command: Command, args: string[]): Promise<void> {
+async function handleCommand(context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage, plugin: Plugin, command: Command, args: string[],easycmd:boolean=false): Promise<void> {
     try {
         // 解析参数 - 传入完整消息文本
         if (!context.message[0].type || context.message[0].type !== 'text') {
             throw new Error('消息内容为空');
         }
         const message = context.message[0].data.text || '';
-        const parsedArgs = await parseCommandParams(message, context, command);
+        const parsedArgs = await parseCommandParams(message, context, command,easycmd);
 
         botlogger.info('命令参数解析完成:' + JSON.stringify({
             command: command.cmd,
@@ -615,27 +651,31 @@ export function runcod(cmd: string | string[], desc: string): MethodDecorator {
 
 
 // 修改参数解析函数
-async function parseCommandParams(message: string, context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage,command:Command): Promise<any[]> {
+async function parseCommandParams(message: string, context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage, command: Command,easycmd:boolean): Promise<any[]> {
     const cmdArgs = message.split(/\s+/).filter(Boolean);
 
     // 移除命令前缀和命令名
     const parts = message.split(/\s+/);
-    const paramArgs = parts.slice(2); // 跳过 #test param 这两个部分
+    let cmdIndex = 2;
+    if (easycmd) {
+        cmdIndex = 1;
+    }
+    const paramArgs = parts.slice(cmdIndex); 
 
     // 调试日志
-    botlogger.info('DEBUG - 命令参数:' + JSON.stringify({paramArgs}));
+    botlogger.info('DEBUG - 命令参数:' + JSON.stringify({ paramArgs }));
 
     const params: any[] = [];
-    const param = paramMetadata.get(command.pluginId+"."+command.fnName);
+    const param = paramMetadata.get(command.pluginId + "." + command.fnName);
     if (param) {
         for (const paramData of param) {
             const { name, type, index, optional } = paramData;
-            const msg= `正确格式为: ${CMD_PREFIX}${command.pluginId} ${command.cmd} ${param.map(p => p.optional ? `[${p.name}]` :`<${p.name}>`).join(' ')}`
+            const msg = `正确格式为: ${CMD_PREFIX}${command.pluginId} ${command.cmd} ${param.map(p => p.optional ? `[${p.name}]` : `<${p.name}>`).join(' ')}`
             if (paramArgs.length < param?.length) {
                 throw new Error(`参数不足,${msg}`);
             }
             if (!optional && !paramArgs[index]) {
-                throw new Error(`参数 <${name}> 是必需的,${msg}`);   
+                throw new Error(`参数 <${name}> 是必需的,${msg}`);
             }
             switch (type) {
                 case "string":
@@ -649,9 +689,9 @@ async function parseCommandParams(message: string, context: PrivateFriendMessage
                     break;
                 case "boolean":
                     params[index] = paramArgs[index] === 'true';
-                        if (optional && paramArgs[index] === undefined) {
-                            params[index] = false;
-                        }
+                    if (optional && paramArgs[index] === undefined) {
+                        params[index] = false;
+                    }
                     break;
                 case "rest":
                     params[index] = paramArgs.slice(index);

+ 1 - 0
src/plugins/test.ts

@@ -9,6 +9,7 @@ import botlogger from '../lib/logger.js';
 import { ParamType } from '../interface/plugin.js';
 
 @plugins({
+    easycmd: true,//是否启用简易命令,启用将将命令注册为#<命令名称>,不启用将注册为#<插件名称> <命令名称>
     name: "测试插件", //插件名称,用于显示在菜单中
     version: "1.0.0", //插件版本号,用于显示在菜单中
     describe: "测试功能", //插件描述,用于显示在菜单中