mirror of
https://github.com/Jerryplusy/crystelf-plugin.git
synced 2025-07-04 14:19:19 +00:00
添加广播接口
This commit is contained in:
parent
f5210c2079
commit
525194f097
@ -4,13 +4,18 @@ export default class ReportBots extends plugin {
|
|||||||
constructor() {
|
constructor() {
|
||||||
super({
|
super({
|
||||||
name: 'crystelf Bot状态上报',
|
name: 'crystelf Bot状态上报',
|
||||||
dsc: '定时上报botID和群聊列表',
|
dsc: '一些操作bot的功能',
|
||||||
rule: [
|
rule: [
|
||||||
{
|
{
|
||||||
reg: '^#crystelf同步$',
|
reg: '^#crystelf同步$',
|
||||||
fnc: 'manualReport',
|
fnc: 'manualReport',
|
||||||
permission: 'master',
|
permission: 'master',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
reg: '^#crystelf广播(.+)$',
|
||||||
|
fnc: 'broadcast',
|
||||||
|
permission: 'master',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
task: [
|
task: [
|
||||||
{
|
{
|
||||||
@ -34,4 +39,20 @@ export default class ReportBots extends plugin {
|
|||||||
e.reply('crystelf Bot同步失败:核心未连接..');
|
e.reply('crystelf Bot同步失败:核心未连接..');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async broadcast(e) {
|
||||||
|
const msg = e?.msg?.match(/^#crystelf广播(.+)$/)?.[1]?.trim();
|
||||||
|
if (!msg) {
|
||||||
|
return e.reply('广播内容不能为空');
|
||||||
|
}
|
||||||
|
|
||||||
|
e.reply(`开始广播消息到所有群(内容:${msg})..`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await botControl.broadcastMessage(msg);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(`广播执行异常: ${err.message}`);
|
||||||
|
return e.reply('广播过程中发生错误,请检查日志..');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ const botControl = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const group = bot.pickGroup(groupId);
|
const group = bot.pickGroup(groupId);
|
||||||
if (!group || typeof group.getInfo !== 'function') {
|
if (!group) {
|
||||||
logger.warn(`Bot ${botId}中未找到群${groupId}`);
|
logger.warn(`Bot ${botId}中未找到群${groupId}`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -81,18 +81,57 @@ const botControl = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const group = bot.pickGroup(groupId);
|
const group = bot.pickGroup(groupId);
|
||||||
if (!group || typeof group.getInfo !== 'function') {
|
if (!group) {
|
||||||
logger.warn(`Bot ${botId}中未找到群${groupId}`);
|
logger.warn(`Bot ${botId}中未找到群${groupId}`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return !!group.send(message);
|
return !!(await group.send(message));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error(`发送群信息失败:${groupId}..`);
|
logger.error(`发送群信息失败:${groupId}..`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 广播消息到所有群聊
|
||||||
|
* @param message 消息
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
async broadcastMessage(message) {
|
||||||
|
const groupMap = new Map();
|
||||||
|
|
||||||
|
for (const bot of Object.values(Bot)) {
|
||||||
|
if (!bot?.uin || !bot.gl) continue;
|
||||||
|
for (const [groupId, groupInfo] of bot.gl.entries()) {
|
||||||
|
if (!groupMap.has(groupId)) {
|
||||||
|
groupMap.set(groupId, []);
|
||||||
|
}
|
||||||
|
groupMap.get(groupId).push(bot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const tasks = [];
|
||||||
|
for (const [groupId, botList] of groupMap.entries()) {
|
||||||
|
const delay = Math.floor(30_000 + Math.random() * 60_000); //30s-90s
|
||||||
|
const bot = botList[Math.floor(Math.random() * botList.length)];
|
||||||
|
const task = setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
const group = bot.pickGroup(groupId);
|
||||||
|
if (!group) {
|
||||||
|
logger.warn(`无法发消息到群${groupId}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await group.send(message);
|
||||||
|
logger.mark(`已广播消息到群 ${groupId}(Bot ${bot.uin}),延迟 ${delay / 1000}s`);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(`广播到群 ${groupId} 失败:${err}`);
|
||||||
|
}
|
||||||
|
}, delay);
|
||||||
|
tasks.push(task);
|
||||||
|
}
|
||||||
|
logger.info(`广播任务已部署,总群数:${groupMap.size}`);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default botControl;
|
export default botControl;
|
||||||
|
@ -10,6 +10,7 @@ class Handler {
|
|||||||
['error', this.handleError.bind(this)],
|
['error', this.handleError.bind(this)],
|
||||||
['getGroupInfo', this.handleGetGroupInfo.bind(this)],
|
['getGroupInfo', this.handleGetGroupInfo.bind(this)],
|
||||||
['sendMessage', this.handleSendMessage.bind(this)],
|
['sendMessage', this.handleSendMessage.bind(this)],
|
||||||
|
['broadcastMessage', this.broadcastMessage.bind(this)],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,6 +84,17 @@ class Handler {
|
|||||||
const message = msg.data?.message;
|
const message = msg.data?.message;
|
||||||
await botControl.sendMessage(botId, message, groupId);
|
await botControl.sendMessage(botId, message, groupId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 广播消息
|
||||||
|
* @param client
|
||||||
|
* @param msg
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
async broadcastMessage(client, msg) {
|
||||||
|
const message = msg.data?.message;
|
||||||
|
await botControl.broadcastMessage(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handler = new Handler();
|
const handler = new Handler();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user