Browse Source

fix类名和参数不一致时候无法传入正确参数

枫林 3 months ago
parent
commit
ee3b70587e
5 changed files with 17 additions and 74 deletions
  1. 11 0
      src/config/permission.yml
  2. 1 0
      src/interface/plugin.ts
  3. 2 1
      src/lib/Plugins.ts
  4. 3 1
      src/lib/decorators.ts
  5. 0 72
      src/plugins/PluginsFile.ts

+ 11 - 0
src/config/permission.yml

@@ -33,6 +33,8 @@ users:
       PluginsFile:
         commands:
           help: true
+          logs: true
+          downloadlog: true
   '914085636':
     plugins:
       saku:
@@ -48,6 +50,15 @@ users:
           download: true
           logs: true
           downloadlog: true
+      sakulass:
+        commands:
+          help: true
+          图: true
+  '1051027747':
+    plugins:
+      sakulass:
+        commands:
+          图: true
   default:
     plugins:
       test:

+ 1 - 0
src/interface/plugin.ts

@@ -29,6 +29,7 @@ export interface PluginConfig {
 export interface Command {
     cmd: string;        // 命令名称
     desc: string;       // 命令描述
+    fnName: string;    // 函数名称
     fn: Function;       // 命令函数
     aliases?: string[]; // 命令别名
     cmdPrefix: string;  // 命令前缀

+ 2 - 1
src/lib/Plugins.ts

@@ -595,6 +595,7 @@ export function runcod(cmd: string | string[], desc: string): MethodDecorator {
             cmd: mainCmd,
             desc,
             fn: descriptor.value,
+            fnName: propertyKey.toString(),
             aliases,
             cmdPrefix: CMD_PREFIX,
             pluginId: pluginId,
@@ -625,7 +626,7 @@ async function parseCommandParams(message: string, context: PrivateFriendMessage
     botlogger.info('DEBUG - 命令参数:' + JSON.stringify({paramArgs}));
 
     const params: any[] = [];
-    const param = paramMetadata.get(command.pluginId+"."+command.cmd);
+    const param = paramMetadata.get(command.pluginId+"."+command.fnName);
     if (param) {
         for (const paramData of param) {
             const { name, type, index, optional } = paramData;

+ 3 - 1
src/lib/decorators.ts

@@ -84,6 +84,7 @@ export function plugins(config: PluginConfig): ClassDecorator {
                 cmdPrefix: CMD_PREFIX,
                 pluginId: config.id,
                 class: target,
+                fnName: 'help',
                 fn: async function (): Promise<object> {
                     const plugin = commandList.find(p => p.class === target);
                     if (!plugin) {
@@ -99,7 +100,7 @@ export function plugins(config: PluginConfig): ClassDecorator {
                             const aliases = cmd.aliases?.map(alias =>
                                 `${CMD_PREFIX}${plugin.id} ${alias}`
                             ) || [];
-                            paramMetadata.get(config.id + '.' + cmd.cmd)?.forEach((param: ParamMetadata) => {
+                            paramMetadata.get(config.id + '.' + cmd.fnName)?.forEach((param: ParamMetadata) => {
                                 param.optional ? fullCmd += ` [${param.name}]` : fullCmd += ` <${param.name}>`;
                             })
                             return {
@@ -217,6 +218,7 @@ export function runcod(cmd: string | string[], desc: string, config: CommandConf
                 const command: Command = {
                     cmd: mainCmd,
                     desc,
+                    fnName: propertyKey.toString(),
                     fn: descriptor.value,
                     aliases,
                     cmdPrefix: CMD_PREFIX,

+ 0 - 72
src/plugins/PluginsFile.ts

@@ -98,76 +98,4 @@ export class PluginsFile {
         }
     }
 
-    @runcod(["logs", "日志"], "获取日志")
-    async getloglist() {
-        // pluName += ".ts"
-        const __dirname = path.dirname(fileURLToPath(import.meta.url));
-        // 查找插件目录下的文件
-        const pluginsDir = path.join(__dirname, '..', '..','logs');
-        try {
-            const files = await fs.readdir(pluginsDir);
-            const foundFiles = files.filter(file =>
-                (file.endsWith('.log'))
-            );
-            foundFiles.forEach((file, index) => {
-                foundFiles[index] = path.parse(file).name;
-            })
-            return `找到插件文件:${foundFiles.join(', ')}`;
-        } catch (error) {
-            botlogger.error('文件查找失败:', error);
-            return '插件查找服务暂不可用';
-        }
-    }
-
-    @runcod(["downloadlog", "getlog"], "下载日志")
-    async getlogs(
-        @param("日志名称", ParamType.String) logName: string,
-        context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage
-    ): Promise<any> {
-        // pluName += ".ts"
-        const __dirname = path.dirname(fileURLToPath(import.meta.url));
-        // 查找插件目录下的文件
-        const pluginsDir = path.join(__dirname, '..', '..','logs');
-        try {
-            const files = await fs.readdir(pluginsDir);
-            const foundFiles = files.filter(file =>
-                (file.endsWith('.log'))
-            );
-
-            // 根据文件名查找具体插件
-            const targetFile = foundFiles.find((file: string) =>
-                path.parse(file).name.toLowerCase() === logName.toLowerCase()
-            );
-
-            if (!targetFile) {
-                return `未找到名为 ${logName} 的日志`;
-            }
-
-            // 返回文件完整路径
-            const fullPath = path.join(pluginsDir, targetFile);
-            //.toString('base64'
-
-            const file = Buffer.from(await fs.readFile(fullPath, { encoding: "utf-8" })).toString('base64')
-            const isGroupMessage = context.message_type === 'group';
-            if (isGroupMessage && context.group_id) {
-                await qqBot.upload_group_file({
-                    group_id: Number(context.group_id),
-                    file: 'data:file;base64,' + file,
-                    name: logName+'.log'
-                })
-
-            } else {
-                await qqBot.upload_private_file({
-                    user_id: Number(context.sender.user_id),
-                    file: 'data:file;base64,' + file,
-                    name: logName+'.log'
-                })
-            }
-
-            return '上传成功';
-        } catch (error) {
-            botlogger.error('文件查找失败:', error);
-            return '插件查找服务暂不可用';
-        }
-    }
 }