mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-30 15:11:38 +00:00
added some utility methods to bot service
reworked message cache to not be jda messages, but own objects: cachedMessages, jda messages should not be kept around added listener to listen for message links, to represent them in an embed (or embeds) added offsetDateTime handling for gson introduced a way on how to handle async transaction handling for suggestions and message loading (self reference with another method with transactional) added timestamp support to embeds
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package dev.sheldan.abstracto.moderation.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.CachedMessage;
|
||||
import dev.sheldan.abstracto.core.utils.ContextUtils;
|
||||
import dev.sheldan.abstracto.core.service.MessageCache;
|
||||
import dev.sheldan.abstracto.core.service.PostTargetService;
|
||||
import dev.sheldan.abstracto.moderation.models.template.listener.MessageDeletedAttachmentLog;
|
||||
import dev.sheldan.abstracto.moderation.models.template.listener.MessageDeletedLog;
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent;
|
||||
@@ -15,8 +17,10 @@ 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 {
|
||||
|
||||
private static final String DELETE_LOG_TARGET = "deleteLog";
|
||||
@@ -38,18 +42,22 @@ public class MessageDeletedListener extends ListenerAdapter {
|
||||
@Override
|
||||
@Transactional
|
||||
public void onGuildMessageDelete(@Nonnull GuildMessageDeleteEvent event) {
|
||||
Message messageFromCache = messageCache.getMessageFromCache(event.getMessageIdLong(),
|
||||
event.getChannel().getIdLong(), event.getGuild().getIdLong());
|
||||
CachedMessage messageFromCache = null;
|
||||
try {
|
||||
messageFromCache = messageCache.getMessageFromCache(event.getGuild().getIdLong(), event.getChannel().getIdLong(), event.getMessageIdLong());
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
log.warn("Failed to load message.", e);
|
||||
return;
|
||||
}
|
||||
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());
|
||||
MessageEmbed embed = templateService.renderEmbedTemplate(MESSAGE_DELETED_TEMPLATE, logModel);
|
||||
postTargetService.sendEmbedInPostTarget(embed, DELETE_LOG_TARGET, event.getGuild().getIdLong());
|
||||
for (int i = 0; i < messageFromCache.getAttachments().size(); i++) {
|
||||
Message.Attachment attachment = messageFromCache.getAttachments().get(i);
|
||||
for (int i = 0; i < messageFromCache.getAttachmentUrls().size(); i++) {
|
||||
MessageDeletedAttachmentLog log = (MessageDeletedAttachmentLog) contextUtils.fromMessage(messageFromCache, MessageDeletedAttachmentLog.class);
|
||||
log.setImageUrl(attachment.getProxyUrl());
|
||||
log.setImageUrl(messageFromCache.getAttachmentUrls().get(i));
|
||||
log.setCounter(i + 1);
|
||||
MessageEmbed attachmentEmbed = templateService.renderEmbedTemplate(MESSAGE_DELETED_ATTACHMENT_TEMPLATE, log);
|
||||
postTargetService.sendEmbedInPostTarget(attachmentEmbed, DELETE_LOG_TARGET, event.getGuild().getIdLong());
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.sheldan.abstracto.moderation.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.MessageTextUpdatedListener;
|
||||
import dev.sheldan.abstracto.core.models.CachedMessage;
|
||||
import dev.sheldan.abstracto.core.service.MessageCache;
|
||||
import dev.sheldan.abstracto.core.service.PostTargetService;
|
||||
import dev.sheldan.abstracto.moderation.models.template.listener.MessageEditedLog;
|
||||
@@ -30,8 +31,8 @@ public class MessageEditedListener implements MessageTextUpdatedListener {
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void execute(Message messageBefore, Message messageAfter) {
|
||||
if(messageBefore.getContentRaw().equals(messageAfter.getContentRaw())){
|
||||
public void execute(CachedMessage messageBefore, Message messageAfter) {
|
||||
if(messageBefore.getContent().equals(messageAfter.getContentRaw())){
|
||||
log.debug("Message content was the same. Possible reason was: message was not in cache.");
|
||||
return;
|
||||
}
|
||||
@@ -45,7 +46,7 @@ public class MessageEditedListener implements MessageTextUpdatedListener {
|
||||
String simpleMessageUpdatedMessage = templateService.renderTemplate(MESSAGE_EDITED_TEMPLATE, log);
|
||||
postTargetService.sendTextInPostTarget(simpleMessageUpdatedMessage, EDIT_LOG_TARGET, messageAfter.getGuild().getIdLong());
|
||||
MessageEmbed embed = templateService.renderEmbedTemplate(MESSAGE_EDITED_TEMPLATE, log);
|
||||
postTargetService.sendEmbedInPostTarget(embed, EDIT_LOG_TARGET, messageBefore.getGuild().getIdLong());
|
||||
postTargetService.sendEmbedInPostTarget(embed, EDIT_LOG_TARGET, messageBefore.getServerId());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,12 +41,8 @@ public class BanServiceBean implements BanService {
|
||||
}
|
||||
|
||||
private void banUser(Long guildId, Long userId, String reason) {
|
||||
Guild guildById = bot.getInstance().getGuildById(guildId);
|
||||
if(guildById != null) {
|
||||
log.info("Banning user {} in guild {}.", userId, guildId);
|
||||
guildById.ban(userId.toString(), 0, reason).queue();
|
||||
} else {
|
||||
log.warn("Guild id {} from member was not found.", guildId);
|
||||
}
|
||||
Guild guildById = bot.getGuildById(guildId);
|
||||
log.info("Banning user {} in guild {}.", userId, guildId);
|
||||
guildById.ban(userId.toString(), 0, reason).queue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,13 +27,9 @@ public class KickServiceBean implements KickService {
|
||||
|
||||
@Override
|
||||
public void kickMember(Member member, String reason, KickLogModel kickLogModel) {
|
||||
Guild guildById = bot.getInstance().getGuildById(member.getGuild().getIdLong());
|
||||
if(guildById != null) {
|
||||
guildById.kick(member, reason).queue();
|
||||
this.sendKickLog(kickLogModel);
|
||||
} else {
|
||||
log.warn("Failed to kick member {} from guild {}. Guild was not found.", member.getId(), member.getGuild().getId());
|
||||
}
|
||||
Guild guildById = bot.getGuildById(kickLogModel.getGuild().getIdLong());
|
||||
guildById.kick(member, reason).queue();
|
||||
this.sendKickLog(kickLogModel);
|
||||
}
|
||||
|
||||
private void sendKickLog(KickLogModel kickLogModel) {
|
||||
|
||||
@@ -29,17 +29,7 @@ public class SlowModeServiceBean implements SlowModeService {
|
||||
|
||||
@Override
|
||||
public void setSlowMode(AChannel channel, Duration duration) {
|
||||
JDA jpaInstance = bot.getInstance();
|
||||
Guild guild = jpaInstance.getGuildById(channel.getServer().getId());
|
||||
if(guild != null) {
|
||||
TextChannel textChannelById = guild.getTextChannelById(channel.getId());
|
||||
if(textChannelById != null) {
|
||||
this.setSlowMode(textChannelById, duration);
|
||||
} else {
|
||||
log.warn("Channel {} was not found in guild {} when trying to set a slow mode.", channel.getId(), channel.getServer().getId());
|
||||
}
|
||||
} else {
|
||||
log.warn("Guild {} was not found when trying to set slow mode.", channel.getServer().getId());
|
||||
}
|
||||
TextChannel textChannel = bot.getTextChannelFromServer(channel.getServer().getId(), channel.getId());
|
||||
this.setSlowMode(textChannel, duration);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public class WarnServiceBean implements WarnService {
|
||||
Warning warning = warnManagementService.createWarning(warnedAUserInAServer, warningAUserInAServer, reason);
|
||||
JDA instance = bot.getInstance();
|
||||
User userBeingWarned = instance.getUserById(warnedAUser.getId());
|
||||
Guild guildById = instance.getGuildById(serverOfWarning.getId());
|
||||
Guild guildById = bot.getGuildById(serverOfWarning.getId());
|
||||
String guildName = "<defaultName>";
|
||||
if(guildById != null) {
|
||||
guildName = guildById.getName();
|
||||
|
||||
@@ -12,11 +12,11 @@
|
||||
"fields": [
|
||||
{
|
||||
"name": ":x: Original Message: ",
|
||||
"value": "${message.contentRaw}"
|
||||
"value": "${message.content}"
|
||||
},
|
||||
{
|
||||
"name": "Link",
|
||||
"value": "[${textChannel.name}](${message.jumpUrl})"
|
||||
"value": "[${textChannel.name}](${message.messageUrl})"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
Message from ${member.effectiveName} (${member.idLong?c}) deleted in ${textChannel.asMention}.
|
||||
Message: ${message.contentRaw}
|
||||
Message: ${message.content}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package dev.sheldan.abstracto.moderation.models.template.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.CachedMessage;
|
||||
import dev.sheldan.abstracto.core.models.UserInitiatedServerContext;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
|
||||
@Getter @Setter @SuperBuilder
|
||||
public class MessageDeletedLog extends UserInitiatedServerContext {
|
||||
private Message message;
|
||||
private CachedMessage message;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.moderation.models.template.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.CachedMessage;
|
||||
import dev.sheldan.abstracto.core.models.UserInitiatedServerContext;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -9,5 +10,5 @@ import net.dv8tion.jda.api.entities.Message;
|
||||
@Getter @Setter @SuperBuilder
|
||||
public class MessageEditedLog extends UserInitiatedServerContext {
|
||||
private Message messageAfter;
|
||||
private Message messageBefore;
|
||||
private CachedMessage messageBefore;
|
||||
}
|
||||
|
||||
@@ -10,13 +10,16 @@ import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import dev.sheldan.abstracto.utility.models.Suggestion;
|
||||
import dev.sheldan.abstracto.utility.models.SuggestionState;
|
||||
import dev.sheldan.abstracto.utility.models.template.SuggestionLog;
|
||||
import dev.sheldan.abstracto.utility.service.management.AsyncSuggestionServiceBean;
|
||||
import dev.sheldan.abstracto.utility.service.management.SuggestionManagementService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -38,15 +41,15 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
@Autowired
|
||||
private Bot botService;
|
||||
|
||||
@Autowired
|
||||
private AsyncSuggestionServiceBean suggestionServiceBean;
|
||||
|
||||
@Autowired
|
||||
private EmoteManagementService emoteManagementService;
|
||||
|
||||
@Autowired
|
||||
private MessageService messageService;
|
||||
|
||||
@Autowired
|
||||
private SuggestionServiceBean self;
|
||||
|
||||
@Override
|
||||
public void createSuggestion(Member member, String text, SuggestionLog suggestionLog) {
|
||||
Suggestion suggestion = suggestionManagementService.createSuggestion(member, text);
|
||||
@@ -93,13 +96,25 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
TextChannel textChannelById = guildById.getTextChannelById(channelId);
|
||||
if(textChannelById != null) {
|
||||
textChannelById.retrieveMessageById(originalMessageId).queue(message -> {
|
||||
suggestionServiceBean.updateSuggestionMessageText(text, suggestionLog, message);
|
||||
self.updateSuggestionMessageText(text, suggestionLog, message);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void updateSuggestionMessageText(String text, SuggestionLog suggestionLog, Message message) {
|
||||
Optional<MessageEmbed> embedOptional = message.getEmbeds().stream().filter(embed -> embed.getDescription() != null).findFirst();
|
||||
if(embedOptional.isPresent()) {
|
||||
MessageEmbed suggestionEmbed = embedOptional.get();
|
||||
suggestionLog.setReason(text);
|
||||
suggestionLog.setText(suggestionEmbed.getDescription());
|
||||
MessageEmbed embed = templateService.renderEmbedTemplate(SUGGESTION_LOG_TEMPLATE, suggestionLog);
|
||||
postTargetService.sendEmbedInPostTarget(embed, SUGGESTIONS_TARGET, suggestionLog.getServer().getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rejectSuggestion(Long suggestionId, String text, SuggestionLog log) {
|
||||
Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId);
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
package dev.sheldan.abstracto.utility.service.management;
|
||||
|
||||
import dev.sheldan.abstracto.core.service.PostTargetService;
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import dev.sheldan.abstracto.utility.models.template.SuggestionLog;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static dev.sheldan.abstracto.utility.service.SuggestionServiceBean.SUGGESTION_LOG_TEMPLATE;
|
||||
import static dev.sheldan.abstracto.utility.service.SuggestionServiceBean.SUGGESTIONS_TARGET;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AsyncSuggestionServiceBean {
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Autowired
|
||||
private PostTargetService postTargetService;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void updateSuggestionMessageText(String text, SuggestionLog suggestionLog, Message message) {
|
||||
Optional<MessageEmbed> embedOptional = message.getEmbeds().stream().filter(embed -> embed.getDescription() != null).findFirst();
|
||||
if(embedOptional.isPresent()) {
|
||||
MessageEmbed suggestionEmbed = embedOptional.get();
|
||||
suggestionLog.setReason(text);
|
||||
suggestionLog.setText(suggestionEmbed.getDescription());
|
||||
MessageEmbed embed = templateService.renderEmbedTemplate(SUGGESTION_LOG_TEMPLATE, suggestionLog);
|
||||
postTargetService.sendEmbedInPostTarget(embed, SUGGESTIONS_TARGET, suggestionLog.getServer().getId());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user