Skip to content

Commit 2928ebc

Browse files
authored
Merge pull request #479 from danthe1st/warn-decay
warn decay
2 parents e59239f + 09037aa commit 2928ebc

20 files changed

+363
-300
lines changed

src/main/java/net/discordjug/javabot/api/routes/user_profile/UserProfileController.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import net.discordjug.javabot.data.config.BotConfig;
1111
import net.discordjug.javabot.systems.help.HelpExperienceService;
1212
import net.discordjug.javabot.systems.help.model.HelpAccount;
13-
import net.discordjug.javabot.systems.moderation.warn.dao.WarnRepository;
13+
import net.discordjug.javabot.systems.moderation.ModerationService;
1414
import net.discordjug.javabot.systems.qotw.QOTWPointsService;
1515
import net.discordjug.javabot.systems.qotw.model.QOTWAccount;
1616
import net.discordjug.javabot.util.Pair;
@@ -29,7 +29,6 @@
2929

3030
import java.sql.Connection;
3131
import java.sql.SQLException;
32-
import java.time.LocalDateTime;
3332
import java.util.concurrent.TimeUnit;
3433

3534
import javax.sql.DataSource;
@@ -44,7 +43,7 @@ public class UserProfileController extends CaffeineCache<Pair<Long, Long>, UserP
4443
private final DataSource dataSource;
4544
private final BotConfig botConfig;
4645
private final HelpExperienceService helpExperienceService;
47-
private final WarnRepository warnRepository;
46+
private final ModerationService moderationService;
4847

