(msg: Api.Message)
| 54 | |
| 55 | /** |
| 56 | * 批量向下删除插件 |
| 57 | * 1. 回复一条消息并输入 .bd 来删除从该消息到当前指令之间的所有消息。 |
| 58 | * 2. 输入 .bd <数字> 来删除自己最近的 <数字> 条消息 (最多99条)。 |
| 59 | * 3. 输入 .bd on/off 来切换删除他人消息的权限。 |
| 60 | */ |
| 61 | const bd = async (msg: Api.Message) => { |
| 62 | const client = (msg as any).client; |
| 63 | if (!client) return; |
| 64 | |
| 65 | const chatId = msg.chatId; |
| 66 | const me = await safeGetMe(client); |
| 67 | if (!me) return; |
| 68 | const userId = me.id.toString(); |
| 69 | |
| 70 | // --- 处理开关命令 --- |
| 71 | const args = msg.message?.split(" ") || []; |
| 72 | const subCommand = args[1]?.toLowerCase(); |
| 73 | |
| 74 | if (subCommand === "on" || subCommand === "off") { |
| 75 | const canDeleteOthers = subCommand === "on"; |
| 76 | // 持久化保存设置 |
| 77 | await saveUserSetting(userId, canDeleteOthers); |
| 78 | const status = canDeleteOthers ? "开启" : "关闭"; |
| 79 | const feedbackMsg = await client.sendMessage(chatId, { |
| 80 | message: `✅ 已${status}删除他人消息权限。`, |
| 81 | }); |
| 82 | scheduleTimer(async () => { |
| 83 | await client.deleteMessages(chatId, [feedbackMsg.id, msg.id], { |
| 84 | revoke: true, |
| 85 | }); |
| 86 | }, 2000); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // --- 1. 处理非回复消息的情况 --- |
| 91 | if (!msg.replyTo) { |
| 92 | const numArgStr = args[1] || ""; |
| 93 | const numArg = parseInt(numArgStr, 10); |
| 94 | |
| 95 | // A. 如果是 .bd <数字> |
| 96 | if (!isNaN(numArg) && numArg > 0 && numArg <= 99) { |
| 97 | const messagesToDelete: number[] = [msg.id]; // 包含指令本身 |
| 98 | let count = 0; |
| 99 | |
| 100 | // 获取最近的消息 |
| 101 | const recentMessages = await safeGetMessages(client, chatId, { limit: 100 }); |
| 102 | const filteredMessages = recentMessages.filter((m: Api.Message) => { |
| 103 | return m.senderId?.equals(me.id) && m.id !== msg.id; |
| 104 | }); |
| 105 | |
| 106 | for (let i = 0; i < Math.min(numArg, filteredMessages.length); i++) { |
| 107 | messagesToDelete.push(filteredMessages[i].id); |
| 108 | count++; |
| 109 | } |
| 110 | |
| 111 | // 执行删除 |
| 112 | if (count > 0) { |
| 113 | await client.deleteMessages(chatId, messagesToDelete, { |
nothing calls this directly
no test coverage detected