From d3d9bcca153995fb4eb544238ea70372a40ab23d Mon Sep 17 00:00:00 2001 From: Matyrobbrt Date: Thu, 6 Jun 2024 23:08:43 +0300 Subject: [PATCH] Move unbanning listeners to the moderation module --- .../java/net/neoforged/camelot/BotMain.java | 32 +++---------------- .../neoforged/camelot/commands/Commands.java | 4 ++- .../camelot/module/ModerationModule.java | 28 ++++++++++++++++ 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/neoforged/camelot/BotMain.java b/src/main/java/net/neoforged/camelot/BotMain.java index 46dab96..06620f3 100644 --- a/src/main/java/net/neoforged/camelot/BotMain.java +++ b/src/main/java/net/neoforged/camelot/BotMain.java @@ -4,11 +4,7 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.Activity; -import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.UserSnowflake; -import net.dv8tion.jda.api.exceptions.ErrorHandler; import net.dv8tion.jda.api.hooks.EventListener; -import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.utils.MemberCachePolicy; import net.dv8tion.jda.api.utils.cache.CacheFlag; @@ -19,10 +15,7 @@ import net.neoforged.camelot.config.module.ModuleConfiguration; import net.neoforged.camelot.configuration.Common; import net.neoforged.camelot.configuration.ConfigMigrator; -import net.neoforged.camelot.db.transactionals.PendingUnbansDAO; import net.neoforged.camelot.db.transactionals.StatsDAO; -import net.neoforged.camelot.listener.DismissListener; -import net.neoforged.camelot.module.BuiltInModule; import net.neoforged.camelot.module.StatsModule; import net.neoforged.camelot.module.api.CamelotModule; import net.neoforged.camelot.module.api.ParameterType; @@ -55,7 +48,6 @@ import java.util.ServiceLoader; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Collectors; @@ -271,31 +263,15 @@ public static void main(String[] args) { } forEachModule(module -> module.registerListeners(botBuilder)); - Commands.init(); - botBuilder.addEventListeners(Commands.get()); - instance = botBuilder.build(); - - instance.addEventListener(Commands.get().getSlashCommands().stream() + botBuilder.addEventListeners(Commands.init()); + botBuilder.addEventListeners(Commands.get().getSlashCommands().stream() .flatMap(slash -> Stream.concat(Stream.of(slash), Arrays.stream(slash.getChildren()))) .filter(EventListener.class::isInstance) .toArray()); // A command implementing EventListener shall be treated as a listener - forEachModule(module -> module.setup(instance)); + instance = botBuilder.build(); - EXECUTOR.scheduleAtFixedRate(() -> { - final PendingUnbansDAO db = Database.main().onDemand(PendingUnbansDAO.class); - for (final Guild guild : instance.getGuilds()) { - final List users = db.getUsersToUnban(guild.getIdLong()); - if (!users.isEmpty()) { - for (final long toUnban : users) { - // We do not use allOf because we do not want a deleted user to cause all unbans to fail - guild.unban(UserSnowflake.fromId(toUnban)).reason("rec: Ban expired") - .queue(_ -> {} /* don't remove the entry here, the ModerationActionRecorder should, and if it doesn't, the unban failed so it should be reattempted next minute */, new ErrorHandler() - .handle(ErrorResponse.UNKNOWN_USER, _ -> db.delete(toUnban, guild.getIdLong()))); // User doesn't exist, so don't care about the unban anymore - } - } - } - }, 1, 1, TimeUnit.MINUTES); + forEachModule(module -> module.setup(instance)); } private static void loadConfig(Path config) { diff --git a/src/main/java/net/neoforged/camelot/commands/Commands.java b/src/main/java/net/neoforged/camelot/commands/Commands.java index 198729d..be69514 100644 --- a/src/main/java/net/neoforged/camelot/commands/Commands.java +++ b/src/main/java/net/neoforged/camelot/commands/Commands.java @@ -30,7 +30,7 @@ public class Commands { * Register and setup every valid command. * To remove a command from the bot, simply comment the line where it is added. */ - public static void init() { + public static CommandClient init() { final var builder = new CommandClientBuilder() .setOwnerId(String.valueOf(CamelotConfig.getInstance().getOwner())) .setPrefix(CamelotConfig.getInstance().getPrefix()) @@ -73,6 +73,8 @@ public static void init() { } } } + + return commands; } } diff --git a/src/main/java/net/neoforged/camelot/module/ModerationModule.java b/src/main/java/net/neoforged/camelot/module/ModerationModule.java index 5d96ac5..69ff490 100644 --- a/src/main/java/net/neoforged/camelot/module/ModerationModule.java +++ b/src/main/java/net/neoforged/camelot/module/ModerationModule.java @@ -2,7 +2,13 @@ import com.google.auto.service.AutoService; import com.jagrosh.jdautilities.command.CommandClientBuilder; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.UserSnowflake; +import net.dv8tion.jda.api.exceptions.ErrorHandler; +import net.dv8tion.jda.api.requests.ErrorResponse; import net.neoforged.camelot.BotMain; +import net.neoforged.camelot.Database; import net.neoforged.camelot.commands.moderation.BanCommand; import net.neoforged.camelot.commands.moderation.KickCommand; import net.neoforged.camelot.commands.moderation.ModLogsCommand; @@ -13,8 +19,12 @@ import net.neoforged.camelot.commands.moderation.UnmuteCommand; import net.neoforged.camelot.commands.moderation.WarnCommand; import net.neoforged.camelot.config.module.Moderation; +import net.neoforged.camelot.db.transactionals.PendingUnbansDAO; import net.neoforged.camelot.module.api.CamelotModule; +import java.util.List; +import java.util.concurrent.TimeUnit; + /** * The module that provides moderation commands. */ @@ -39,4 +49,22 @@ public void registerCommands(CommandClientBuilder builder) { new BanCommand(), new UnbanCommand() ); } + + @Override + public void setup(JDA jda) { + BotMain.EXECUTOR.scheduleAtFixedRate(() -> { + final PendingUnbansDAO db = Database.main().onDemand(PendingUnbansDAO.class); + for (final Guild guild : jda.getGuilds()) { + final List users = db.getUsersToUnban(guild.getIdLong()); + if (!users.isEmpty()) { + for (final long toUnban : users) { + // We do not use allOf because we do not want a deleted user to cause all unbans to fail + guild.unban(UserSnowflake.fromId(toUnban)).reason("rec: Ban expired") + .queue(_ -> {} /* don't remove the entry here, the ModerationActionRecorder should, and if it doesn't, the unban failed so it should be reattempted next minute */, new ErrorHandler() + .handle(ErrorResponse.UNKNOWN_USER, _ -> db.delete(toUnban, guild.getIdLong()))); // User doesn't exist, so don't care about the unban anymore + } + } + } + }, 1, 1, TimeUnit.MINUTES); + } }