From fd360ed29c77a277edb49bf16ce598ed2e1e8be6 Mon Sep 17 00:00:00 2001 From: Jerry Date: Sat, 3 May 2025 10:30:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ws=E6=8E=A5=E5=8F=A3=EF=BC=9A?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=BE=A4=E8=81=8A=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/botControl.js | 31 +++++++++++++++++++++++++++++++ models/ws/handler.js | 32 ++++++++++++++++++++++++++++++++ models/ws/wsClient.js | 5 +++++ 3 files changed, 68 insertions(+) diff --git a/lib/core/botControl.js b/lib/core/botControl.js index 41d43e2..7408010 100644 --- a/lib/core/botControl.js +++ b/lib/core/botControl.js @@ -2,6 +2,10 @@ import wsClient from '../../models/ws/wsClient.js'; import configControl from '../config/configControl.js'; const botControl = { + /** + * 获取全部bot信息并同步到core + * @returns {Promise} + */ async reportBots() { const bots = [{ client: configControl.get('coreConfig').wsClientId }]; @@ -33,6 +37,33 @@ const botControl = { return await wsClient.sendMessage(message); }, + + /** + * 获取群聊信息 + * @param botUin + * @param groupId + * @returns {Promise<*|null>} + */ + async getGroupInfo(botUin, groupId) { + const bot = Bot[botUin]; + if (!bot) { + logger.warn(`未找到bot: ${botUin}`); + return null; + } + + const group = bot.pickGroup(groupId); + if (!group || typeof group.getInfo !== 'function') { + logger.warn(`Bot ${botUin}中未找到群${groupId}`); + return null; + } + + try { + return await group.getInfo(); + } catch (e) { + logger.error(`获取群聊信息失败:${groupId}..`); + return null; + } + }, }; export default botControl; diff --git a/models/ws/handler.js b/models/ws/handler.js index 7a6ccae..4351fac 100644 --- a/models/ws/handler.js +++ b/models/ws/handler.js @@ -1,3 +1,6 @@ +import botControl from '../../lib/core/botControl.js'; +import wsClient from './wsClient.js'; + class Handler { constructor() { this.handlers = new Map([ @@ -5,6 +8,7 @@ class Handler { ['ping', this.handlePing.bind(this)], ['message', this.handleMessageFromServer.bind(this)], ['error', this.handleError.bind(this)], + ['getGroupInfo', this.handleGetGroupInfo.bind(this)], ]); } @@ -37,6 +41,34 @@ class Handler { 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); + } } const handler = new Handler(); diff --git a/models/ws/wsClient.js b/models/ws/wsClient.js index 7001cfb..7fd77c5 100644 --- a/models/ws/wsClient.js +++ b/models/ws/wsClient.js @@ -63,6 +63,11 @@ class WsClient { await this.sendMessage(authMsg); } + /** + * 发送信息到ws服务端,自动格式化 + * @param msg + * @returns {Promise} + */ async sendMessage(msg) { if (this.ws?.readyState === WebSocket.OPEN) { this.ws.send(JSON.stringify(msg));