Skip to content

Commit

Permalink
fix: prevent ban action spam in a short period of time
Browse files Browse the repository at this point in the history
  • Loading branch information
codedealer committed Jun 20, 2024
1 parent f6a9e4c commit 1762545
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/actions/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class BanAction extends BaseAction {
public name = 'ban';

private deleteMessageSeconds?: number;
private readonly banCooldown = 120_000;

public constructor(reason?: string, params?: Action['params']) {
super(reason, params);
Expand All @@ -19,6 +20,28 @@ class BanAction extends BaseAction {
}

public async run(message: Message) {
// Do not ban the user that was already banned within banCooldown
const actionsAgainstMember =
container.appStore.actionRegistryStore.data.cache[message.guildId!]?.[message.author.id];
if (Array.isArray(actionsAgainstMember && actionsAgainstMember.length > 0)) {
const now = Date.now();
for (let i = actionsAgainstMember.length - 1; i >= 0; i--) {
const action = actionsAgainstMember[i];
// The array is sorted in ascending order, so we can break early
if (now - action.createdTimestamp > this.banCooldown) {
break;
}

if (action.action.name === this.name) {
container.logger.info(
`User ${message.author.username}[${message.author.id}] in ${message.guild?.name}[${message.guild?.id}] has been banned within the cooldown period. No action taken this time.`,
);

return true;
}
}
}

container.logger.debug(
`Banning user ${message.author.username}[${message.author.id}] in ${message.guild?.name}[${message.guild?.id}] | ${(message.channel as TextChannel).name}\nReason: ${this.reason}`,
);
Expand Down

0 comments on commit 1762545

Please sign in to comment.