自动获取groupID

This commit is contained in:
Jerry 2025-05-07 13:47:35 +08:00
parent 56ec7b7ecf
commit 86f8a04112
2 changed files with 84 additions and 7 deletions

View File

@ -46,7 +46,6 @@ class BotController {
* ```json * ```json
* { * {
* token: 114514, * token: 114514,
* botId: 114514,
* groupId: 114514 * groupId: 114514
* } * }
* ``` * ```
@ -57,9 +56,8 @@ class BotController {
try { try {
const token = req.body.token; const token = req.body.token;
if (tools.checkToken(token.toString())) { if (tools.checkToken(token.toString())) {
const botId = req.body.botId;
const groupId = req.body.groupId; const groupId = req.body.groupId;
let returnData = await BotService.getGroupInfo({ botId: botId, groupId: groupId }); let returnData = await BotService.getGroupInfo({ groupId: groupId });
if (returnData) { if (returnData) {
await response.success(res, returnData); await response.success(res, returnData);
logger.debug(returnData); logger.debug(returnData);

View File

@ -9,7 +9,7 @@ class BotService {
/** /**
* botId数组 * botId数组
*/ */
public async getBotId() { public async getBotId(): Promise<{ uin: string; nickName: string }[]> {
logger.debug('GetBotId..'); logger.debug('GetBotId..');
const userPath = paths.get('userData'); const userPath = paths.get('userData');
const botsPath = path.join(userPath, '/crystelfBots'); const botsPath = path.join(userPath, '/crystelfBots');
@ -47,13 +47,31 @@ class BotService {
return uins; return uins;
} }
public async getGroupInfo(data: { botId: string; groupId: string }) { /**
*
* @param data
*/
public async getGroupInfo(data: {
botId?: string;
groupId: string;
clientId?: string;
}): Promise<any> {
logger.debug('GetGroupInfo..'); logger.debug('GetGroupInfo..');
const sendBot: string | undefined = data.botId
? data.botId
: await this.getGroupBot(data.groupId);
if (!sendBot) {
logger.warn(`不存在能向群聊${data.groupId}发送消息的Bot!`);
return undefined;
}
let sendData = { let sendData = {
type: 'getGroupInfo', type: 'getGroupInfo',
data: data, data: {
botId: sendBot,
groupId: data.groupId,
clientID: data.clientId ? data.clientId : await this.getBotClient(sendBot),
},
}; };
// TO DO 自动寻找botId对应的Client
const returnData = await wsClientManager.sendAndWait('test', sendData); const returnData = await wsClientManager.sendAndWait('test', sendData);
if (returnData) { if (returnData) {
return returnData; return returnData;
@ -62,6 +80,67 @@ class BotService {
return undefined; return undefined;
} }
} }
/**
* `botId``client`
* @param botId
* @private
*/
private async getBotClient(botId: string): Promise<string | undefined> {
const userPath = paths.get('userData');
const botsPath = path.join(userPath, '/crystelfBots');
const dirData = await fs.readdir(botsPath);
for (const clientId of dirData) {
if (!clientId.endsWith('.json')) continue;
try {
const raw:
| { uin: string; groups: { group_id: string; group_name: string }[]; nickName: string }[]
| undefined = await redisService.fetch('crystelfBots', clientId);
if (!raw) continue;
for (const bot of raw) {
if (bot.uin === botId) {
return clientId;
}
}
} catch (err) {
logger.error(`读取${clientId}出错..`);
}
}
return undefined;
}
/**
* `groupId``botId`
* @param groupId
* @private
*/
private async getGroupBot(groupId: string): Promise<string | undefined> {
const userPath = paths.get('userData');
const botsPath = path.join(userPath, '/crystelfBots');
const dirData = await fs.readdir(botsPath);
for (const clientId of dirData) {
if (!clientId.endsWith('.json')) continue;
try {
const raw:
| { uin: string; groups: { group_id: string; group_name: string }[]; nickName: string }[]
| undefined = await redisService.fetch('crystelfBots', clientId);
if (!raw) continue;
for (const bot of raw) {
if (bot.uin && bot.groups) {
for (const group of bot.groups) {
if (group.group_id === groupId) {
return bot.uin;
}
}
}
}
} catch (err) {
logger.error(`读取${clientId}出错..`);
}
}
return undefined;
}
} }
export default new BotService(); export default new BotService();