From 6ab19c809386fe3ded40295cd6641e80d00033ad Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Wed, 7 Oct 2020 01:44:42 +0200 Subject: [PATCH] Allow clean to hard stop at a certain ID --- backend/src/plugins/Utility/commands/CleanCmd.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/backend/src/plugins/Utility/commands/CleanCmd.ts b/backend/src/plugins/Utility/commands/CleanCmd.ts index 717f0ddc..635d3e92 100644 --- a/backend/src/plugins/Utility/commands/CleanCmd.ts +++ b/backend/src/plugins/Utility/commands/CleanCmd.ts @@ -8,6 +8,7 @@ import { GuildPluginData } from "knub"; import { SavedMessage } from "../../../data/entities/SavedMessage"; import { LogType } from "../../../data/LogType"; import { allowTimeout } from "../../../RegExpRunner"; +import { snowflakeToTimestamp } from "src/utils/snowflakeToTimestamp"; const MAX_CLEAN_COUNT = 150; const MAX_CLEAN_TIME = 1 * DAYS; @@ -63,6 +64,7 @@ export const CleanCmd = utilityCmd({ bots: ct.switchOption({ shortcut: "b" }), "has-invites": ct.switchOption({ shortcut: "i" }), match: ct.regex({ option: true, shortcut: "m" }), + "to-id": ct.anyId({ option: true, shortcut: "id" }), }, async run({ message: msg, args, pluginData }) { @@ -95,6 +97,8 @@ export const CleanCmd = utilityCmd({ const messagesToClean = []; let beforeId = msg.id; const timeCutoff = msg.timestamp - MAX_CLEAN_TIME; + const upToMsgId = args["to-id"]; + let foundId = false; while (messagesToClean.length < args.count) { const potentialMessagesToClean = await pluginData.state.savedMessages.getLatestByChannelBeforeId( @@ -110,6 +114,10 @@ export const CleanCmd = utilityCmd({ if (args.user && message.user_id !== args.user) continue; if (args.bots && !message.is_bot) continue; if (args["has-invites"] && getInviteCodesInString(contentString).length === 0) continue; + if (upToMsgId != null && message.id < upToMsgId) { + foundId = true; + break; + } if (moment.utc(message.posted_at).valueOf() < timeCutoff) continue; if (args.match && !(await pluginData.state.regexRunner.exec(args.match, contentString).catch(allowTimeout))) { continue; @@ -123,7 +131,10 @@ export const CleanCmd = utilityCmd({ beforeId = potentialMessagesToClean[potentialMessagesToClean.length - 1].id; - if (moment.utc(potentialMessagesToClean[potentialMessagesToClean.length - 1].posted_at).valueOf() < timeCutoff) { + if ( + foundId || + moment.utc(potentialMessagesToClean[potentialMessagesToClean.length - 1].posted_at).valueOf() < timeCutoff + ) { break; } }