UNPKG

koishi-plugin-kbot

Version:
75 lines (74 loc) 3.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initRecall = void 0; const koishi_1 = require("koishi"); const enum_1 = require("../enum"); const utils_1 = require("../utils"); async function initRecall(context) { const recent = {}; context.guild().on('send', (session) => { var _a; const list = recent[_a = session.channelId] || (recent[_a] = []); list.unshift(session.messageId); context.setTimeout(() => (0, koishi_1.remove)(list, session.messageId), koishi_1.Time.hour); }); context .guild() .command('kbot/guildManage.recall <user:string>', '在 1 天时间内批量撤回成员信息 默认1条', { checkArgCount: true, authority: 3, }) .alias('批量撤回') .option('count', '-c <count:number> 指定撤回条数, 需要大于等于1', { fallback: 1, type: /^[1-9]\d*$/ }) .usage('注意: 机器人权限至少为管理员且无法撤回管理员消息') .example('guildManage.recall qq -c 2') .example('批量撤回 qq') .action(async ({ session, options }, user) => { const count = Math.max(+options.count, 1); const botRole = await (0, utils_1.getGroupMemberRole)(session.bot, session.guildId, session.selfId); const qqRole = await (0, utils_1.getGroupMemberRole)(session.bot, session.guildId, user); const delay = context.root.config?.delay?.broadcast || 500; if (enum_1.RoleNumber[botRole] > enum_1.RoleNumber[qqRole]) { const messageList = []; const getHistoryMsg = async (length, messageId) => { const history = await session.bot.internal.getGroupMsgHistory(session.channelId, messageId); if (!history.messages.length) return; const msg = history.messages.filter(message => message.user_id === +user); if (msg.length) messageList.push(...msg.slice(0, length)); messageList.sort((a, b) => b.time - a.time); return history.messages[0].time * 1000; }; while (messageList.length < count) { const lastTime = await getHistoryMsg(count - messageList.length, messageList[messageList.length - 1]?.message_id); if (!lastTime) break; if (lastTime > Date.now() - koishi_1.Time.day) break; } messageList.map(async (message) => { await session.bot.internal.deleteMsg(message.message_id); if (delay) await (0, koishi_1.sleep)(delay); }); } else if (user === session.selfId) { const list = recent[session.channelId]; if (!list) return '机器人近期没有发送消息'; const removal = list.splice(0, count); if (!list.length) delete recent[session.channelId]; for (let index = 0; index < removal.length; index++) { if (index && delay) await (0, koishi_1.sleep)(delay); await session.bot.internal.deleteMsg(removal[index]); } } else { return '机器人权限不足'; } }); } exports.initRecall = initRecall;