4948
/**
5049
* The constructor of this class which initializes the {@link Caffeine} cache.
@@ -54,10 +53,10 @@ public class UserProfileController extends CaffeineCache<Pair<Long, Long>, UserP
5453
* @param botConfig The main configuration of the bot
5554
* @param dataSource A factory for connections to the main database
5655
* @param helpExperienceService Service object that handles Help Experience Transactions.
57-
* @param warnRepository DAO for interacting with the set of {@link Warn} objects.
56+
* @param moderationService Service object for moderating members
5857
*/
5958
@Autowired
60-
public UserProfileController(final JDA jda, QOTWPointsService qotwPointsService, BotConfig botConfig, DataSource dataSource, HelpExperienceService helpExperienceService, WarnRepository warnRepository) {
59+
public UserProfileController(final JDA jda, QOTWPointsService qotwPointsService, BotConfig botConfig, DataSource dataSource, HelpExperienceService helpExperienceService, ModerationService moderationService) {
6160
super(Caffeine.newBuilder()
6261
.expireAfterWrite(10, TimeUnit.MINUTES)
6362
.build()
@@ -67,7 +66,7 @@ public UserProfileController(final JDA jda, QOTWPointsService qotwPointsService,
6766
this.dataSource = dataSource;
6867
this.botConfig = botConfig;
6968
this.helpExperienceService = helpExperienceService;
70-
this.warnRepository = warnRepository;
69+
this.moderationService = moderationService;
7170
}
7271

7372
/**
@@ -108,8 +107,7 @@ public ResponseEntity<UserProfileData> getUserProfile(
108107
HelpAccount helpAccount = helpExperienceService.getOrCreateAccount(user.getIdLong());
109108
data.setHelpAccount(HelpAccountData.of(botConfig, helpAccount, guild));
110109
// User Warns
111-
LocalDateTime cutoff = LocalDateTime.now().minusDays(botConfig.get(guild).getModerationConfig().getWarnTimeoutDays());
112-
data.setWarns(warnRepository.getActiveWarnsByUserId(user.getIdLong(), cutoff));
110+
data.setWarns(moderationService.getTotalSeverityWeight(guild, user.getIdLong()).contributingWarns());
113111
// Insert into cache
114112
getCache().put(new Pair<>(guild.getIdLong(), user.getIdLong()), data);
115113
}

src/main/java/net/discordjug/javabot/data/config/guild/ModerationConfig.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ public class ModerationConfig extends GuildConfigItem {
5353
* being removed from the server. Warnings older than this are still kept,
5454
* but ignored.
5555
*/
56-
private int warnTimeoutDays = 30;
56+
private int maxWarnValidityDays = 30;
57+
58+
/**
59+
* The number of days it takes to remove a certain amount of severity weights from the warns of a user.
60+
*/
61+
private int warnDecayDays = maxWarnValidityDays;
62+
63+
/**
64+
* The total severity weight removed from a user after {@link ModerationConfig#warnDecayDays} passed.
65+
*/
66+
private int warnDecayAmount = 0;
5767

5868
/**
5969
* The maximum total severity that a user can accrue from warnings before

src/main/java/net/discordjug/javabot/systems/moderation/AutoMod.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import lombok.extern.slf4j.Slf4j;
44
import net.discordjug.javabot.data.config.BotConfig;
5-
import net.discordjug.javabot.systems.moderation.warn.dao.WarnRepository;
65
import net.discordjug.javabot.systems.moderation.warn.model.WarnSeverity;
76
import net.discordjug.javabot.systems.notification.NotificationService;
87
import net.discordjug.javabot.util.ExceptionLogger;
@@ -27,7 +26,6 @@
2726
import java.util.List;
2827
import java.util.Objects;
2928
import java.util.Scanner;
30-
import java.util.concurrent.ExecutorService;
3129
import java.util.concurrent.TimeUnit;
3230
import java.util.regex.Matcher;
3331
import java.util.regex.Pattern;
@@ -48,21 +46,18 @@ public class AutoMod extends ListenerAdapter {
4846
private final NotificationService notificationService;
4947
private final BotConfig botConfig;
5048
private List<String> spamUrls;
51-
private final WarnRepository warnRepository;
52-
private final ExecutorService asyncPool;
49+
private final ModerationService moderationService;
5350

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

139134
private void doAutomodActions(Message message, String reason) {
140135
notificationService.withGuild(message.getGuild()).sendToModerationLog(c -> c.sendMessageFormat("Message by %s: `%s`", message.getAuthor().getAsMention(), message.getContentRaw()));
141-
new ModerationService(notificationService, botConfig.get(message.getGuild()), warnRepository, asyncPool)
136+
moderationService
142137
.warn(
143138
message.getAuthor(),
144139
WarnSeverity.MEDIUM,
@@ -163,7 +158,7 @@ private void handleSpam(@Nonnull Message msg, Member member) {
163158
if (!msg.getAttachments().isEmpty() && msg.getAttachments().stream().allMatch(a -> Objects.equals(a.getFileExtension(), "java"))) {
164159
return;
165160
}
166-
new ModerationService(notificationService, botConfig.get(member.getGuild()), warnRepository, asyncPool)
161+
moderationService
167162
.timeout(
168163
member.getUser(),
169164
"Automod: Spam",
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package net.discordjug.javabot.systems.moderation;
22

33
import net.discordjug.javabot.data.config.BotConfig;
4-
import net.discordjug.javabot.systems.moderation.warn.dao.WarnRepository;
5-
import net.discordjug.javabot.systems.notification.NotificationService;
64
import net.discordjug.javabot.util.Checks;
75
import net.discordjug.javabot.util.Responses;
86
import net.dv8tion.jda.api.Permission;
@@ -14,31 +12,23 @@
1412
import net.dv8tion.jda.api.interactions.commands.build.Commands;
1513
import net.dv8tion.jda.api.requests.restaction.WebhookMessageCreateAction;
1614

17-
import java.util.concurrent.ExecutorService;
18-
1915
import javax.annotation.Nonnull;
2016
import javax.annotation.Nullable;
2117

2218
/**
2319
* Command that allows staff-members to ban guild members.
2420
*/
2521
public class BanCommand extends ModerateUserCommand {
26-
private final NotificationService notificationService;
27-
private final WarnRepository warnRepository;
28-
private final ExecutorService asyncPool;
22+
private final ModerationService moderationService;
2923

3024
/**
3125
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
32-
* @param notificationService The {@link NotificationService}
3326
* @param botConfig The main configuration of the bot
34-
* @param asyncPool The main thread pool for asynchronous operations
35-
* @param warnRepository DAO for interacting with the set of {@link Warn} objects.s
27+
* @param moderationService Service object for moderating members
3628
*/
37-
public BanCommand(NotificationService notificationService, BotConfig botConfig, ExecutorService asyncPool, WarnRepository warnRepository) {
29+
public BanCommand(BotConfig botConfig, ModerationService moderationService) {
3830
super(botConfig);
39-
this.notificationService = notificationService;
40-
this.warnRepository = warnRepository;
41-
this.asyncPool = asyncPool;
31+
this.moderationService = moderationService;
4232
setModerationSlashCommandData(Commands.slash("ban", "Ban a user.")
4333
.addOption(OptionType.USER, "user", "The user to ban.", true)
4434
.addOption(OptionType.STRING, "reason", "The reason for banning this user.", true)
@@ -52,8 +42,7 @@ protected WebhookMessageCreateAction<Message> handleModerationUserCommand(@Nonnu
5242
return Responses.replyInsufficientPermissions(event.getHook(), Permission.BAN_MEMBERS);
5343
}
5444
boolean quiet = isQuiet(event);
55-
ModerationService service = new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool);
56-
service.ban(target, reason, commandUser, event.getChannel(), quiet);
45+
moderationService.ban(target, reason, commandUser, event.getChannel(), quiet);
5746
return Responses.success(event.getHook(), "User Banned", "%s has been banned.", target.getAsMention());
5847
}
5948
}

src/main/java/net/discordjug/javabot/systems/moderation/DiscordModerationLogListener.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
import java.time.Duration;
44
import java.time.ZonedDateTime;
55
import java.util.EnumSet;
6-
import java.util.concurrent.ExecutorService;
76

87
import lombok.RequiredArgsConstructor;
9-
import net.discordjug.javabot.data.config.BotConfig;
10-
import net.discordjug.javabot.systems.moderation.warn.dao.WarnRepository;
11-
import net.discordjug.javabot.systems.notification.NotificationService;
128
import net.discordjug.javabot.util.ExceptionLogger;
139
import net.dv8tion.jda.api.audit.ActionType;
10+
1411
import net.dv8tion.jda.api.audit.AuditLogChange;
1512
import net.dv8tion.jda.api.audit.AuditLogEntry;
1613
import net.dv8tion.jda.api.events.guild.GuildAuditLogEntryCreateEvent;
@@ -22,15 +19,10 @@
2219
@RequiredArgsConstructor
2320
public class DiscordModerationLogListener extends ListenerAdapter{
2421

25-
private final NotificationService notificationService;
26-
private final BotConfig botConfig;
27-
private final WarnRepository warnRepository;
28-
private final ExecutorService asyncPool;
22+
private final ModerationService moderationService;
2923

3024
@Override
3125
public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) {
32-
ModerationService moderationService = new ModerationService(notificationService, botConfig.get(event.getGuild()), warnRepository, asyncPool);
33-
3426
AuditLogEntry entry = event.getEntry();
3527
long targetUserId = entry.getTargetIdLong();
3628
long moderatorUserId = entry.getUserIdLong();
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package net.discordjug.javabot.systems.moderation;
22

33
import net.discordjug.javabot.data.config.BotConfig;
4-
import net.discordjug.javabot.systems.moderation.warn.dao.WarnRepository;
5-
import net.discordjug.javabot.systems.notification.NotificationService;
64
import net.discordjug.javabot.util.Checks;
75
import net.discordjug.javabot.util.Responses;
86
import net.dv8tion.jda.api.Permission;
@@ -14,31 +12,23 @@
1412
import net.dv8tion.jda.api.interactions.commands.build.Commands;
1513
import net.dv8tion.jda.api.requests.restaction.WebhookMessageCreateAction;
1614

17-
import java.util.concurrent.ExecutorService;
18-
1915
import javax.annotation.Nonnull;
2016
import javax.annotation.Nullable;
2117

2218
/**
2319
* <h3>This class represents the /kick command.</h3>
2420
*/
2521
public class KickCommand extends ModerateUserCommand {
26-
private final NotificationService notificationService;
27-
private final WarnRepository warnRepository;
28-
private final ExecutorService asyncPool;
22+
private final ModerationService moderationService;
2923

3024
/**
3125
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
32-
* @param notificationService The {@link NotificationService}
3326
* @param botConfig The main configuration of the bot
34-
* @param asyncPool The main thread pool for asynchronous operations
35-
* @param warnRepository DAO for interacting with the set of {@link Warn} objects.
27+
* @param moderationService Service object for moderating members
3628
*/
37-
public KickCommand(NotificationService notificationService, BotConfig botConfig, ExecutorService asyncPool, WarnRepository warnRepository) {
29+
public KickCommand(BotConfig botConfig, ModerationService moderationService) {
3830
super(botConfig);
39-
this.notificationService = notificationService;
40-
this.warnRepository = warnRepository;
41-
this.asyncPool = asyncPool;
31+
this.moderationService = moderationService;
4232
setModerationSlashCommandData(Commands.slash("kick", "Kicks a member")
4333
.addOption(OptionType.USER, "user", "The user to kick.", true)
4434
.addOption(OptionType.STRING, "reason", "The reason for kicking this user.", true)
@@ -52,8 +42,7 @@ protected WebhookMessageCreateAction<Message> handleModerationUserCommand(@Nonnu
5242
return Responses.replyInsufficientPermissions(event.getHook(), Permission.KICK_MEMBERS);
5343
}
5444
boolean quiet = isQuiet(event);
55-
ModerationService service = new ModerationService(notificationService, botConfig, event.getInteraction(), warnRepository, asyncPool);
56-
service.kick(target, reason, event.getMember(), event.getChannel(), quiet);
45+
moderationService.kick(target, reason, event.getMember(), event.getChannel(), quiet);
5746
return Responses.success(event.getHook(), "User Kicked", "%s has been kicked.", target.getAsMention());
5847
}
5948
}

0 commit comments

Comments
 (0)