Skip to content

Commit

Permalink
make ModerationService a spring bean
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Feb 12, 2024
1 parent 3876a40 commit 548a7a9
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.discordjug.javabot.systems.help.HelpExperienceService;
import net.discordjug.javabot.systems.help.model.HelpAccount;
import net.discordjug.javabot.systems.moderation.ModerationService;
import net.discordjug.javabot.systems.moderation.warn.dao.WarnRepository;
import net.discordjug.javabot.systems.qotw.QOTWPointsService;
import net.discordjug.javabot.systems.qotw.model.QOTWAccount;
import net.discordjug.javabot.util.Pair;
Expand Down Expand Up @@ -44,7 +43,7 @@ public class UserProfileController extends CaffeineCache<Pair<Long, Long>, UserP
private final DataSource dataSource;
private final BotConfig botConfig;
private final HelpExperienceService helpExperienceService;
private final WarnRepository warnRepository;
private final ModerationService moderationService;

/**
* The constructor of this class which initializes the {@link Caffeine} cache.
Expand All @@ -54,10 +53,10 @@ public class UserProfileController extends CaffeineCache<Pair<Long, Long>, UserP
* @param botConfig The main configuration of the bot
* @param dataSource A factory for connections to the main database
* @param helpExperienceService Service object that handles Help Experience Transactions.
* @param warnRepository DAO for interacting with the set of {@link Warn} objects.
* @param moderationService Service object for moderating members
*/
@Autowired
public UserProfileController(final JDA jda, QOTWPointsService qotwPointsService, BotConfig botConfig, DataSource dataSource, HelpExperienceService helpExperienceService, WarnRepository warnRepository) {
public UserProfileController(final JDA jda, QOTWPointsService qotwPointsService, BotConfig botConfig, DataSource dataSource, HelpExperienceService helpExperienceService, ModerationService moderationService) {
super(Caffeine.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.build()
Expand All @@ -67,7 +66,7 @@ public UserProfileController(final JDA jda, QOTWPointsService qotwPointsService,
this.dataSource = dataSource;
this.botConfig = botConfig;
this.helpExperienceService = helpExperienceService;
this.warnRepository = warnRepository;
this.moderationService = moderationService;
}

/**
Expand Down Expand Up @@ -108,8 +107,7 @@ public ResponseEntity<UserProfileData> getUserProfile(
HelpAccount helpAccount = helpExperienceService.getOrCreateAccount(user.getIdLong());
data.setHelpAccount(HelpAccountData.of(botConfig, helpAccount, guild));
// User Warns
ModerationService moderationService = new ModerationService(null, botConfig.get(guild), warnRepository, null);
data.setWarns(moderationService.getTotalSeverityWeight(user.getIdLong()).contributingWarns());
data.setWarns(moderationService.getTotalSeverityWeight(guild, user.getIdLong()).contributingWarns());
// Insert into cache
getCache().put(new Pair<>(guild.getIdLong(), user.getIdLong()), data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.extern.slf4j.Slf4j;
import net.discordjug.javabot.data.config.BotConfig;
import net.discordjug.javabot.systems.moderation.warn.dao.WarnRepository;
import net.discordjug.javabot.systems.moderation.warn.model.WarnSeverity;
import net.discordjug.javabot.systems.notification.NotificationService;
import net.discordjug.javabot.util.ExceptionLogger;
Expand All @@ -27,7 +26,6 @@
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -48,21 +46,18 @@ public class AutoMod extends ListenerAdapter {
private final NotificationService notificationService;
private final BotConfig botConfig;
private List<String> spamUrls;
private final WarnRepository warnRepository;
private final ExecutorService asyncPool;
private final ModerationService moderationService;

/**
* Constructor of the class, that creates a list of strings with potential spam/scam urls.
* @param notificationService The {@link QOTWPointsService}
* @param botConfig The main configuration of the bot
* @param asyncPool The main thread pool for asynchronous operations
* @param warnRepository The main thread pool for asynchronous operations
* @param moderationService Service object for moderating members
*/
public AutoMod(NotificationService notificationService, BotConfig botConfig, ExecutorService asyncPool, WarnRepository warnRepository) {
public AutoMod(NotificationService notificationService, BotConfig botConfig, ModerationService moderationService) {
this.notificationService = notificationService;
this.botConfig = botConfig;
this.warnRepository = warnRepository;
this.asyncPool = asyncPool;
this.moderationService = moderationService;
try(Scanner scan = new Scanner(new URL("https://raw.githubusercontent.com/DevSpen/scam-links/master/src/links.txt").openStream()).useDelimiter("\\A")) {
String response = scan.next();
spamUrls = List.of(response.split("\n"));
Expand Down Expand Up @@ -138,7 +133,7 @@ private void checkContentAutomod(@Nonnull Message message) {

private void doAutomodActions(Message message, String reason) {
notificationService.withGuild(message.getGuild()).sendToModerationLog(c -> c.sendMessageFormat("Message by %s: `%s`", message.getAuthor().getAsMention(), message.getContentRaw()));
new ModerationService(notificationService, botConfig.get(message.getGuild()), warnRepository, asyncPool)
moderationService
.warn(
message.getAuthor(),
WarnSeverity.MEDIUM,
Expand All @@ -163,7 +158,7 @@ private void handleSpam(@Nonnull Message msg, Member member) {
if (!msg.getAttachments().isEmpty() && msg.getAttachments().stream().allMatch(a -> Objects.equals(a.getFileExtension(), "java"))) {
return;
}
new ModerationService(notificationService, botConfig.get(member.getGuild()), warnRepository, asyncPool)
moderationService
.timeout(
member.getUser(),
"Automod: Spam",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package net.discordjug.javabot.systems.moderation;

import net.discordjug.javabot.data.config.BotConfig;
import net.discordjug.javabot.systems.moderation.warn.dao.WarnRepository;
import net.discordjug.javabot.systems.notification.NotificationService;
import net.discordjug.javabot.util.Checks;
import net.discordjug.javabot.util.Responses;
import net.dv8tion.jda.api.Permission;
Expand All @@ -14,31 +12,23 @@
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.requests.restaction.WebhookMessageCreateAction;

import java.util.concurrent.ExecutorService;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Command that allows staff-members to ban guild members.
*/
public class BanCommand extends ModerateUserCommand {
private final NotificationService notificationService;
private final WarnRepository warnRepository;
private final ExecutorService asyncPool;
private final ModerationService moderationService;

/**
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
* @param notificationService The {@link NotificationService}
* @param botConfig The main configuration of the bot
* @param asyncPool The main thread pool for asynchronous operations
* @param warnRepository DAO for interacting with the set of {@link Warn} objects.s
* @param moderationService Service object for moderating members
*/
public BanCommand(NotificationService notificationService, BotConfig botConfig, ExecutorService asyncPool, WarnRepository warnRepository) {
public BanCommand(BotConfig botConfig, ModerationService moderationService) {
super(botConfig);
this.notificationService = notificationService;
this.warnRepository = warnRepository;
this.asyncPool = asyncPool;
this.moderationService = moderationService;
setModerationSlashCommandData(Commands.slash("ban", "Ban a user.")
.addOption(OptionType.USER, "user", "The user to ban.", true)
.addOption(OptionType.STRING, "reason", "The reason for banning this user.", true)
Expand All @@ -52,8 +42,7 @@ protected WebhookMessageCreateAction<Message> handleModerationUserCommand(@Nonnu
return Responses.replyInsufficientPermissions(event.getHook(), Permission.BAN_MEMBERS);
}
boolean quiet = isQuiet(event);
ModerationService service = new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool);
service.ban(target, reason, commandUser, event.getChannel(), quiet);
moderationService.ban(target, reason, commandUser, event.getChannel(), quiet);
return Responses.success(event.getHook(), "User Banned", "%s has been banned.", target.getAsMention());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.EnumSet;
import java.util.concurrent.ExecutorService;

import lombok.RequiredArgsConstructor;
import net.discordjug.javabot.data.config.BotConfig;
import net.discordjug.javabot.systems.moderation.warn.dao.WarnRepository;
import net.discordjug.javabot.systems.notification.NotificationService;
import net.discordjug.javabot.util.ExceptionLogger;
import net.dv8tion.jda.api.audit.ActionType;

import net.dv8tion.jda.api.audit.AuditLogChange;
import net.dv8tion.jda.api.audit.AuditLogEntry;
import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent;
Expand All @@ -22,15 +19,10 @@
@RequiredArgsConstructor
public class DiscordModerationLogListener extends ListenerAdapter{

private final NotificationService notificationService;
private final BotConfig botConfig;
private final WarnRepository warnRepository;
private final ExecutorService asyncPool;
private final ModerationService moderationService;

@Override
public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) {
ModerationService moderationService = new ModerationService(notificationService, botConfig.get(event.getGuild()), warnRepository, asyncPool);

AuditLogEntry entry = event.getEntry();
long targetUserId = entry.getTargetIdLong();
long moderatorUserId = entry.getUserIdLong();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package net.discordjug.javabot.systems.moderation;

import net.discordjug.javabot.data.config.BotConfig;
import net.discordjug.javabot.systems.moderation.warn.dao.WarnRepository;
import net.discordjug.javabot.systems.notification.NotificationService;
import net.discordjug.javabot.util.Checks;
import net.discordjug.javabot.util.Responses;
import net.dv8tion.jda.api.Permission;
Expand All @@ -14,31 +12,23 @@
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.requests.restaction.WebhookMessageCreateAction;

import java.util.concurrent.ExecutorService;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* <h3>This class represents the /kick command.</h3>
*/
public class KickCommand extends ModerateUserCommand {
private final NotificationService notificationService;
private final WarnRepository warnRepository;
private final ExecutorService asyncPool;
private final ModerationService moderationService;

/**
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
* @param notificationService The {@link NotificationService}
* @param botConfig The main configuration of the bot
* @param asyncPool The main thread pool for asynchronous operations
* @param warnRepository DAO for interacting with the set of {@link Warn} objects.
* @param moderationService Service object for moderating members
*/
public KickCommand(NotificationService notificationService, BotConfig botConfig, ExecutorService asyncPool, WarnRepository warnRepository) {
public KickCommand(BotConfig botConfig, ModerationService moderationService) {
super(botConfig);
this.notificationService = notificationService;
this.warnRepository = warnRepository;
this.asyncPool = asyncPool;
this.moderationService = moderationService;
setModerationSlashCommandData(Commands.slash("kick", "Kicks a member")
.addOption(OptionType.USER, "user", "The user to kick.", true)
.addOption(OptionType.STRING, "reason", "The reason for kicking this user.", true)
Expand All @@ -52,8 +42,7 @@ protected WebhookMessageCreateAction<Message> handleModerationUserCommand(@Nonnu
return Responses.replyInsufficientPermissions(event.getHook(), Permission.KICK_MEMBERS);
}
boolean quiet = isQuiet(event);
ModerationService service = new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool);
service.kick(target, reason, event.getMember(), event.getChannel(), quiet);
moderationService.kick(target, reason, event.getMember(), event.getChannel(), quiet);
return Responses.success(event.getHook(), "User Kicked", "%s has been kicked.", target.getAsMention());
}
}
Loading

0 comments on commit 548a7a9

Please sign in to comment.