diff --git a/apps/coreRestart.js b/apps/coreRestart.js deleted file mode 100644 index ed16173..0000000 --- a/apps/coreRestart.js +++ /dev/null @@ -1,38 +0,0 @@ -import systemControl from '../lib/core/systemControl.js'; -import tools from '../components/tool.js'; -import configControl from '../lib/config/configControl.js'; - -export default class CoreRestart extends plugin { - constructor() { - super({ - name: 'crystelf重启核心', - dsc: '实现核心的重启功能', - rule: [ - { - reg: '^#core重启$', - fnc: 'restart', - permission: 'master', - }, - ], - }); - } - - async restart(e) { - if (!configControl.get()?.core) { - return e.reply(`晶灵核心未启用..`, true); - } - const returnData = await systemControl.systemRestart(); - if (returnData?.data?.success) { - await e.reply(`操作成功:${returnData?.data?.data}..`, true); - } else { - await e.reply(`操作失败:${returnData?.data?.data}..`, true); - } - await tools.sleep(8000); - const restartTime = await systemControl.getRestartTime(); - if (restartTime) { - await e.reply(`晶灵核心重启成功!耗时${restartTime?.data?.data}秒..`, true); - } else { - await e.reply(`核心重启花的时间有点久了呢..${restartTime?.data?.data}`, true); - } - } -} diff --git a/apps/reportBots.js b/apps/reportBots.js deleted file mode 100644 index cd073e8..0000000 --- a/apps/reportBots.js +++ /dev/null @@ -1,69 +0,0 @@ -import botControl from '../lib/core/botControl.js'; -import configControl from '../lib/config/configControl.js'; -import schedule from 'node-schedule'; -import axios from 'axios'; - -export default class ReportBots extends plugin { - constructor() { - super({ - name: 'crystelf Bot状态上报', - dsc: '一些操作bot的功能', - rule: [ - { - reg: '^#crystelf同步$', - fnc: 'manualReport', - permission: 'master', - }, - { - reg: '^#crystelf广播(.+)$', - fnc: 'broadcast', - permission: 'master', - }, - ], - }); - schedule.scheduleJob('*/30 * * * *', () => this.autoReport()); - } - - async autoReport() { - logger.mark(`[crystelf-admin] 正在自动同步bot数据到晶灵核心..`); - if (configControl.get()?.core) { - await botControl.reportBots(); - } - } - - async manualReport(e) { - if (!configControl.get()?.core) { - return e.reply(`[crystelf-admin] 晶灵核心未启用..`, true); - } - let success = await botControl.reportBots(); - if (success) { - await e.reply('Bot信息已同步到核心..', true); - } else { - await e.reply('Bot同步失败:核心未连接..', true); - } - } - - async broadcast(e) { - const msg = e?.msg?.match(/^#crystelf广播(.+)$/)?.[1]?.trim(); - if (!msg) { - return e.reply('广播内容不能为空'); - } - await e.reply(`开始广播消息到所有群..`); - try { - const sendData = { - token: configControl.get()?.coreConfig?.token, - message: msg.toString(), - }; - const url = configControl.get()?.coreConfig?.coreUrl; - const returnData = await axios.post(`${url}/api/bot/broadcast`, sendData); - if (returnData?.data?.success) { - return await e.reply(`操作成功:${returnData?.data.data?.toString()}`); - } else { - return await e.reply(`广播出现错误,请检查日志..`); - } - } catch (err) { - logger.error(`广播执行异常: ${err.message}`); - return await e.reply('广播过程中发生错误,请检查日志..'); - } - } -} diff --git a/lib/core/botControl.js b/lib/core/botControl.js deleted file mode 100644 index 896f84f..0000000 --- a/lib/core/botControl.js +++ /dev/null @@ -1,98 +0,0 @@ -import wsClient from '../../modules/ws/wsClient.js'; -import configControl from '../config/configControl.js'; - -const botControl = { - /** - * 获取全部bot信息并同步到core - * @returns {Promise} - */ - async reportBots() { - const bots = [{ client: configControl.get('coreConfig').wsClientId }]; - - for (const bot of Object.values(Bot)) { - if (!bot || !bot.uin) continue; - - const botInfo = { - uin: bot.uin, - nickName: bot.nickname.replace(/[\u200E-\u200F\u202A-\u202E\u2066-\u2069]/g, ''), - groups: [], - }; - - let groupsMap = bot.gl; - if (groupsMap) { - for (const [groupId, groupInfo] of groupsMap) { - botInfo.groups.push({ - group_id: groupId, - group_name: groupInfo.group_name || '未知', - }); - } - } - - bots.push(botInfo); - } - - const message = { - type: 'reportBots', - data: bots, - }; - - return await wsClient.sendMessage(message); - }, - - /** - * 获取群聊信息 - * @param botId - * @param groupId - * @returns {Promise<*|null>} - */ - async getGroupInfo(botId, groupId) { - const bot = Bot[botId]; - if (!bot) { - logger.warn(`[crystelf-admin] 未找到bot: ${botId}`); - return null; - } - - const group = bot.pickGroup(groupId); - if (!group) { - logger.warn(`[crystelf-admin] Bot ${botId}中未找到群${groupId}`); - return null; - } - - try { - return await group.getInfo(); - } catch (e) { - logger.error(`[crystelf-admin] 获取群聊信息失败:${groupId}..`); - return null; - } - }, - - /** - * 发送信息到群 - * @param botId bot账号 - * @param message 发送的信息 - * @param groupId 群号 - * @returns {Promise} - */ - async sendMessage(botId, message, groupId) { - const bot = Bot[botId]; - if (!bot) { - logger.warn(`[crystelf-admin] 未找到bot: ${botId}`); - return false; - } - - const group = bot.pickGroup(groupId); - if (!group) { - logger.warn(`[crystelf-admin] Bot ${botId}中未找到群${groupId}`); - return false; - } - - try { - return !!(await group.send(message)); - } catch (e) { - logger.error(`[crystelf-admin] 发送群信息失败:${groupId}..`); - return false; - } - }, -}; - -export default botControl; diff --git a/lib/core/systemControl.js b/lib/core/systemControl.js deleted file mode 100644 index e8912bd..0000000 --- a/lib/core/systemControl.js +++ /dev/null @@ -1,20 +0,0 @@ -import configControl from '../config/configControl.js'; -import axios from 'axios'; - -let systemControl = { - async systemRestart() { - const token = configControl.get('coreConfig')?.token; - const coreUrl = configControl.get('coreConfig')?.coreUrl; - const postUrl = coreUrl + '/api/system/restart'; - //logger.info(returnData); - return await axios.post(postUrl, { token: token }); - }, - - async getRestartTime() { - const token = configControl.get('coreConfig')?.token; - const coreUrl = configControl.get('coreConfig')?.coreUrl; - const postUrl = coreUrl + '/api/system/getRestartTime'; - return axios.post(postUrl, { token: token }); - }, -}; -export default systemControl; diff --git a/modules/ws/handler.js b/modules/ws/handler.js deleted file mode 100644 index 89a02a7..0000000 --- a/modules/ws/handler.js +++ /dev/null @@ -1,95 +0,0 @@ -import botControl from '../../lib/core/botControl.js'; -import wsClient from './wsClient.js'; - -class Handler { - constructor() { - this.handlers = new Map([ - ['auth', this.handleAuth.bind(this)], - ['ping', this.handlePing.bind(this)], - ['message', this.handleMessageFromServer.bind(this)], - ['error', this.handleError.bind(this)], - ['getGroupInfo', this.handleGetGroupInfo.bind(this)], - ['sendMessage', this.handleSendMessage.bind(this)], - ['reportBots', this.reportBots.bind(this)], - ]); - } - - async handle(client, msg) { - const handler = this.handlers.get(msg.type); - if (handler) { - await handler(client, msg); - } else { - logger.warn(`未知消息类型: ${msg.type}`); - } - } - - async handleAuth(client, msg) { - if (msg.success) { - logger.mark('crystelf WS 认证成功..'); - } else { - logger.error('crystelf WS 认证失败,关闭连接..'); - client.ws.close(4001, '认证失败'); - } - } - - async handlePing(client, msg) { - await client.sendMessage({ type: 'pong' }); - } - - async handleMessageFromServer(client, msg) { - logger.mark(`crystelf 服务端消息: ${msg.data}`); - } - - async handleError(client, msg) { - logger.warn(`crystelf WS 错误:${msg.data}`); - } - - /** - 获取群聊信息,自动回调 - @examples 请求示例 - ```json - { - requestId: 114514, - type: 'getGroupInfo', - data: { - botId: 114514, - groupId: 114514, - }, - } - ``` - **/ - async handleGetGroupInfo(client, msg) { - const requestId = msg?.requestId; - const botId = msg.data?.botId; - const groupId = msg.data?.groupId; - const type = msg.type + 'Return'; - const groupData = await botControl.getGroupInfo(botId, groupId); - const returnData = { - type: type, - requestId: requestId, - data: groupData, - }; - await wsClient.sendMessage(returnData); - } - - /** - * 发送信息到群聊 - * @param client - * @param msg - * @returns {Promise} - */ - // TODO 测试可用性 - async handleSendMessage(client, msg) { - const botId = Number(msg.data?.botId); - const groupId = Number(msg.data?.groupId); - const message = msg.data?.message?.toString(); - await botControl.sendMessage(botId, message, groupId); - } - - async reportBots(client, msg) { - await botControl.reportBots(); - } -} - -const handler = new Handler(); -export default handler; diff --git a/modules/ws/wsClient.js b/modules/ws/wsClient.js deleted file mode 100644 index 8c3b86f..0000000 --- a/modules/ws/wsClient.js +++ /dev/null @@ -1,95 +0,0 @@ -import WebSocket from 'ws'; -import configControl from '../../lib/config/configControl.js'; -import handler from './handler.js'; - -class WsClient { - constructor() { - this.ws = null; - this.wsURL = null; - this.secret = null; - this.clientId = null; - this.reconnectInterval = null; - this.isReconnecting = false; - } - - async initialize() { - try { - if (this.ws && this.ws.readyState === WebSocket.OPEN) { - logger.mark('crystelf WS 客户端已连接..'); - return; - } - - this.wsURL = configControl.get('config')?.coreConfig?.coreUrl; - this.secret = configControl.get('config')?.coreConfig?.wsSecret; - this.clientId = configControl.get('config')?.coreConfig?.wsClientId; - this.reconnectInterval = configControl.get('config')?.coreConfig?.wsReConnectInterval; - - //logger.info(configControl.get('config')); - this.ws = new WebSocket(this.wsURL); - - this.ws.on('open', () => { - logger.mark('crystelf WS 客户端连接成功..'); - this.authenticate(); - }); - - this.ws.on('message', (raw) => { - try { - const data = JSON.parse(raw); - handler.handle(this, data); - } catch (err) { - logger.err(err); - } - }); - - this.ws.on('error', (err) => { - logger.error('WS 连接错误:', err); - }); - - this.ws.on('close', (code, reason) => { - logger.warn(`crystelf WS 客户端连接断开:${code} - ${reason}`); - this.reconnect(); - }); - } catch (err) { - logger.error(err); - } - } - - async authenticate() { - const authMsg = { - type: 'auth', - secret: this.secret, - clientId: this.clientId, - }; - await this.sendMessage(authMsg); - } - - /** - * 发送信息到ws服务端,自动格式化 - * @param msg - * @returns {Promise} - */ - async sendMessage(msg) { - if (this.ws?.readyState === WebSocket.OPEN) { - this.ws.send(JSON.stringify(msg)); - return true; - } else { - logger.warn('crystelf WS 服务器未连接,无法发送消息..'); - return false; - } - } - - async reconnect() { - if (this.isReconnecting) return; - this.isReconnecting = true; - - logger.mark('crystelf WS 客户端尝试重连..'); - setTimeout(() => { - this.isReconnecting = false; - this.initialize(); - }, this.reconnectInterval); - } -} - -const wsClient = new WsClient(); - -export default wsClient;