mirror of
https://github.com/Sheldan/OnePlusBot.git
synced 2026-01-01 15:28:28 +00:00
[OPB-40] adding warning threshold notification
restructured template module structure and naming
This commit is contained in:
@@ -2,6 +2,7 @@ package dev.sheldan.oneplus.bot.custom.moderation.config;
|
||||
|
||||
import dev.sheldan.abstracto.core.config.FeatureConfig;
|
||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||
import dev.sheldan.abstracto.core.config.PostTargetEnum;
|
||||
import dev.sheldan.abstracto.moderation.config.feature.ModerationFeatureConfig;
|
||||
import dev.sheldan.oneplus.bot.custom.moderation.service.ModModeServiceBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -14,6 +15,8 @@ import java.util.List;
|
||||
@Component
|
||||
public class ModerationCustomFeature implements FeatureConfig {
|
||||
|
||||
public static final String WARN_NOTIFICATION_THRESHOLD = "warnNotificationThreshold";
|
||||
|
||||
@Autowired
|
||||
private ModerationFeatureConfig moderationFeatureConfig;
|
||||
|
||||
@@ -27,9 +30,14 @@ public class ModerationCustomFeature implements FeatureConfig {
|
||||
return Arrays.asList(moderationFeatureConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PostTargetEnum> getRequiredPostTargets() {
|
||||
return Arrays.asList(ModerationCustomPostTarget.WARN_THRESHOLD_NOTIFICATION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getRequiredSystemConfigKeys() {
|
||||
return Arrays.asList(ModModeServiceBean.MODMODE_ROLE_CONFIG_KEY,
|
||||
ModModeServiceBean.MODMODE_CHANGED_ROLE_COLOR_CONFIG_KEY);
|
||||
ModModeServiceBean.MODMODE_CHANGED_ROLE_COLOR_CONFIG_KEY, WARN_NOTIFICATION_THRESHOLD);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package dev.sheldan.oneplus.bot.custom.moderation.config;
|
||||
|
||||
import dev.sheldan.abstracto.core.config.PostTargetEnum;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum ModerationCustomPostTarget implements PostTargetEnum {
|
||||
WARN_THRESHOLD_NOTIFICATION("warnThresholdNotification");
|
||||
|
||||
private String key;
|
||||
|
||||
ModerationCustomPostTarget(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package dev.sheldan.oneplus.bot.custom.moderation.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||
import dev.sheldan.abstracto.core.listener.DefaultListenerResult;
|
||||
import dev.sheldan.abstracto.core.models.ServerUser;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.models.template.display.ChannelDisplay;
|
||||
import dev.sheldan.abstracto.core.models.template.display.MemberDisplay;
|
||||
import dev.sheldan.abstracto.core.service.*;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.moderation.listener.WarningCreatedListener;
|
||||
import dev.sheldan.abstracto.moderation.model.listener.WarningCreatedEventModel;
|
||||
import dev.sheldan.abstracto.moderation.service.management.WarnManagementService;
|
||||
import dev.sheldan.oneplus.bot.custom.moderation.config.ModerationCustomFeature;
|
||||
import dev.sheldan.oneplus.bot.custom.moderation.config.ModerationCustomFeatureDefinition;
|
||||
import dev.sheldan.oneplus.bot.custom.moderation.config.ModerationCustomPostTarget;
|
||||
import dev.sheldan.oneplus.bot.custom.moderation.model.template.WarningThresholdNotificationModel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class WarningAddedListener implements WarningCreatedListener {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
|
||||
@Autowired
|
||||
private WarnManagementService warnManagementService;
|
||||
|
||||
@Autowired
|
||||
private UserInServerManagementService userInServerManagementService;
|
||||
|
||||
@Autowired
|
||||
private ChannelService channelService;
|
||||
|
||||
@Autowired
|
||||
private PostTargetService postTargetService;
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
public static final String WARN_THRESHOLD_NOTIFICATION_TEMPLATE_KEY = "warning_threshold_notification";
|
||||
|
||||
@Override
|
||||
public DefaultListenerResult execute(WarningCreatedEventModel model) {
|
||||
Long warnNotificationAmount = configService.getLongValueOrConfigDefault(ModerationCustomFeature.WARN_NOTIFICATION_THRESHOLD, model.getServerId());
|
||||
ServerUser warnedUser = ServerUser
|
||||
.builder()
|
||||
.userId(model.getWarnedUserId())
|
||||
.serverId(model.getServerId())
|
||||
.build();
|
||||
AUserInAServer warnedUserInAServer = userInServerManagementService.loadOrCreateUser(warnedUser);
|
||||
List<Long> activeWarnsForUser = warnManagementService.getActiveWarnsForUser(warnedUserInAServer)
|
||||
.stream().map(warning -> warning.getWarnId().getId()).collect(Collectors.toList());
|
||||
Set<Long> warnIds = new HashSet<>(activeWarnsForUser);
|
||||
// we cant be sure we receive the newly persisted warning yet, sadly
|
||||
warnIds.add(model.getWarningId());
|
||||
if(warnIds.size() == warnNotificationAmount) {
|
||||
Long serverId = model.getServerId();
|
||||
|
||||
Long channelId = model.getWarningChannelId();
|
||||
Long warnedUserId = model.getWarnedUserId();
|
||||
TextChannel channel = channelService.getTextChannelFromServer(serverId, channelId);
|
||||
WarningThresholdNotificationModel notificationModel = WarningThresholdNotificationModel
|
||||
.builder()
|
||||
.channelDisplay(ChannelDisplay.fromChannel(channel))
|
||||
.memberDisplay(MemberDisplay.fromAUserInAServer(warnedUserInAServer))
|
||||
.messageId(model.getWarningMessageId())
|
||||
.warnCount(warnIds.size())
|
||||
.build();
|
||||
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(WARN_THRESHOLD_NOTIFICATION_TEMPLATE_KEY, notificationModel, serverId);
|
||||
FutureUtils.toSingleFutureGeneric(postTargetService.sendEmbedInPostTarget(messageToSend, ModerationCustomPostTarget.WARN_THRESHOLD_NOTIFICATION, serverId))
|
||||
.thenAccept(unused -> log.info("Warn threshold notification sent for user {} in server {}.", warnedUserId, serverId))
|
||||
.exceptionally(throwable -> {
|
||||
log.error("Failed to sent warn threshold notification for user {} in server {}.", warnedUserId, serverId, throwable);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
return DefaultListenerResult.PROCESSED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FeatureDefinition getFeature() {
|
||||
return ModerationCustomFeatureDefinition.MODERATION_CUSTOM;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package dev.sheldan.oneplus.bot.custom.moderation.model.template;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.template.display.ChannelDisplay;
|
||||
import dev.sheldan.abstracto.core.models.template.display.MemberDisplay;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class WarningThresholdNotificationModel {
|
||||
private MemberDisplay memberDisplay;
|
||||
private Integer warnCount;
|
||||
private ChannelDisplay channelDisplay;
|
||||
private Long messageId;
|
||||
}
|
||||
@@ -5,4 +5,9 @@ abstracto.systemConfigs.modModeRoleId.name=modModeRoleId
|
||||
abstracto.systemConfigs.modModeRoleId.longValue=0
|
||||
|
||||
abstracto.systemConfigs.modModeNewRoleColor.name=modModeNewRoleColor
|
||||
abstracto.systemConfigs.modModeNewRoleColor.stringValue=0,0,0
|
||||
abstracto.systemConfigs.modModeNewRoleColor.stringValue=0,0,0
|
||||
|
||||
abstracto.systemConfigs.warnNotificationThreshold.name=warnNotificationThreshold
|
||||
abstracto.systemConfigs.warnNotificationThreshold.longValue=3
|
||||
|
||||
abstracto.postTargets.warnThresholdNotification.name=warnThresholdNotification
|
||||
Reference in New Issue
Block a user