added configurable warning and success reactions

changed that channels dont actually get deleted, but rather with a deleted flag, so the foreign keys are valid
added more optionals to server/channel/message/member retrieval
added throwing exceptions instead of just returning null
fixed filtering of deleted channels while the bot was offline
fixed channel listener
added message deleted listener structure
moved quartz properties to general application properties, because they were not found in the separate one
added try catch to reminderJob, so that the job is not in an invalid state
we now completely remove the starboard post, in case it falls under the threshold
added code to 'ignore' a staroard post form further stars, in case the post in the starboard gets deleted
now actually setting the reminded flag on a reminder
added handnling in case the channel to remind in does not exist anymore
This commit is contained in:
Sheldan
2020-04-01 23:46:28 +02:00
parent 089862bf15
commit c9557fccc2
45 changed files with 409 additions and 181 deletions

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.moderation.listener;
import dev.sheldan.abstracto.core.listener.MessageDeletedListener;
import dev.sheldan.abstracto.core.models.CachedMessage;
import dev.sheldan.abstracto.core.utils.ContextUtils;
import dev.sheldan.abstracto.core.service.MessageCache;
@@ -9,19 +10,12 @@ import dev.sheldan.abstracto.moderation.models.template.listener.MessageDeletedL
import dev.sheldan.abstracto.core.models.embed.MessageToSend;
import dev.sheldan.abstracto.templating.TemplateService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Nonnull;
import java.util.concurrent.ExecutionException;
@Component
@Slf4j
public class MessageDeletedListener extends ListenerAdapter {
public class MessageDeleteLogListener implements MessageDeletedListener {
private static final String DELETE_LOG_TARGET = "deleteLog";
private static String MESSAGE_DELETED_TEMPLATE = "message_deleted";
@@ -39,37 +33,20 @@ public class MessageDeletedListener extends ListenerAdapter {
@Autowired
private PostTargetService postTargetService;
@Autowired
private MessageDeletedListener self;
@Override
@Transactional
public void onGuildMessageDelete(@Nonnull GuildMessageDeleteEvent event) {
messageCache.getMessageFromCache(event.getGuild().getIdLong(), event.getChannel().getIdLong(), event.getMessageIdLong())
.thenAccept(messageFromCache -> {
try {
self.logMessage(event, messageFromCache);
} catch (Exception e) {
log.error("Error when logging message deletion.", e);
}
});
}
@Transactional
public void logMessage(@Nonnull GuildMessageDeleteEvent event, CachedMessage messageFromCache) {
public void execute(CachedMessage messageFromCache) {
MessageDeletedLog logModel = (MessageDeletedLog) contextUtils.fromMessage(messageFromCache, MessageDeletedLog.class);
logModel.setMessage(messageFromCache);
String simpleMessageUpdatedMessage = templateService.renderTemplate(MESSAGE_DELETED_TEMPLATE, logModel);
postTargetService.sendTextInPostTarget(simpleMessageUpdatedMessage, DELETE_LOG_TARGET, event.getGuild().getIdLong());
postTargetService.sendTextInPostTarget(simpleMessageUpdatedMessage, DELETE_LOG_TARGET, messageFromCache.getServerId());
MessageToSend message = templateService.renderEmbedTemplate(MESSAGE_DELETED_TEMPLATE, logModel);
postTargetService.sendEmbedInPostTarget(message, DELETE_LOG_TARGET, event.getGuild().getIdLong());
postTargetService.sendEmbedInPostTarget(message, DELETE_LOG_TARGET, messageFromCache.getServerId());
for (int i = 0; i < messageFromCache.getAttachmentUrls().size(); i++) {
MessageDeletedAttachmentLog log = (MessageDeletedAttachmentLog) contextUtils.fromMessage(messageFromCache, MessageDeletedAttachmentLog.class);
log.setImageUrl(messageFromCache.getAttachmentUrls().get(i));
log.setCounter(i + 1);
MessageToSend attachmentEmbed = templateService.renderEmbedTemplate(MESSAGE_DELETED_ATTACHMENT_TEMPLATE, log);
postTargetService.sendEmbedInPostTarget(attachmentEmbed, DELETE_LOG_TARGET, event.getGuild().getIdLong());
postTargetService.sendEmbedInPostTarget(attachmentEmbed, DELETE_LOG_TARGET, messageFromCache.getServerId());
}
}
}

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.moderation.service;
import dev.sheldan.abstracto.core.exception.NotFoundException;
import dev.sheldan.abstracto.core.models.ServerContext;
import dev.sheldan.abstracto.core.service.Bot;
import dev.sheldan.abstracto.core.service.PostTargetService;
@@ -10,6 +11,8 @@ import net.dv8tion.jda.api.entities.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
@Slf4j
public class BanServiceBean implements BanService {
@@ -41,8 +44,13 @@ public class BanServiceBean implements BanService {
}
private void banUser(Long guildId, Long userId, String reason) {
Guild guildById = bot.getGuildById(guildId);
log.info("Banning user {} in guild {}.", userId, guildId);
guildById.ban(userId.toString(), 0, reason).queue();
Optional<Guild> guildByIdOptional = bot.getGuildById(guildId);
if(guildByIdOptional.isPresent()) {
log.info("Banning user {} in guild {}.", userId, guildId);
guildByIdOptional.get().ban(userId.toString(), 0, reason).queue();
} else {
log.warn("Guild {} not found. Not able to ban user {}", guildId, userId);
throw new NotFoundException(String.format("Guild %s not found. Not able to ban user %s", guildId, userId));
}
}
}

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.moderation.service;
import dev.sheldan.abstracto.core.exception.NotFoundException;
import dev.sheldan.abstracto.core.service.Bot;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.moderation.models.template.KickLogModel;
@@ -10,6 +11,8 @@ import net.dv8tion.jda.api.entities.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
@Slf4j
public class KickServiceBean implements KickService {
@@ -27,9 +30,14 @@ public class KickServiceBean implements KickService {
@Override
public void kickMember(Member member, String reason, KickLogModel kickLogModel) {
Guild guildById = bot.getGuildById(kickLogModel.getGuild().getIdLong());
guildById.kick(member, reason).queue();
this.sendKickLog(kickLogModel);
Optional<Guild> guildById = bot.getGuildById(kickLogModel.getGuild().getIdLong());
if(guildById.isPresent()) {
guildById.get().kick(member, reason).queue();
this.sendKickLog(kickLogModel);
} else {
log.warn("Not able to kick. Guild {} not found.", kickLogModel.getGuild().getIdLong());
throw new NotFoundException(String.format("Not able to kick %s. Guild %s not found", kickLogModel.getMember().getIdLong(), kickLogModel.getGuild().getIdLong()));
}
}
private void sendKickLog(KickLogModel kickLogModel) {

View File

@@ -1,15 +1,15 @@
package dev.sheldan.abstracto.moderation.service;
import dev.sheldan.abstracto.core.exception.NotFoundException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.service.Bot;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.Optional;
@Component
@Slf4j
@@ -29,7 +29,12 @@ public class SlowModeServiceBean implements SlowModeService {
@Override
public void setSlowMode(AChannel channel, Duration duration) {
TextChannel textChannel = bot.getTextChannelFromServer(channel.getServer().getId(), channel.getId());
this.setSlowMode(textChannel, duration);
Optional<TextChannel> textChannelOptional = bot.getTextChannelFromServer(channel.getServer().getId(), channel.getId());
if(textChannelOptional.isPresent()) {
TextChannel textChannel = textChannelOptional.get();
this.setSlowMode(textChannel, duration);
} else {
throw new NotFoundException(String.format("Channel %s not found in guild %s", channel.getId(), channel.getServer().getId()));
}
}
}

View File

@@ -22,6 +22,8 @@ import net.dv8tion.jda.api.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Slf4j
@Component
public class WarnServiceBean implements WarnService {
@@ -57,10 +59,10 @@ public class WarnServiceBean implements WarnService {
Warning warning = warnManagementService.createWarning(warnedAUserInAServer, warningAUserInAServer, reason);
JDA instance = bot.getInstance();
User userBeingWarned = instance.getUserById(warnedAUser.getId());
Guild guildById = bot.getGuildById(serverOfWarning.getId());
Optional<Guild> guildById = bot.getGuildById(serverOfWarning.getId());
String guildName = "<defaultName>";
if(guildById != null) {
guildName = guildById.getName();
if(guildById.isPresent()) {
guildName = guildById.get().getName();
}
WarnNotification warnNotification = WarnNotification.builder().warning(warning).serverName(guildName).build();
if(userBeingWarned != null) {