Skip to content

Commit

Permalink
Move unbanning listeners to the moderation module
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Jun 6, 2024
1 parent 139565a commit d3d9bcc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
32 changes: 4 additions & 28 deletions src/main/java/net/neoforged/camelot/BotMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Long> 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) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/neoforged/camelot/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -73,6 +73,8 @@ public static void init() {
}
}
}

return commands;
}

}
28 changes: 28 additions & 0 deletions src/main/java/net/neoforged/camelot/module/ModerationModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand All @@ -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<Long> 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);
}
}

0 comments on commit d3d9bcc

Please sign in to comment.