Selaa lähdekoodia

默认接口修补+默认接口实现+插件配置保存

Sakulin 3 kuukautta sitten
vanhempi
sitoutus
1660cbb041
2 muutettua tiedostoa jossa 24 lisäystä ja 9 poistoa
  1. 15 4
      src/lib/Plugins.ts
  2. 9 5
      src/lib/decorators.ts

+ 15 - 4
src/lib/Plugins.ts

@@ -118,7 +118,8 @@ async function loadPlugins(): Promise<void> {
                                 class: instance.constructor,
                                 class: instance.constructor,
                                 version: pluginConfig.version,
                                 version: pluginConfig.version,
                                 author: pluginConfig.author,
                                 author: pluginConfig.author,
-                                describe: pluginConfig.describe
+                                describe: pluginConfig.describe,
+                                config: pluginConfig
                             };
                             };
                             // 触发装饰器
                             // 触发装饰器
                             await initializePluginCommands(instance);
                             await initializePluginCommands(instance);
@@ -332,8 +333,17 @@ export async function runplugins() {
                     botlogger.info(`${CMD_PREFIX}${plugin.id} ${cmd.cmd}`);
                     botlogger.info(`${CMD_PREFIX}${plugin.id} ${cmd.cmd}`);
                 });
                 });
 
 
-                // 查找命令
-                const command = findCommand(plugin, cmdName);
+                let command: Command | undefined = undefined;
+                if (cmdName == undefined) {
+                    botlogger.info(`未检测到第二个参数,运行默认命令: ${plugin.config.defaultCommandId}`);
+
+                    const commandId = plugin.config.defaultCommandId ?? "help";
+
+                    command = findCommand(plugin, commandId);
+                } else {
+                    command = findCommand(plugin, cmdName);
+                }
+
                 if (!command) {
                 if (!command) {
                     botlogger.info(`命令未找到: ${cmdName}`);
                     botlogger.info(`命令未找到: ${cmdName}`);
                     return;
                     return;
@@ -578,7 +588,8 @@ export function runcod(cmd: string | string[], desc: string): MethodDecorator {
                 id: pluginId,
                 id: pluginId,
                 name: pluginName,
                 name: pluginName,
                 commands: [],
                 commands: [],
-                class: target.constructor
+                class: target.constructor,
+                config: pluginConfig
             };
             };
             commandList.push(plugin);
             commandList.push(plugin);
             botlogger.info(`创建新插件[${pluginId}]: ${pluginName}`);
             botlogger.info(`创建新插件[${pluginId}]: ${pluginName}`);

+ 9 - 5
src/lib/decorators.ts

@@ -60,6 +60,7 @@ export interface Plugin {
     version?: string;
     version?: string;
     author?: string;
     author?: string;
     describe?: string;
     describe?: string;
+    config: PluginConfig
 }
 }
 // 修改插件装饰器配置接口
 // 修改插件装饰器配置接口
 export interface PluginConfig {
 export interface PluginConfig {
@@ -73,7 +74,7 @@ export interface PluginConfig {
         command?: string[];     // 帮助命令
         command?: string[];     // 帮助命令
         description?: string;   // 帮助命令描述
         description?: string;   // 帮助命令描述
     };
     };
-    default?: () => any; // 默认函数
+    defaultCommandId?: string; // 默认函数
 }
 }
 
 
 // 在 decorators.ts 中定义统一的接口
 // 在 decorators.ts 中定义统一的接口
@@ -114,7 +115,8 @@ export function plugins(config: PluginConfig): ClassDecorator {
                 id: config.id,
                 id: config.id,
                 name: config.name,
                 name: config.name,
                 class: target,
                 class: target,
-                commands: [] as Command[]
+                commands: [] as Command[],
+                config
             };
             };
             commandList.push(plugin);
             commandList.push(plugin);
             // 添加调试日志
             // 添加调试日志
@@ -209,8 +211,9 @@ export function plugins(config: PluginConfig): ClassDecorator {
             botlogger.info(`成功注册[${plugin.id}]帮助命令: ${CMD_PREFIX}${plugin.id} help`);
             botlogger.info(`成功注册[${plugin.id}]帮助命令: ${CMD_PREFIX}${plugin.id} help`);
         }
         }
 
 
-        if (!config.default) {
-            config.default = () => ({redirect: "help"});
+        // 检查并添加默认命令
+        if (!config.defaultCommandId) {
+            config.defaultCommandId = "help";
         }
         }
     };
     };
 }
 }
@@ -262,7 +265,8 @@ export function runcod(cmd: string | string[], desc: string, config: CommandConf
                     id: pluginConfig.id,
                     id: pluginConfig.id,
                     name: pluginName,
                     name: pluginName,
                     commands: [] as Command[],
                     commands: [] as Command[],
-                    class: target.constructor
+                    class: target.constructor,
+                    config: pluginConfig
                 };
                 };
                 commandList.push(plugin);
                 commandList.push(plugin);
                 botlogger.info(`创建新插件: ${pluginName}`);
                 botlogger.info(`创建新插件: ${pluginName}`);