Browse Source

支持聊天文本安装插件

sakulin 3 months ago
parent
commit
ddd12217d6
1 changed files with 45 additions and 24 deletions
  1. 45 24
      src/lib/Plugins.ts

+ 45 - 24
src/lib/Plugins.ts

@@ -1,8 +1,12 @@
-
 import botlogger from "./logger.js";
 import botlogger from "./logger.js";
-import { promises as fsPromises } from 'fs';
-import { HtmlImg } from "./Puppeteer.js";
-import type { GroupMessage, EventHandleMap as MessageContext, PrivateFriendMessage, PrivateGroupMessage } from 'node-napcat-ts/dist/Interfaces.js';
+import {promises as fsPromises} from 'fs';
+import {HtmlImg} from "./Puppeteer.js";
+import type {
+    GroupMessage,
+    EventHandleMap as MessageContext,
+    PrivateFriendMessage,
+    PrivateGroupMessage
+} from 'node-napcat-ts/dist/Interfaces.js';
 import * as cron from 'node-cron';
 import * as cron from 'node-cron';
 import 'reflect-metadata';
 import 'reflect-metadata';
 import {
 import {
@@ -14,13 +18,13 @@ import {
 import * as fs from 'fs'
 import * as fs from 'fs'
 import * as path from 'path'
 import * as path from 'path'
 // 获取指令前缀
 // 获取指令前缀
-import { Botconfig as config, PermissionConfig } from './config.js'
-import { ImageSegment, ReplySegment, TextSegment } from "node-napcat-ts/dist/Structs.js";
-import { fileURLToPath } from 'node:url';
-import { qqBot } from "../app.js";
-import { count } from "node:console";
-import { IsPermission } from "./Permission.js";
-import { download } from "./download.js";
+import {Botconfig as config, PermissionConfig} from './config.js'
+import {ImageSegment, ReplySegment, TextSegment} from "node-napcat-ts/dist/Structs.js";
+import {fileURLToPath} from 'node:url';
+import {qqBot} from "../app.js";
+import {count} from "node:console";
+import {IsPermission} from "./Permission.js";
+import {download} from "./download.js";
 
 
 //WSSendParam
 //WSSendParam
 
 
@@ -29,7 +33,7 @@ const CMD_PREFIX = config?.cmd?.prefix ?? '#';
 // 导出装饰器
 // 导出装饰器
 // export { param, ParamType };
 // export { param, ParamType };
 // export const plugins = pluginsDecorator;
 // export const plugins = pluginsDecorator;
-export { config };  // 导出配置对象
+export {config};  // 导出配置对象
 // 创建消息工厂函数
 // 创建消息工厂函数
 function createTextMessage(text: string): TextSegment {
 function createTextMessage(text: string): TextSegment {
     return {
     return {
@@ -81,7 +85,6 @@ function findCommand(plugin: Plugin, cmdName: string): Command | undefined {
 }
 }
 
 
 
 
-
 // 添加插件加载函数
 // 添加插件加载函数
 async function loadPlugins(): Promise<void> {
 async function loadPlugins(): Promise<void> {
     try {
     try {
@@ -179,7 +182,6 @@ async function initializeScheduledTasks(instance: any): Promise<void> {
 }
 }
 
 
 
 
-
 // 修改 runplugins 函数
 // 修改 runplugins 函数
 export async function runplugins() {
 export async function runplugins() {
     try {
     try {
@@ -207,13 +209,11 @@ export async function runplugins() {
                     const file = context.message[0].data;
                     const file = context.message[0].data;
                     botlogger.info("收到文件消息:" + JSON.stringify(file));
                     botlogger.info("收到文件消息:" + JSON.stringify(file));
                     if (file.file.includes(".ts")) {
                     if (file.file.includes(".ts")) {
-                        let isAdmin = false
-                        // 使用 some 方法简化循环逻辑,只要数组中有一个元素满足条件就返回 true
-                        isAdmin = PermissionConfig.admins.some((admin: string) => admin === String(context.sender.user_id));
+                        let isAdmin = PermissionConfig.admins.some((admin: string) => admin === String(context.sender.user_id));
                         if (!isAdmin) {
                         if (!isAdmin) {
                             context.quick_action([{
                             context.quick_action([{
                                 type: 'text',
                                 type: 'text',
-                                data: { text: `无权限,无法加载插件` }
+                                data: {text: `无权限,无法加载插件`}
                             }]);
                             }]);
                             return;
                             return;
                         }
                         }
@@ -222,17 +222,38 @@ export async function runplugins() {
                         botlogger.info("下载完成:" + JSON.stringify(file));
                         botlogger.info("下载完成:" + JSON.stringify(file));
                         context.quick_action([{
                         context.quick_action([{
                             type: 'text',
                             type: 'text',
-                            data: { text: `插件下载完成,开始重载` }
+                            data: {text: `插件下载完成,开始重载`}
                         }]);
                         }]);
                     }
                     }
                     return;
                     return;
                 }
                 }
 
 
-
                 if (context.message[0].type !== 'text') {
                 if (context.message[0].type !== 'text') {
                     return;
                     return;
                 }
                 }
                 const msg = context.message[0].data.text || '';
                 const msg = context.message[0].data.text || '';
+
+                if (msg.startsWith("//PLUGIN ")) {
+                    const endOfLine = msg.indexOf("\n");
+                    if (endOfLine !== -1) {
+                        const pluginName = msg.substring(9).trim();
+                        if (pluginName.endsWith(".ts")) {
+
+                            botlogger.info("开始安装插件: " + pluginName);
+
+                            // @ts-ignore
+                            fs.writeFile(`../plugins/${pluginName}`, msg, "utf8", (e) => {
+                                botlogger.error("插件安装失败: " + JSON.stringify(e));
+                            });
+
+                            context.quick_action([{
+                                type: 'text',
+                                data: {text: `插件下载完成,开始重载`}
+                            }]);
+                        }
+                    }
+                }
+
                 botlogger.info('收到消息:' + context.message[0].data.text);
                 botlogger.info('收到消息:' + context.message[0].data.text);
                 // 检查是否是命令
                 // 检查是否是命令
                 if (!msg.startsWith(CMD_PREFIX)) {
                 if (!msg.startsWith(CMD_PREFIX)) {
@@ -282,7 +303,7 @@ export async function runplugins() {
                         botlogger.info(`[${context.user_id}]无权限执行命令: ${CMD_PREFIX}${plugin.id} ${command.cmd}`);
                         botlogger.info(`[${context.user_id}]无权限执行命令: ${CMD_PREFIX}${plugin.id} ${command.cmd}`);
                         context.quick_action([{
                         context.quick_action([{
                             type: 'text',
                             type: 'text',
-                            data: { text: `你没有权限执行此命令` }
+                            data: {text: `你没有权限执行此命令`}
                         }]);
                         }]);
                         return;
                         return;
                     }
                     }
@@ -292,7 +313,7 @@ export async function runplugins() {
                         botlogger.info(`[${context.group_id}]无权限执行命令: ${CMD_PREFIX}${plugin.id} ${command.cmd}`);
                         botlogger.info(`[${context.group_id}]无权限执行命令: ${CMD_PREFIX}${plugin.id} ${command.cmd}`);
                         context.quick_action([{
                         context.quick_action([{
                             type: 'text',
                             type: 'text',
-                            data: { text: `你没有权限执行此命令` }
+                            data: {text: `你没有权限执行此命令`}
                         }])
                         }])
                         return;
                         return;
                     }
                     }
@@ -304,7 +325,7 @@ export async function runplugins() {
                 botlogger.error("处理消息时出错:", error);
                 botlogger.error("处理消息时出错:", error);
                 await context.quick_action([{
                 await context.quick_action([{
                     type: 'text',
                     type: 'text',
-                    data: { text: `处理消息时出错: ${error instanceof Error ? error.message : '未知错误'}` }
+                    data: {text: `处理消息时出错: ${error instanceof Error ? error.message : '未知错误'}`}
                 }]);
                 }]);
             }
             }
         });
         });
@@ -470,7 +491,7 @@ async function handleCommand(context: PrivateFriendMessage | PrivateGroupMessage
         const errorMessage = error instanceof Error ? error.message : '未知错误';
         const errorMessage = error instanceof Error ? error.message : '未知错误';
         await context.quick_action([{
         await context.quick_action([{
             type: 'text',
             type: 'text',
-            data: { text: `执行命令时出错: ${errorMessage}` }
+            data: {text: `执行命令时出错: ${errorMessage}`}
         }]);
         }]);
     }
     }
 }
 }