Skip to content

Commit

Permalink
Fixed bugs with rate limiting by adding cooldown
Browse files Browse the repository at this point in the history
* close command
* change category command
  • Loading branch information
Zabuzard committed Jun 15, 2022
1 parent 737e456 commit b797ffa
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.togetherjava.tjbot.commands.help;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.Role;
Expand All @@ -14,7 +16,10 @@
import org.togetherjava.tjbot.commands.SlashCommandVisibility;
import org.togetherjava.tjbot.config.Config;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
* Implements the {@code /change-help-category} command, which is able to change the category of a
Expand All @@ -29,7 +34,11 @@
public final class ChangeHelpCategoryCommand extends SlashCommandAdapter {
private static final String CATEGORY_OPTION = "category";

private static final int COOLDOWN_DURATION_VALUE = 1;
private static final ChronoUnit COOLDOWN_DURATION_UNIT = ChronoUnit.HOURS;

private final HelpSystemHelper helper;
private final Cache<Long, Instant> helpThreadIdToLastCategoryChange;

/**
* Creates a new instance.
Expand All @@ -49,6 +58,11 @@ public ChangeHelpCategoryCommand(@NotNull Config config, @NotNull HelpSystemHelp

getData().addOptions(category);

helpThreadIdToLastCategoryChange = Caffeine.newBuilder()
.maximumSize(1_000)
.expireAfterAccess(COOLDOWN_DURATION_VALUE, TimeUnit.of(COOLDOWN_DURATION_UNIT))
.build();

this.helper = helper;
}

Expand All @@ -66,6 +80,16 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
return;
}

if (isHelpThreadOnCooldown(helpThread)) {
event
.reply("Please wait a bit, this command can only be used once per %d %s."
.formatted(COOLDOWN_DURATION_VALUE, COOLDOWN_DURATION_UNIT))
.setEphemeral(true)
.queue();
return;
}
helpThreadIdToLastCategoryChange.put(helpThread.getIdLong(), Instant.now());

event.deferReply().queue();

helper.renameChannelToCategoryTitle(helpThread, category)
Expand Down Expand Up @@ -96,4 +120,13 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
return action.flatMap(any -> helpThread.sendMessage(headsUpWithoutRole)
.flatMap(message -> message.editMessage(headsUpWithRole)));
}

private boolean isHelpThreadOnCooldown(@NotNull ThreadChannel helpThread) {
return Optional
.ofNullable(helpThreadIdToLastCategoryChange.getIfPresent(helpThread.getIdLong()))

This comment has been minimized.

Copy link
@Tais993

Tais993 Jun 16, 2022

Member

"getIfPresent" sounds like, if present get, otherwise throw.
So should the optional be used here? As I'd assume it's never null?

This comment has been minimized.

Copy link
@Zabuzard

Zabuzard Jun 17, 2022

Author Member

Its a method from caffeine. it gets or returns null instead.

Yeah, its unusual naming.

.map(lastCategoryChange -> lastCategoryChange.plus(COOLDOWN_DURATION_VALUE,
COOLDOWN_DURATION_UNIT))
.filter(Instant.now()::isBefore)
.isPresent();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.togetherjava.tjbot.commands.help;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.ThreadChannel;
Expand All @@ -8,14 +10,23 @@
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
import org.togetherjava.tjbot.commands.SlashCommandVisibility;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
* Implements the {@code /close} command to close question threads.
* <p>
* Can be used in active (non-archived) question threads. Will close, i.e. archive, the thread upon
* use. Meant to be used once a question has been resolved.
*/
public final class CloseCommand extends SlashCommandAdapter {
private static final int COOLDOWN_DURATION_VALUE = 1;
private static final ChronoUnit COOLDOWN_DURATION_UNIT = ChronoUnit.HOURS;

private final HelpSystemHelper helper;
private final Cache<Long, Instant> helpThreadIdToLastClose;

/**
* Creates a new instance.
Expand All @@ -25,6 +36,11 @@ public final class CloseCommand extends SlashCommandAdapter {
public CloseCommand(@NotNull HelpSystemHelper helper) {
super("close", "Close this question thread", SlashCommandVisibility.GUILD);

helpThreadIdToLastClose = Caffeine.newBuilder()
.maximumSize(1_000)
.expireAfterAccess(COOLDOWN_DURATION_VALUE, TimeUnit.of(COOLDOWN_DURATION_UNIT))
.build();

this.helper = helper;
}

Expand All @@ -40,10 +56,28 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
return;
}

if (isHelpThreadOnCooldown(helpThread)) {
event
.reply("Please wait a bit, this command can only be used once per %d %s."
.formatted(COOLDOWN_DURATION_VALUE, COOLDOWN_DURATION_UNIT))
.setEphemeral(true)
.queue();
return;
}
helpThreadIdToLastClose.put(helpThread.getIdLong(), Instant.now());

MessageEmbed embed = new EmbedBuilder().setDescription("Closed the thread.")
.setColor(HelpSystemHelper.AMBIENT_COLOR)
.build();

event.replyEmbeds(embed).flatMap(any -> helpThread.getManager().setArchived(true)).queue();
}

private boolean isHelpThreadOnCooldown(@NotNull ThreadChannel helpThread) {
return Optional.ofNullable(helpThreadIdToLastClose.getIfPresent(helpThread.getIdLong()))
.map(lastCategoryChange -> lastCategoryChange.plus(COOLDOWN_DURATION_VALUE,
COOLDOWN_DURATION_UNIT))
.filter(Instant.now()::isBefore)
.isPresent();
}
}

0 comments on commit b797ffa

Please sign in to comment.