From 4467f96b334fb3b0bb8828d5257d3de1370a8ade Mon Sep 17 00:00:00 2001 From: Matyrobbrt Date: Mon, 27 May 2024 20:16:05 +0300 Subject: [PATCH] Add a config option to limit the amount of custom pings an user can have --- .../camelot/config/module/CustomPings.groovy | 6 ++++++ .../camelot/commands/utility/CustomPingsCommand.java | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/config/src/main/groovy/net/neoforged/camelot/config/module/CustomPings.groovy b/config/src/main/groovy/net/neoforged/camelot/config/module/CustomPings.groovy index a29d10d..b642c5d 100644 --- a/config/src/main/groovy/net/neoforged/camelot/config/module/CustomPings.groovy +++ b/config/src/main/groovy/net/neoforged/camelot/config/module/CustomPings.groovy @@ -12,4 +12,10 @@ class CustomPings extends ModuleConfiguration { * The channel in which to create ping private threads if a member does not have DMs enabled. */ long pingThreadsChannel + + /** + * The maximum amount of custom pings an user can have. + * {@code -1} means indefinite + */ + int limit = 100 } diff --git a/src/main/java/net/neoforged/camelot/commands/utility/CustomPingsCommand.java b/src/main/java/net/neoforged/camelot/commands/utility/CustomPingsCommand.java index a5d6165..a181f36 100644 --- a/src/main/java/net/neoforged/camelot/commands/utility/CustomPingsCommand.java +++ b/src/main/java/net/neoforged/camelot/commands/utility/CustomPingsCommand.java @@ -18,6 +18,7 @@ import net.neoforged.camelot.commands.PaginatableCommand; import net.neoforged.camelot.db.schemas.Ping; import net.neoforged.camelot.db.transactionals.PingsDAO; +import net.neoforged.camelot.module.CustomPingsModule; import org.jetbrains.annotations.Nullable; import java.util.List; @@ -66,6 +67,16 @@ protected void execute(SlashCommandEvent event) { return; } + final var pingAmount = Database.pings().withExtension(PingsDAO.class, + db -> db.getAllPingsOf(event.getUser().getIdLong(), event.getGuild().getIdLong())) + .size(); + + final var config = BotMain.getModule(CustomPingsModule.class).config(); + if (config.getLimit() >= 0 && pingAmount >= config.getLimit()) { + event.reply(STR."You have already reached your limit of **\{config.getLimit()}** custom pings.").setEphemeral(true).queue(); + return; + } + Database.pings().useExtension(PingsDAO.class, db -> db.insert( event.getGuild().getIdLong(), event.getUser().getIdLong(), regex, event.getOption("message", "", OptionMapping::getAsString)