添加发送信息到群聊接口

This commit is contained in:
Jerry 2025-05-14 13:34:20 +08:00
parent cc9a6261f8
commit 9016ab405a
2 changed files with 41 additions and 5 deletions

View File

@ -41,20 +41,20 @@ const botControl = {
/** /**
* 获取群聊信息 * 获取群聊信息
* @param botUin * @param botId
* @param groupId * @param groupId
* @returns {Promise<*|null>} * @returns {Promise<*|null>}
*/ */
async getGroupInfo(botUin, groupId) { async getGroupInfo(botId, groupId) {
const bot = Bot[botUin]; const bot = Bot[botId];
if (!bot) { if (!bot) {
logger.warn(`未找到bot: ${botUin}`); logger.warn(`未找到bot: ${botId}`);
return null; return null;
} }
const group = bot.pickGroup(groupId); const group = bot.pickGroup(groupId);
if (!group || typeof group.getInfo !== 'function') { if (!group || typeof group.getInfo !== 'function') {
logger.warn(`Bot ${botUin}中未找到群${groupId}`); logger.warn(`Bot ${botId}中未找到群${groupId}`);
return null; return null;
} }
@ -65,6 +65,34 @@ const botControl = {
return null; return null;
} }
}, },
/**
* 发送信息到群
* @param botId bot账号
* @param message 发送的信息
* @param groupId 群号
* @returns {Promise<boolean>}
*/
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; export default botControl;

View File

@ -9,6 +9,7 @@ class Handler {
['message', this.handleMessageFromServer.bind(this)], ['message', this.handleMessageFromServer.bind(this)],
['error', this.handleError.bind(this)], ['error', this.handleError.bind(this)],
['getGroupInfo', this.handleGetGroupInfo.bind(this)], ['getGroupInfo', this.handleGetGroupInfo.bind(this)],
['sendMessage', this.handleSendMessage.bind(this)],
]); ]);
} }
@ -69,6 +70,13 @@ class Handler {
}; };
await wsClient.sendMessage(returnData); 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(); const handler = new Handler();