-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bugs with rate limiting by adding cooldown
* close command * change category command
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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 | ||
|
@@ -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. | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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) | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Zabuzard
Author
Member
|
||
.map(lastCategoryChange -> lastCategoryChange.plus(COOLDOWN_DURATION_VALUE, | ||
COOLDOWN_DURATION_UNIT)) | ||
.filter(Instant.now()::isBefore) | ||
.isPresent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"getIfPresent" sounds like, if present get, otherwise throw.
So should the optional be used here? As I'd assume it's never null?