From 9016ab405a44525617beb8e5d8a15dc049de3186 Mon Sep 17 00:00:00 2001 From: Jerry Date: Wed, 14 May 2025 13:34:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=91=E9=80=81=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=88=B0=E7=BE=A4=E8=81=8A=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/botControl.js | 38 +++++++++++++++++++++++++++++++++----- models/ws/handler.js | 8 ++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/core/botControl.js b/lib/core/botControl.js index 6cf7b4f..3f0768a 100644 --- a/lib/core/botControl.js +++ b/lib/core/botControl.js @@ -41,20 +41,20 @@ const botControl = { /** * 获取群聊信息 - * @param botUin + * @param botId * @param groupId * @returns {Promise<*|null>} */ - async getGroupInfo(botUin, groupId) { - const bot = Bot[botUin]; + async getGroupInfo(botId, groupId) { + const bot = Bot[botId]; if (!bot) { - logger.warn(`未找到bot: ${botUin}`); + logger.warn(`未找到bot: ${botId}`); return null; } const group = bot.pickGroup(groupId); if (!group || typeof group.getInfo !== 'function') { - logger.warn(`Bot ${botUin}中未找到群${groupId}`); + logger.warn(`Bot ${botId}中未找到群${groupId}`); return null; } @@ -65,6 +65,34 @@ const botControl = { return null; } }, + + /** + * 发送信息到群 + * @param botId bot账号 + * @param message 发送的信息 + * @param groupId 群号 + * @returns {Promise} + */ + async sendMessage(botId, message, groupId) { + const bot = Bot[botId]; + if (!bot) { + logger.warn(`未找到bot: ${botId}`); + return false; + } + + const group = bot.pickGroup(groupId); + if (!group || typeof group.getInfo !== 'function') { + logger.warn(`Bot ${botId}中未找到群${groupId}`); + return false; + } + + try { + return !!group.send(message); + } catch (e) { + logger.error(`发送群信息失败:${groupId}..`); + return false; + } + }, }; export default botControl; diff --git a/models/ws/handler.js b/models/ws/handler.js index 4351fac..5ec23ff 100644 --- a/models/ws/handler.js +++ b/models/ws/handler.js @@ -9,6 +9,7 @@ class Handler { ['message', this.handleMessageFromServer.bind(this)], ['error', this.handleError.bind(this)], ['getGroupInfo', this.handleGetGroupInfo.bind(this)], + ['sendMessage', this.handleSendMessage.bind(this)], ]); } @@ -69,6 +70,13 @@ class Handler { }; await wsClient.sendMessage(returnData); } + + async handleSendMessage(client, msg) { + const botId = msg.data?.botId; + const groupId = msg.data?.groupId; + const message = msg.data?.message; + await botControl.sendMessage(botId, message, groupId); + } } const handler = new Handler();