Bläddra i källkod

分离log日志为新插件,去除id设置自动根据类名生成

枫林 3 månader sedan
förälder
incheckning
483d317d3f
6 ändrade filer med 100 tillägg och 7 borttagningar
  1. 2 1
      .gitignore
  2. 0 1
      src/interface/plugin.ts
  3. 3 3
      src/lib/decorators.ts
  4. 0 1
      src/plugins/PluginsFile.ts
  5. 95 0
      src/plugins/log.ts
  6. 0 1
      src/plugins/test.ts

+ 2 - 1
.gitignore

@@ -79,4 +79,5 @@ test/
 
 src/plugins/*
 !src/plugins/PluginsFile.ts
-!src/plugins/test.ts
+!src/plugins/test.ts
+!src/plugins/log.ts

+ 0 - 1
src/interface/plugin.ts

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

+ 3 - 3
src/lib/decorators.ts

@@ -60,7 +60,7 @@ export function plugins(config: PluginConfig): ClassDecorator {
         } else {
             // 添加新插件
             plugin = {
-                id: config.id,
+                id: target.name,
                 name: config.name,
                 class: target,
                 commands: [] as Command[],
@@ -82,7 +82,7 @@ export function plugins(config: PluginConfig): ClassDecorator {
                     sendText: false
                 },
                 cmdPrefix: CMD_PREFIX,
-                pluginId: config.id,
+                pluginId: target.name,
                 class: target,
                 fnName: 'help',
                 fn: async function (): Promise<object> {
@@ -100,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.fnName)?.forEach((param: ParamMetadata) => {
+                            paramMetadata.get(target.name + '.' + cmd.fnName)?.forEach((param: ParamMetadata) => {
                                 param.optional ? fullCmd += ` [${param.name}]` : fullCmd += ` <${param.name}>`;
                             })
                             return {

+ 0 - 1
src/plugins/PluginsFile.ts

@@ -9,7 +9,6 @@ import { qqBot } from '../app.js';
 import { ParamType } from '../interface/plugin.js';
 
 @plugins({
-    id: "PluginsFile", //插件ID,必须类名一致
     name: "插件文件管理", //插件名称,用于显示在菜单中
     version: "1.0.1", //插件版本号,用于显示在菜单中
     describe: "可以查看服务器的插件/日志,可以上传插件到群内", 

+ 95 - 0
src/plugins/log.ts

@@ -0,0 +1,95 @@
+import { param, plugins, runcod } from '../lib/decorators.js';
+import path from 'path';
+import 'reflect-metadata';
+import { fileURLToPath } from 'node:url';
+import { GroupMessage, PrivateFriendMessage, PrivateGroupMessage } from 'node-napcat-ts';
+import botlogger from '../lib/logger.js';
+import fs from 'fs/promises';
+import { qqBot } from '../app.js';
+import { ParamType } from '../interface/plugin.js';
+@plugins({
+    name: "log日志管理", //插件名称,用于显示在菜单中
+    version: "1.0.0", //插件版本号,用于显示在菜单中
+    describe: "获取服务器机器人到log日志", //插件描述,用于显示在菜单中
+    author: "枫叶秋林",//插件作者,用于显示在菜单中
+    help: { //插件帮助信息,用于显示在菜单中
+        enabled: true, //是否启用帮助信息
+        description: "显示帮助信息" //帮助信息描述
+    }
+})
+export class Botlog {
+
+    @runcod(["logs", "日志"], "获取日志")
+    async logs() {
+        // 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 getBotlogbyfile(
+        @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 '插件查找服务暂不可用';
+        }
+    }
+
+}

+ 0 - 1
src/plugins/test.ts

@@ -9,7 +9,6 @@ import botlogger from '../lib/logger.js';
 import { ParamType } from '../interface/plugin.js';
 
 @plugins({
-    id: "test", //插件ID,需与类名一致,唯一
     name: "测试插件", //插件名称,用于显示在菜单中
     version: "1.0.0", //插件版本号,用于显示在菜单中
     describe: "测试功能", //插件描述,用于显示在菜单中