自动/手动上报bots详情

id,群聊
This commit is contained in:
Jerry 2025-04-28 13:40:56 +08:00
parent 0d44015957
commit 69877be997
3 changed files with 70 additions and 0 deletions

35
apps/reportBots.js Normal file
View File

@ -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同步失败核心未连接..');
}
}
}

33
lib/core/botControl.js Normal file
View File

@ -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;

View File

@ -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;
}
}