108 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import botControl from '../../lib/core/botControl.js';
import wsClient from './wsClient.js';
class Handler {
constructor() {
this.handlers = new Map([
['auth', this.handleAuth.bind(this)],
['ping', this.handlePing.bind(this)],
['message', this.handleMessageFromServer.bind(this)],
['error', this.handleError.bind(this)],
['getGroupInfo', this.handleGetGroupInfo.bind(this)],
['sendMessage', this.handleSendMessage.bind(this)],
['broadcastMessage', this.broadcastMessage.bind(this)],
['reportBots', this.reportBots.bind(this)],
]);
}
async handle(client, msg) {
const handler = this.handlers.get(msg.type);
if (handler) {
await handler(client, msg);
} else {
logger.warn(`未知消息类型: ${msg.type}`);
}
}
async handleAuth(client, msg) {
if (msg.success) {
logger.mark('crystelf WS 认证成功..');
} else {
logger.error('crystelf WS 认证失败,关闭连接..');
client.ws.close(4001, '认证失败');
}
}
async handlePing(client, msg) {
await client.sendMessage({ type: 'pong' });
}
async handleMessageFromServer(client, msg) {
logger.mark(`crystelf 服务端消息: ${msg.data}`);
}
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);
}
/**
* 发送信息到群聊
* @param client
* @param msg
* @returns {Promise<void>}
*/
// TODO 测试可用性
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);
}
/**
* 广播消息感觉没啥用用core的广播代替了
* @param client
* @param msg
* @returns {Promise<void>}
*/
async broadcastMessage(client, msg) {
const message = msg.data?.message;
await botControl.broadcastMessage(message);
}
async reportBots(client, msg) {
await botControl.reportBots();
}
}
const handler = new Handler();
export default handler;