Skip to content

Commit 09037aa

Browse files
committed
change /warn discard-all to only discard active warns
1 parent 44de33d commit 09037aa

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void warn(User user, WarnSeverity severity, String reason, Member warnedB
9797
*/
9898
public SeverityInformation getTotalSeverityWeight(Guild guild, long userId) {
9999
ModerationConfig moderationConfig = botConfig.get(guild).getModerationConfig();
100-
List<Warn> activeWarns = warnRepository.getActiveWarnsByUserId(userId, LocalDateTime.now().minusDays(moderationConfig.getMaxWarnValidityDays()));
100+
List<Warn> activeWarns = warnRepository.getActiveWarnsByUserId(userId, getEarliestActiveWarnTimestamp(moderationConfig));
101101
return calculateSeverityWeight(moderationConfig, activeWarns);
102102
}
103103

@@ -132,9 +132,10 @@ static SeverityInformation calculateSeverityWeight(ModerationConfig moderationCo
132132
* @param clearedBy The user who cleared the warns.
133133
*/
134134
public void discardAllWarns(User user, Member clearedBy) {
135+
ModerationConfig moderationConfig = getModerationConfig(clearedBy);
135136
asyncPool.execute(() -> {
136137
try {
137-
warnRepository.discardAll(user.getIdLong());
138+
warnRepository.discardAll(user.getIdLong(), getEarliestActiveWarnTimestamp(moderationConfig));
138139
MessageEmbed embed = buildClearWarnsEmbed(user, clearedBy.getUser());
139140
notificationService.withUser(user, clearedBy.getGuild()).sendDirectMessage(c -> c.sendMessageEmbeds(embed));
140141
notificationService.withGuild(clearedBy.getGuild()).sendToModerationLog(c -> c.sendMessageEmbeds(embed));
@@ -144,6 +145,10 @@ public void discardAllWarns(User user, Member clearedBy) {
144145
});
145146
}
146147

148+
private LocalDateTime getEarliestActiveWarnTimestamp(ModerationConfig moderationConfig) {
149+
return LocalDateTime.now().minusDays(moderationConfig.getMaxWarnValidityDays());
150+
}
151+
147152
/**
148153
* Clears a warn by discarding the Warn with the corresponding id.
149154
*
@@ -177,7 +182,7 @@ public List<Warn> getWarns(Guild guild, long userId) {
177182
try {
178183
ModerationConfig moderationConfig = botConfig.get(guild).getModerationConfig();
179184
WarnRepository repo = warnRepository;
180-
LocalDateTime cutoff = LocalDateTime.now().minusDays(moderationConfig.getMaxWarnValidityDays());
185+
LocalDateTime cutoff = getEarliestActiveWarnTimestamp(moderationConfig);
181186
return repo.getActiveWarnsByUserId(userId, cutoff);
182187
} catch (DataAccessException e) {
183188
ExceptionLogger.capture(e, getClass().getSimpleName());

src/main/java/net/discordjug/javabot/systems/moderation/warn/dao/WarnRepository.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,18 @@ public Optional<Warn> findById(long id) throws DataAccessException {
6363
}
6464

6565
/**
66-
* Discards all warnings that have been issued to a given user.
66+
* Discards all warnings that have been issued to a given user after a given timestamp.
6767
*
6868
* @param userId The id of the user to discard warnings for.
69+
* @param cutoff timestamp specifying which warns should be discarded. Only warns after the cutoff are discarded.
6970
* @throws SQLException If an error occurs.
7071
*/
71-
public void discardAll(long userId) throws DataAccessException {
72+
public void discardAll(long userId, LocalDateTime cutoff) throws DataAccessException {
7273
jdbcTemplate.update("""
7374
UPDATE warn SET discarded = TRUE
74-
WHERE user_id = ?""",
75-
userId);
75+
WHERE user_id = ?
76+
AND created_at > ?""",
77+
userId, Timestamp.valueOf(cutoff));
7678
}
7779

7880
/**

0 commit comments

Comments
 (0)