|
@@ -1,46 +1,33 @@
|
|
|
import * as path from 'path';
|
|
|
import botlogger from './logger.js';
|
|
|
-import type {WSSendParam as MessageContext } from 'node-napcat-ts/dist/Interfaces.js';
|
|
|
// 读取配置文件
|
|
|
-import {Botconfig as config} from './config.js'
|
|
|
-const CMD_PREFIX = config?.cmd?.prefix??'#';
|
|
|
+import { Botconfig as config } from './config.js'
|
|
|
+const CMD_PREFIX = config?.cmd?.prefix ?? '#';
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
-import { assert } from 'node:console';
|
|
|
-// 参数类型枚举
|
|
|
-export const ParamType = {
|
|
|
- String: "string" as const,
|
|
|
- Number: "number" as const,
|
|
|
- Boolean: "boolean" as const,
|
|
|
- Rest: "rest" as const
|
|
|
-} as const;
|
|
|
-
|
|
|
-// 从对象值中获取类型
|
|
|
-export type ParamType = typeof ParamType[keyof typeof ParamType];
|
|
|
-
|
|
|
-// 参数元数据接口
|
|
|
-interface ParamMetadata {
|
|
|
- name: string; // 参数名称
|
|
|
- type: ParamType; // 参数类型
|
|
|
- index: number; // 参数索引
|
|
|
- optional: boolean; // 是否可选
|
|
|
-}
|
|
|
+import { Command, CommandConfig, ParamMetadata, ParamType, PluginConfig } from '../interface/plugin.js';
|
|
|
+import { Plugin } from '../interface/plugin.js';
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
// 存储参数元数据
|
|
|
export const paramMetadata = new Map<Function, ParamMetadata[]>();
|
|
|
+// 存储命令的数组
|
|
|
+export const commandList: Plugin[] = [];
|
|
|
|
|
|
// 修改参数装饰器
|
|
|
-export function param(name: string, type: ParamType = ParamType.String, optional:boolean = false): ParameterDecorator {
|
|
|
+export function param(name: string, type: ParamType = ParamType.String, optional: boolean = false): ParameterDecorator {
|
|
|
|
|
|
-return function(target: any, propertyKey: string | symbol | undefined, parameterIndex: number): void {
|
|
|
- const actualPropertyKey = propertyKey!;
|
|
|
+ return function (target: any, propertyKey: string | symbol | undefined, parameterIndex: number): void {
|
|
|
+ const actualPropertyKey = propertyKey!;
|
|
|
const fn = target[actualPropertyKey];
|
|
|
let metadata = paramMetadata.get(fn);
|
|
|
-
|
|
|
+
|
|
|
if (!metadata) {
|
|
|
metadata = [];
|
|
|
paramMetadata.set(fn, metadata);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
const paramData: ParamMetadata = {
|
|
|
name,
|
|
|
type,
|
|
@@ -51,47 +38,7 @@ return function(target: any, propertyKey: string | symbol | undefined, parameter
|
|
|
metadata[parameterIndex] = paramData;
|
|
|
};
|
|
|
}
|
|
|
-// 更新 Plugin 接口
|
|
|
-export interface Plugin {
|
|
|
- id: string;
|
|
|
- name: string;
|
|
|
- commands: Command[];
|
|
|
- class: any;
|
|
|
- version?: string;
|
|
|
- author?: string;
|
|
|
- describe?: string;
|
|
|
- config: PluginConfig
|
|
|
-}
|
|
|
-// 修改插件装饰器配置接口
|
|
|
-export interface PluginConfig {
|
|
|
- id: string;// 插件ID
|
|
|
- name: string;// 插件名称
|
|
|
- version?: string;// 插件版本
|
|
|
- describe?: string;// 插件描述
|
|
|
- author?: string;// 插件作者
|
|
|
- help?: {// 帮助命令配置,使用框架默认的帮助配置
|
|
|
- enabled?: boolean; // 是否启用帮助命令
|
|
|
- command?: string[]; // 帮助命令
|
|
|
- description?: string; // 帮助命令描述
|
|
|
- };
|
|
|
- defaultCommandId?: string; // 默认函数
|
|
|
-}
|
|
|
|
|
|
-// 在 decorators.ts 中定义统一的接口
|
|
|
-export interface Command {
|
|
|
- cmd: string; // 命令名称
|
|
|
- desc: string; // 命令描述
|
|
|
- fn: Function; // 命令函数
|
|
|
- aliases?: string[]; // 命令别名
|
|
|
- cmdPrefix: string; // 命令前缀
|
|
|
- pluginId: string; // 插件ID
|
|
|
- class: new () => any;
|
|
|
- template?: {
|
|
|
- enabled: boolean; // 是否启用模板
|
|
|
- sendText: boolean; // 是否发送文本
|
|
|
- [key: string]: any; // 其他模板配置
|
|
|
- };
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
|
@@ -100,11 +47,11 @@ export function plugins(config: PluginConfig): ClassDecorator {
|
|
|
return function (target: any): void {
|
|
|
// 保存插件配置
|
|
|
target.prototype.plugincfg = config;
|
|
|
-
|
|
|
+
|
|
|
// 确保插件名称正确
|
|
|
const existingPlugin = commandList.find((x) => x.class === target);
|
|
|
let plugin: Plugin;
|
|
|
-
|
|
|
+
|
|
|
if (existingPlugin) {
|
|
|
// 更新现有插件的配置
|
|
|
existingPlugin.name = config.name;
|
|
@@ -136,7 +83,7 @@ export function plugins(config: PluginConfig): ClassDecorator {
|
|
|
cmdPrefix: CMD_PREFIX,
|
|
|
pluginId: config.id,
|
|
|
class: target,
|
|
|
- fn: async function(): Promise<object> {
|
|
|
+ fn: async function (): Promise<object> {
|
|
|
const plugin = commandList.find(p => p.class === target);
|
|
|
if (!plugin) {
|
|
|
return {
|
|
@@ -148,10 +95,10 @@ export function plugins(config: PluginConfig): ClassDecorator {
|
|
|
.filter(cmd => !cmd.cmd.endsWith('help'))
|
|
|
.map(cmd => {
|
|
|
const fullCmd = `${CMD_PREFIX}${plugin.id} ${cmd.cmd}`;
|
|
|
- const aliases = cmd.aliases?.map(alias =>
|
|
|
+ const aliases = cmd.aliases?.map(alias =>
|
|
|
`${CMD_PREFIX}${plugin.id} ${alias}`
|
|
|
) || [];
|
|
|
-
|
|
|
+
|
|
|
return {
|
|
|
name: cmd.cmd,
|
|
|
fullCmd,
|
|
@@ -159,7 +106,7 @@ export function plugins(config: PluginConfig): ClassDecorator {
|
|
|
aliases
|
|
|
};
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
// 返回支持模板的响应对象
|
|
|
return {
|
|
@@ -220,22 +167,6 @@ export function plugins(config: PluginConfig): ClassDecorator {
|
|
|
|
|
|
|
|
|
|
|
|
-// 存储命令的数组
|
|
|
-export const commandList: Plugin[] = [];
|
|
|
-
|
|
|
-// 添加模板配置接口
|
|
|
-interface TemplateConfig {
|
|
|
- enabled: boolean;
|
|
|
- sendText: boolean;
|
|
|
- [key: string]: any;
|
|
|
-}
|
|
|
-
|
|
|
-// 修改命令装饰器配置
|
|
|
-interface CommandConfig {
|
|
|
- template?: TemplateConfig;
|
|
|
- [key: string]: any;
|
|
|
-}
|
|
|
-
|
|
|
// 修改 runcod 装饰器
|
|
|
/**
|
|
|
* 运行命令装饰器
|
|
@@ -245,19 +176,19 @@ interface CommandConfig {
|
|
|
*/
|
|
|
//权限
|
|
|
export function runcod(cmd: string | string[], desc: string, config: CommandConfig = {}, IsTest = false): MethodDecorator {
|
|
|
- return function(target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
|
|
|
+ return function (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
|
|
|
// 延迟执行命令注册
|
|
|
const originalMethod = descriptor.value;
|
|
|
- descriptor.value = function(...args: any[]) {
|
|
|
+ descriptor.value = function (...args: any[]) {
|
|
|
// 获取插件配置
|
|
|
const pluginConfig = target.constructor.prototype.plugincfg;
|
|
|
if (!pluginConfig) {
|
|
|
botlogger.error(`未找到插件配置: ${target.constructor.name}`);
|
|
|
return originalMethod.apply(this, args);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
const pluginName = pluginConfig.name;
|
|
|
-
|
|
|
+
|
|
|
// 获取或创建插件的命令列表
|
|
|
let plugin = commandList.find((p: Plugin) => p.class === target.constructor);
|
|
|
if (!plugin) {
|
|
@@ -279,7 +210,7 @@ export function runcod(cmd: string | string[], desc: string, config: CommandConf
|
|
|
|
|
|
// 第一个命令作为主命令,其他的作为别名
|
|
|
const [mainCmd, ...aliases] = allCmds;
|
|
|
-
|
|
|
+
|
|
|
// 添加命令
|
|
|
const command: Command = {
|
|
|
cmd: mainCmd,
|
|
@@ -295,22 +226,22 @@ export function runcod(cmd: string | string[], desc: string, config: CommandConf
|
|
|
...(config.template || {})
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
plugin.commands.push(command);
|
|
|
|
|
|
// 修改日志输出
|
|
|
botlogger.info(`注册插件${plugin.id}命令: ${CMD_PREFIX}${plugin.id} ${mainCmd} 成功`);
|
|
|
-
|
|
|
+
|
|
|
// 添加命令标记
|
|
|
descriptor.value.isCommand = true;
|
|
|
descriptor.value.cmd = Array.isArray(cmd) ? cmd[0] : cmd;
|
|
|
descriptor.value.desc = desc;
|
|
|
descriptor.value.aliases = Array.isArray(cmd) ? cmd.slice(1) : [];
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return originalMethod.apply(this, args);
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
return descriptor;
|
|
|
};
|
|
|
}
|
|
@@ -318,7 +249,7 @@ export function runcod(cmd: string | string[], desc: string, config: CommandConf
|
|
|
|
|
|
// 添加定时任务装饰器
|
|
|
export function schedule(cron: string): MethodDecorator {
|
|
|
- return function(target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
|
|
|
+ return function (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
|
|
|
// 保存原始方法
|
|
|
const originalMethod = descriptor.value;
|
|
|
|