PluginsFile.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { param, plugins, runcod } from '../lib/decorators.js';
  2. import path from 'path';
  3. import 'reflect-metadata';
  4. import { fileURLToPath } from 'node:url';
  5. import { GroupMessage, PrivateFriendMessage, PrivateGroupMessage } from 'node-napcat-ts';
  6. import botlogger from '../lib/logger.js';
  7. import fs from 'fs/promises';
  8. import { qqBot } from '../app.js';
  9. import { ParamType } from '../interface/plugin.js';
  10. @plugins({
  11. name: "插件文件管理", //插件名称,用于显示在菜单中
  12. version: "1.0.1", //插件版本号,用于显示在菜单中
  13. describe: "可以查看服务器的插件/日志,可以上传插件到群内",
  14. author: "枫叶秋林",//插件作者,用于显示在菜单中
  15. help: { //插件帮助信息,用于显示在菜单中
  16. enabled: true, //是否启用帮助信息
  17. description: "显示帮助信息" //帮助信息描述
  18. }
  19. })
  20. export class PluginsFile {
  21. @runcod(["download", "下载插件"], "下载插件")
  22. async download(
  23. @param("插件名称", ParamType.String) pluName: string,
  24. context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage
  25. ): Promise<any> {
  26. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  27. // 查找插件目录下的文件
  28. const pluginsDir = path.join(__dirname, '..', 'plugins');
  29. try {
  30. const files = await fs.readdir(pluginsDir);
  31. const foundFiles = files.filter(file =>
  32. (file.endsWith('.ts') || file.endsWith('.js')) &&
  33. file !== 'index.ts'
  34. );
  35. // 在服务器日志中输出找到的文件列表
  36. botlogger.info(`找到插件文件:${foundFiles.join(', ')}`);
  37. // 根据文件名查找具体插件
  38. const targetFile = foundFiles.find((file: string) =>
  39. path.parse(file).name.toLowerCase() === pluName.toLowerCase()
  40. );
  41. if (!targetFile) {
  42. return `未找到名为 ${pluName} 的插件`;
  43. }
  44. // 返回文件完整路径
  45. const fullPath = path.join(pluginsDir, targetFile);
  46. //.toString('base64'
  47. const file = Buffer.from(await fs.readFile(fullPath, { encoding: "utf-8" })).toString('base64')
  48. const isGroupMessage = context.message_type === 'group';
  49. if (isGroupMessage && context.group_id) {
  50. await qqBot.upload_group_file({
  51. group_id: Number(context.group_id),
  52. file: 'data:file;base64,' + file,
  53. name: pluName + '.ts'
  54. })
  55. } else {
  56. await qqBot.upload_private_file({
  57. user_id: Number(context.sender.user_id),
  58. file: 'data:file;base64,' + file,
  59. name: pluName + '.ts'
  60. })
  61. }
  62. return '上传成功';
  63. } catch (error) {
  64. botlogger.error('文件查找失败:', error);
  65. return '插件查找服务暂不可用';
  66. }
  67. }
  68. @runcod(["plugins", "插件列表"], "查看插件文件")
  69. async plugins(
  70. context: PrivateFriendMessage | PrivateGroupMessage | GroupMessage
  71. ): Promise<any> {
  72. // pluName += ".ts"
  73. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  74. // 查找插件目录下的文件
  75. const pluginsDir = path.join(__dirname, '..', 'plugins');
  76. try {
  77. const files = await fs.readdir(pluginsDir);
  78. const foundFiles = files.filter(file =>
  79. (file.endsWith('.ts') || file.endsWith('.js')) &&
  80. file !== 'index.ts'
  81. );
  82. foundFiles.forEach((file, index) => {
  83. foundFiles[index] = path.parse(file).name;
  84. })
  85. return `找到插件文件:${foundFiles.join(', ')}`;
  86. } catch (error) {
  87. botlogger.error('文件查找失败:', error);
  88. return '插件查找服务暂不可用';
  89. }
  90. }
  91. }