Skip to content

Commit

Permalink
feat(commands:clear): add support for regex based message clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
virtual-designer committed Nov 4, 2023
1 parent 2b15c26 commit b126627
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ setup.sh
/.idea
/storage
/extensions
/lib
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
all:
$(MAKE) -C lib

clean:
$(MAKE) -C lib clean
23 changes: 22 additions & 1 deletion src/commands/moderation/ClearCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export default class ClearCommand extends Command {
.addBooleanOption(option => option.setName("filter_embeds").setDescription("Deletes messages that have embeds"))
.addBooleanOption(option =>
option.setName("filter_unverifed_bots").setDescription("Deletes messages from unverified bots")
)
.addStringOption(option =>
option.setName("filter_pattern").setDescription("Deletes messages matching with this regex pattern")
)
.addStringOption(option =>
option.setName("filter_pattern_flags").setDescription("Flags for the regex pattern. Defaults to 'g' (global)")
);

async execute(message: CommandMessage, context: BasicCommandContext): Promise<CommandReturn> {
Expand Down Expand Up @@ -162,14 +168,29 @@ export default class ClearCommand extends Command {

if (message instanceof ChatInputCommandInteraction) {
for (const option of message.options.data) {
if (!option.name.startsWith("filter_")) {
if (!option.name.startsWith("filter_") || !option.value) {
continue;
}

const filter = filters[option.name.replace("filter_", "") as keyof typeof filters];

if (filter) {
filterHandlers.push(filter);
} else {
if (option.name === "filter_pattern" && message.options.getString("filter_pattern")) {
try {
const regex = new RegExp(
message.options.getString("filter_pattern", true),
message.options.getString("filter_pattern_flags") ?? "g"
);

filterHandlers.push((message: Message) => regex.test(message.content));
} catch (e) {
logError(e);
await this.error(message, "Invalid flag(s) supplied for the regex pattern");
return;
}
}
}
}

Expand Down

0 comments on commit b126627

Please sign in to comment.