decorators.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import * as path from 'path';
  2. import botlogger from './logger.js';
  3. import type {WSSendParam as MessageContext } from 'node-napcat-ts/dist/Interfaces.js';
  4. // 读取配置文件
  5. import {Botconfig as config} from './config.js'
  6. const CMD_PREFIX = config?.cmd?.prefix??'#';
  7. import { fileURLToPath } from 'node:url';
  8. import { assert } from 'node:console';
  9. // 参数类型枚举
  10. export const ParamType = {
  11. String: "string" as const,
  12. Number: "number" as const,
  13. Boolean: "boolean" as const,
  14. Rest: "rest" as const
  15. } as const;
  16. // 从对象值中获取类型
  17. export type ParamType = typeof ParamType[keyof typeof ParamType];
  18. // 参数元数据接口
  19. interface ParamMetadata {
  20. name: string; // 参数名称
  21. type: ParamType; // 参数类型
  22. index: number; // 参数索引
  23. optional: boolean; // 是否可选
  24. }
  25. // 存储参数元数据
  26. export const paramMetadata = new Map<Function, ParamMetadata[]>();
  27. // 修改参数装饰器
  28. export function param(name: string, type: ParamType = ParamType.String, optional:boolean = false): ParameterDecorator {
  29. return function(target: any, propertyKey: string | symbol | undefined, parameterIndex: number): void {
  30. const actualPropertyKey = propertyKey!;
  31. const fn = target[actualPropertyKey];
  32. let metadata = paramMetadata.get(fn);
  33. if (!metadata) {
  34. metadata = [];
  35. paramMetadata.set(fn, metadata);
  36. }
  37. const paramData: ParamMetadata = {
  38. name,
  39. type,
  40. index: parameterIndex,
  41. optional: optional
  42. };
  43. metadata[parameterIndex] = paramData;
  44. };
  45. }
  46. // 更新 Plugin 接口
  47. export interface Plugin {
  48. id: string;
  49. name: string;
  50. commands: Command[];
  51. class: any;
  52. version?: string;
  53. author?: string;
  54. describe?: string;
  55. }
  56. // 修改插件装饰器配置接口
  57. export interface PluginConfig {
  58. id: string;// 插件ID
  59. name: string;// 插件名称
  60. version?: string;// 插件版本
  61. describe?: string;// 插件描述
  62. author?: string;// 插件作者
  63. help?: {// 帮助命令配置,使用框架默认的帮助配置
  64. enabled?: boolean; // 是否启用帮助命令
  65. command?: string[]; // 帮助命令
  66. description?: string; // 帮助命令描述
  67. };
  68. default?: () => any; // 默认函数
  69. }
  70. // 在 decorators.ts 中定义统一的接口
  71. export interface Command {
  72. cmd: string; // 命令名称
  73. desc: string; // 命令描述
  74. fn: Function; // 命令函数
  75. aliases?: string[]; // 命令别名
  76. cmdPrefix: string; // 命令前缀
  77. pluginId: string; // 插件ID
  78. class: new () => any;
  79. template?: {
  80. enabled: boolean; // 是否启用模板
  81. sendText: boolean; // 是否发送文本
  82. [key: string]: any; // 其他模板配置
  83. };
  84. }
  85. // 修改插件装饰器
  86. export function plugins(config: PluginConfig): ClassDecorator {
  87. return function (target: any): void {
  88. // 保存插件配置
  89. target.prototype.plugincfg = config;
  90. // 确保插件名称正确
  91. const existingPlugin = commandList.find((x) => x.class === target);
  92. let plugin: Plugin;
  93. if (existingPlugin) {
  94. // 更新现有插件的配置
  95. existingPlugin.name = config.name;
  96. plugin = existingPlugin;
  97. } else {
  98. // 添加新插件
  99. plugin = {
  100. id: config.id,
  101. name: config.name,
  102. class: target,
  103. commands: [] as Command[]
  104. };
  105. commandList.push(plugin);
  106. // 添加调试日志
  107. botlogger.info(`发现插件: ${config.name}[${config.version}],插件类: ${target.name}`);
  108. }
  109. // 添加帮助命令
  110. if (config.help?.enabled !== false) {
  111. const helpCommand: Command = {
  112. cmd: 'help',
  113. desc: config.help?.description || "显示帮助信息",
  114. aliases: ['帮助', '?'],
  115. template: {
  116. enabled: true,
  117. sendText: false
  118. },
  119. cmdPrefix: CMD_PREFIX,
  120. pluginId: config.id,
  121. class: target,
  122. fn: async function(): Promise<object> {
  123. const plugin = commandList.find(p => p.class === target);
  124. if (!plugin) {
  125. return {
  126. message: "错误: 找不到插件信息"
  127. };
  128. }
  129. // 过滤并格式化命令列表
  130. const commands = plugin.commands
  131. .filter(cmd => !cmd.cmd.endsWith('help'))
  132. .map(cmd => {
  133. const fullCmd = `${CMD_PREFIX}${plugin.id} ${cmd.cmd}`;
  134. const aliases = cmd.aliases?.map(alias =>
  135. `${CMD_PREFIX}${plugin.id} ${alias}`
  136. ) || [];
  137. return {
  138. name: cmd.cmd,
  139. fullCmd,
  140. desc: cmd.desc,
  141. aliases
  142. };
  143. });
  144. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  145. // 返回支持模板的响应对象
  146. return {
  147. title: plugin.name,
  148. version: config.version || '',
  149. description: config.describe || '',
  150. author: config.author || '',
  151. commands,
  152. pluginId: plugin.id,
  153. cmdPrefix: CMD_PREFIX,
  154. template: {
  155. name: 'help',
  156. path: path.join(__dirname, '..', 'resources', 'help', 'help.html'),
  157. enabled: true,
  158. sendText: false,
  159. render: {
  160. width: 800,
  161. height: 600,
  162. type: 'png',
  163. quality: 100,
  164. fullPage: false,
  165. background: true
  166. }
  167. },
  168. toString() {
  169. const commandsText = commands.map(cmd => {
  170. let text = `${cmd.fullCmd} - ${cmd.desc}`;
  171. if (cmd.aliases.length > 0) {
  172. text += `\n 别名: ${cmd.aliases.join(', ')}`;
  173. }
  174. return text;
  175. }).join('\n');
  176. return [
  177. `=== ${plugin.name} ===`,
  178. `版本: ${config.version}`,
  179. config.describe ? `描述: ${config.describe}` : '',
  180. config.author ? `作者: ${config.author}` : '',
  181. '',
  182. '可用命令:',
  183. commandsText
  184. ].filter(Boolean).join('\n');
  185. }
  186. };
  187. }
  188. };
  189. plugin.commands.push(helpCommand);
  190. botlogger.info(`成功注册[${plugin.id}]帮助命令: ${CMD_PREFIX}${plugin.id} help`);
  191. }
  192. if (!config.default) {
  193. config.default = () => ({redirect: "help"});
  194. }
  195. };
  196. }
  197. // 存储命令的数组
  198. export const commandList: Plugin[] = [];
  199. // 添加模板配置接口
  200. interface TemplateConfig {
  201. enabled: boolean;
  202. sendText: boolean;
  203. [key: string]: any;
  204. }
  205. // 修改命令装饰器配置
  206. interface CommandConfig {
  207. template?: TemplateConfig;
  208. [key: string]: any;
  209. }
  210. // 修改 runcod 装饰器
  211. /**
  212. * 运行命令装饰器
  213. * @param cmd - 命令名称或别名数组
  214. * @param desc - 命令描述
  215. * @param config - 命令配置
  216. */
  217. //权限
  218. export function runcod(cmd: string | string[], desc: string, config: CommandConfig = {}, IsTest = false): MethodDecorator {
  219. return function(target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
  220. // 延迟执行命令注册
  221. const originalMethod = descriptor.value;
  222. descriptor.value = function(...args: any[]) {
  223. // 获取插件配置
  224. const pluginConfig = target.constructor.prototype.plugincfg;
  225. if (!pluginConfig) {
  226. botlogger.error(`未找到插件配置: ${target.constructor.name}`);
  227. return originalMethod.apply(this, args);
  228. }
  229. const pluginName = pluginConfig.name;
  230. // 获取或创建插件的命令列表
  231. let plugin = commandList.find((p: Plugin) => p.class === target.constructor);
  232. if (!plugin) {
  233. plugin = {
  234. id: pluginConfig.id,
  235. name: pluginName,
  236. commands: [] as Command[],
  237. class: target.constructor
  238. };
  239. commandList.push(plugin);
  240. botlogger.info(`创建新插件: ${pluginName}`);
  241. }
  242. // 如果命令还没有注册
  243. if (!descriptor.value.isCommand) {
  244. const cmdList = Array.isArray(cmd) ? cmd : [cmd];
  245. const allCmds = cmdList.map(c => c);
  246. // 第一个命令作为主命令,其他的作为别名
  247. const [mainCmd, ...aliases] = allCmds;
  248. // 添加命令
  249. const command: Command = {
  250. cmd: mainCmd,
  251. desc,
  252. fn: descriptor.value,
  253. aliases,
  254. cmdPrefix: CMD_PREFIX,
  255. pluginId: plugin.id,
  256. class: target.constructor,
  257. template: {
  258. enabled: !IsTest,
  259. sendText: IsTest, // 默认不发送文本,
  260. ...(config.template || {})
  261. }
  262. };
  263. plugin.commands.push(command);
  264. // 修改日志输出
  265. botlogger.info(`注册插件${plugin.id}命令: ${CMD_PREFIX}${plugin.id} ${mainCmd} 成功`);
  266. // 添加命令标记
  267. descriptor.value.isCommand = true;
  268. descriptor.value.cmd = Array.isArray(cmd) ? cmd[0] : cmd;
  269. descriptor.value.desc = desc;
  270. descriptor.value.aliases = Array.isArray(cmd) ? cmd.slice(1) : [];
  271. }
  272. return originalMethod.apply(this, args);
  273. };
  274. return descriptor;
  275. };
  276. }
  277. // 添加定时任务装饰器
  278. export function schedule(cron: string): MethodDecorator {
  279. return function(target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
  280. // 保存原始方法
  281. const originalMethod = descriptor.value;
  282. // 添加定时任务标记
  283. descriptor.value.isScheduled = true;
  284. descriptor.value.cron = cron;
  285. descriptor.value.methodName = propertyKey;
  286. // 返回修改后的描述符
  287. return descriptor;
  288. };
  289. }