mirror of
https://github.com/Jerryplusy/crystelf-plugin.git
synced 2025-07-04 14:19:19 +00:00
添加ws接口:获取群聊信息
This commit is contained in:
parent
893387951a
commit
fd360ed29c
@ -2,6 +2,10 @@ import wsClient from '../../models/ws/wsClient.js';
|
|||||||
import configControl from '../config/configControl.js';
|
import configControl from '../config/configControl.js';
|
||||||
|
|
||||||
const botControl = {
|
const botControl = {
|
||||||
|
/**
|
||||||
|
* 获取全部bot信息并同步到core
|
||||||
|
* @returns {Promise<boolean>}
|
||||||
|
*/
|
||||||
async reportBots() {
|
async reportBots() {
|
||||||
const bots = [{ client: configControl.get('coreConfig').wsClientId }];
|
const bots = [{ client: configControl.get('coreConfig').wsClientId }];
|
||||||
|
|
||||||
@ -33,6 +37,33 @@ const botControl = {
|
|||||||
|
|
||||||
return await wsClient.sendMessage(message);
|
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;
|
export default botControl;
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
import botControl from '../../lib/core/botControl.js';
|
||||||
|
import wsClient from './wsClient.js';
|
||||||
|
|
||||||
class Handler {
|
class Handler {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.handlers = new Map([
|
this.handlers = new Map([
|
||||||
@ -5,6 +8,7 @@ class Handler {
|
|||||||
['ping', this.handlePing.bind(this)],
|
['ping', this.handlePing.bind(this)],
|
||||||
['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)],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,6 +41,34 @@ class Handler {
|
|||||||
async handleError(client, msg) {
|
async handleError(client, msg) {
|
||||||
logger.warn(`crystelf WS 错误:${msg.data}`);
|
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();
|
const handler = new Handler();
|
||||||
|
@ -63,6 +63,11 @@ class WsClient {
|
|||||||
await this.sendMessage(authMsg);
|
await this.sendMessage(authMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送信息到ws服务端,自动格式化
|
||||||
|
* @param msg
|
||||||
|
* @returns {Promise<boolean>}
|
||||||
|
*/
|
||||||
async sendMessage(msg) {
|
async sendMessage(msg) {
|
||||||
if (this.ws?.readyState === WebSocket.OPEN) {
|
if (this.ws?.readyState === WebSocket.OPEN) {
|
||||||
this.ws.send(JSON.stringify(msg));
|
this.ws.send(JSON.stringify(msg));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user