Add -match option to !clean
This commit is contained in:
parent
1f52ebef44
commit
1d5dd10bff
2 changed files with 25 additions and 7 deletions
|
@ -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<any>): 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<Promise<Member | null>>(commandTypes.resolvedMember),
|
||||
messageTarget: createTypeHelper<Promise<MessageTarget>>(commandTypes.messageTarget),
|
||||
anyId: createTypeHelper<Promise<string>>(commandTypes.anyId),
|
||||
regex: createTypeHelper<RegExp>(commandTypes.regex),
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue