diff --git a/apps/reportBots.js b/apps/reportBots.js new file mode 100644 index 0000000..80af304 --- /dev/null +++ b/apps/reportBots.js @@ -0,0 +1,35 @@ +import botControl from '../lib/core/botControl.js'; + +export default class ReportBots extends plugin { + constructor() { + super({ + name: 'crystelf Bot状态上报', + dsc: '定时上报botID和群聊列表', + rule: [ + { + reg: '^#crystelf同步$', + fnc: 'manualReport', + permission: 'master', + }, + ], + task: { + name: 'crystelf定时同步', + corn: '0 */30 * * * *', + fnc: 'autoReport', + }, + }); + } + + async autoReport() { + await botControl.reportBots(); + } + + async manualReport(e) { + let success = await botControl.reportBots(); + if (success) { + e.reply('crystelf Bot信息已同步到核心..'); + } else { + e.reply('crystelf Bot同步失败:核心未连接..'); + } + } +} diff --git a/lib/core/botControl.js b/lib/core/botControl.js new file mode 100644 index 0000000..03020bf --- /dev/null +++ b/lib/core/botControl.js @@ -0,0 +1,33 @@ +import wsClient from '../../models/ws/wsClient.js'; + +const botControl = { + async reportBots() { + const bots = []; + + for (const bot of Object.values(Bot)) { + if (!bot || !bot.uin) continue; + + const botInfo = { + uin: bot.uin, + groups: [], + }; + let groupsMap = bot.gl; + if (groupsMap) { + for (const [groupId, groupInfo] of groupsMap) { + botInfo.groups.push({ + group_id: groupId, + group_name: groupInfo.group_name || '未知', + }); + } + } + bots.push(botInfo); + } + + return await wsClient.sendMessage({ + type: 'reportBots', + data: bots, + }); + }, +}; + +export default botControl; diff --git a/models/ws/wsClient.js b/models/ws/wsClient.js index 57e110f..7001cfb 100644 --- a/models/ws/wsClient.js +++ b/models/ws/wsClient.js @@ -66,8 +66,10 @@ class WsClient { async sendMessage(msg) { if (this.ws?.readyState === WebSocket.OPEN) { this.ws.send(JSON.stringify(msg)); + return true; } else { logger.warn('crystelf WS 服务器未连接,无法发送消息..'); + return false; } }