From 1d5dd10bffb169465931fa6386d8a7a341cb55ce Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Mon, 10 Aug 2020 01:09:45 +0300 Subject: [PATCH] Add -match option to !clean --- backend/src/commandTypes.ts | 10 +++++++++ .../src/plugins/Utility/commands/CleanCmd.ts | 22 +++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/backend/src/commandTypes.ts b/backend/src/commandTypes.ts index 74919110..3c3cb1cb 100644 --- a/backend/src/commandTypes.ts +++ b/backend/src/commandTypes.ts @@ -16,6 +16,7 @@ import { baseTypeConverters, baseTypeHelpers, CommandContext, TypeConversionErro import { createTypeHelper } from "knub-command-manager"; import { getChannelIdFromMessageId } from "./data/getChannelIdFromMessageId"; import { MessageTarget, resolveMessageTarget } from "./utils/resolveMessageTarget"; +import { inputPatternToRegExp } from "./validatorUtils"; export const commandTypes = { ...baseTypeConverters, @@ -84,6 +85,14 @@ export const commandTypes = { throw new TypeConversionError(`Could not parse ID: \`${disableInlineCode(value)}\``); }, + + regex(value: string, context: CommandContext): RegExp { + try { + return inputPatternToRegExp(value); + } catch (e) { + throw new TypeConversionError(`Could not parse RegExp: \`${disableInlineCode(e.message)}\``); + } + }, }; export const commandTypeHelpers = { @@ -95,4 +104,5 @@ export const commandTypeHelpers = { resolvedMember: createTypeHelper>(commandTypes.resolvedMember), messageTarget: createTypeHelper>(commandTypes.messageTarget), anyId: createTypeHelper>(commandTypes.anyId), + regex: createTypeHelper(commandTypes.regex), }; diff --git a/backend/src/plugins/Utility/commands/CleanCmd.ts b/backend/src/plugins/Utility/commands/CleanCmd.ts index 5d01ca72..e3341c5f 100644 --- a/backend/src/plugins/Utility/commands/CleanCmd.ts +++ b/backend/src/plugins/Utility/commands/CleanCmd.ts @@ -7,6 +7,7 @@ import moment from "moment-timezone"; import { PluginData } from "knub"; import { SavedMessage } from "../../../data/entities/SavedMessage"; import { LogType } from "../../../data/LogType"; +import { allowTimeout } from "../../../RegExpRunner"; const MAX_CLEAN_COUNT = 150; const MAX_CLEAN_TIME = 1 * DAYS; @@ -61,6 +62,7 @@ export const CleanCmd = utilityCmd({ channel: ct.channelId({ option: true, shortcut: "c" }), bots: ct.switchOption({ shortcut: "b" }), "has-invites": ct.switchOption({ shortcut: "i" }), + match: ct.regex({ option: true, shortcut: "m" }), }, async run({ message: msg, args, pluginData }) { @@ -102,13 +104,19 @@ export const CleanCmd = utilityCmd({ ); if (potentialMessagesToClean.length === 0) break; - const filtered = potentialMessagesToClean.filter(message => { - if (args.user && message.user_id !== args.user) return false; - if (args.bots && !message.is_bot) return false; - if (args["has-invites"] && getInviteCodesInString(message.data.content || "").length === 0) return false; - if (moment.utc(message.posted_at).valueOf() < timeCutoff) return false; - return true; - }); + const filtered = []; + for (const message of potentialMessagesToClean) { + const contentString = message.data.content || ""; + 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 (moment.utc(message.posted_at).valueOf() < timeCutoff) continue; + if (args.match && !(await pluginData.state.regexRunner.exec(args.match, contentString).catch(allowTimeout))) { + continue; + } + + filtered.push(message); + } const remaining = args.count - messagesToClean.length; const withoutOverflow = filtered.slice(0, remaining); messagesToClean.push(...withoutOverflow);