added mechanism to dynamically load properties defining postTargets and valid emoteNames

changed the output of the exception message
refactored emote management service
added service to define whether or not an emote is usable by the bot
This commit is contained in:
Sheldan
2020-03-22 10:26:58 +01:00
parent 9e9eb615c0
commit d95382b589
25 changed files with 246 additions and 100 deletions

View File

@@ -0,0 +1,10 @@
package dev.sheldan.abstracto.moderation.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:moderation.properties")
public class ModerationConfig {
}

View File

@@ -1,7 +1,6 @@
package dev.sheldan.abstracto.moderation.listener;
import dev.sheldan.abstracto.core.utils.ContextUtils;
import dev.sheldan.abstracto.core.models.database.PostTarget;
import dev.sheldan.abstracto.core.service.MessageCache;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.moderation.models.template.listener.MessageDeletedAttachmentLog;
@@ -20,6 +19,7 @@ import javax.annotation.Nonnull;
@Component
public class MessageDeletedListener extends ListenerAdapter {
private static final String DELETE_LOG_TARGET = "deleteLog";
private static String MESSAGE_DELETED_TEMPLATE = "message_deleted";
private static String MESSAGE_DELETED_ATTACHMENT_TEMPLATE = "message_deleted_attachment";
@@ -43,16 +43,16 @@ public class MessageDeletedListener extends ListenerAdapter {
MessageDeletedLog logModel = (MessageDeletedLog) contextUtils.fromMessage(messageFromCache, MessageDeletedLog.class);
logModel.setMessage(messageFromCache);
String simpleMessageUpdatedMessage = templateService.renderTemplate(MESSAGE_DELETED_TEMPLATE, logModel);
postTargetService.sendTextInPostTarget(simpleMessageUpdatedMessage, PostTarget.EDIT_LOG, event.getGuild().getIdLong());
postTargetService.sendTextInPostTarget(simpleMessageUpdatedMessage, DELETE_LOG_TARGET, event.getGuild().getIdLong());
MessageEmbed embed = templateService.renderEmbedTemplate(MESSAGE_DELETED_TEMPLATE, logModel);
postTargetService.sendEmbedInPostTarget(embed, PostTarget.DELETE_LOG, event.getGuild().getIdLong());
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);
MessageDeletedAttachmentLog log = (MessageDeletedAttachmentLog) contextUtils.fromMessage(messageFromCache, MessageDeletedAttachmentLog.class);
log.setImageUrl(attachment.getProxyUrl());
log.setCounter(i + 1);
MessageEmbed attachmentEmbed = templateService.renderEmbedTemplate(MESSAGE_DELETED_ATTACHMENT_TEMPLATE, log);
postTargetService.sendEmbedInPostTarget(attachmentEmbed, PostTarget.DELETE_LOG, event.getGuild().getIdLong());
postTargetService.sendEmbedInPostTarget(attachmentEmbed, DELETE_LOG_TARGET, event.getGuild().getIdLong());
}
}
}

View File

@@ -1,7 +1,6 @@
package dev.sheldan.abstracto.moderation.listener;
import dev.sheldan.abstracto.core.MessageTextUpdatedListener;
import dev.sheldan.abstracto.core.models.database.PostTarget;
import dev.sheldan.abstracto.core.service.MessageCache;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.moderation.models.template.listener.MessageEditedLog;
@@ -18,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional;
public class MessageEditedListener implements MessageTextUpdatedListener {
private static final String MESSAGE_EDITED_TEMPLATE = "message_edited";
private static final String EDIT_LOG_TARGET = "editLog";
@Autowired
private TemplateService templateService;
@@ -43,9 +43,9 @@ public class MessageEditedListener implements MessageTextUpdatedListener {
.guild(messageAfter.getGuild())
.member(messageAfter.getMember()).build();
String simpleMessageUpdatedMessage = templateService.renderTemplate(MESSAGE_EDITED_TEMPLATE, log);
postTargetService.sendTextInPostTarget(simpleMessageUpdatedMessage, PostTarget.EDIT_LOG, messageAfter.getGuild().getIdLong());
postTargetService.sendTextInPostTarget(simpleMessageUpdatedMessage, EDIT_LOG_TARGET, messageAfter.getGuild().getIdLong());
MessageEmbed embed = templateService.renderEmbedTemplate(MESSAGE_EDITED_TEMPLATE, log);
postTargetService.sendEmbedInPostTarget(embed, PostTarget.DELETE_LOG, messageBefore.getGuild().getIdLong());
postTargetService.sendEmbedInPostTarget(embed, EDIT_LOG_TARGET, messageBefore.getGuild().getIdLong());
}
}

View File

@@ -1,11 +1,8 @@
package dev.sheldan.abstracto.moderation.service;
import dev.sheldan.abstracto.core.models.ServerContext;
import dev.sheldan.abstracto.core.models.database.PostTarget;
import dev.sheldan.abstracto.core.service.Bot;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.moderation.models.template.BanIdLog;
import dev.sheldan.abstracto.moderation.models.template.BanLog;
import dev.sheldan.abstracto.templating.TemplateService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Guild;
@@ -17,8 +14,9 @@ import org.springframework.stereotype.Component;
@Slf4j
public class BanServiceBean implements BanService {
public static final String BAN_LOG_TEMPLATE = "ban_log";
public static final String BAN_ID_LOG_TEMPLATE = "banid_log";
private static final String BAN_LOG_TEMPLATE = "ban_log";
private static final String BAN_ID_LOG_TEMPLATE = "banid_log";
private static final String BAN_LOG_TARGET = "banLog";
@Autowired
private Bot bot;
@@ -32,14 +30,14 @@ public class BanServiceBean implements BanService {
public void banMember(Member member, String reason, ServerContext banLog) {
this.banUser(member.getGuild().getIdLong(), member.getIdLong(), reason);
String warnLogMessage = templateService.renderContextAwareTemplate(BAN_LOG_TEMPLATE, banLog);
postTargetService.sendTextInPostTarget(warnLogMessage, PostTarget.BAN_LOG, banLog.getServer().getId());
postTargetService.sendTextInPostTarget(warnLogMessage, BAN_LOG_TARGET, banLog.getServer().getId());
}
@Override
public void banMember(Long guildId, Long userId, String reason, ServerContext banIdLog) {
banUser(guildId, userId, reason);
String warnLogMessage = templateService.renderContextAwareTemplate(BAN_ID_LOG_TEMPLATE, banIdLog);
postTargetService.sendTextInPostTarget(warnLogMessage, PostTarget.BAN_LOG, guildId);
postTargetService.sendTextInPostTarget(warnLogMessage, BAN_LOG_TARGET, guildId);
}
private void banUser(Long guildId, Long userId, String reason) {

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.moderation.service;
import dev.sheldan.abstracto.core.models.database.PostTarget;
import dev.sheldan.abstracto.core.service.Bot;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.moderation.models.template.KickLogModel;
@@ -15,7 +14,8 @@ import org.springframework.stereotype.Component;
@Slf4j
public class KickServiceBean implements KickService {
public static final String KICK_LOG_TEMPLATE = "kick_log";
private static final String KICK_LOG_TEMPLATE = "kick_log";
private static final String WARN_LOG_TARGET = "warnLog";
@Autowired
private Bot bot;
@@ -38,6 +38,6 @@ public class KickServiceBean implements KickService {
private void sendKickLog(KickLogModel kickLogModel) {
String warnLogMessage = templateService.renderContextAwareTemplate(KICK_LOG_TEMPLATE, kickLogModel);
postTargetService.sendTextInPostTarget(warnLogMessage, PostTarget.WARN_LOG, kickLogModel.getServer().getId());
postTargetService.sendTextInPostTarget(warnLogMessage, WARN_LOG_TARGET, kickLogModel.getServer().getId());
}
}

View File

@@ -1,7 +1,6 @@
package dev.sheldan.abstracto.moderation.service;
import dev.sheldan.abstracto.core.models.ServerContext;
import dev.sheldan.abstracto.core.models.UserInitiatedServerContext;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUser;
import dev.sheldan.abstracto.moderation.models.template.WarnLog;
@@ -11,7 +10,6 @@ import dev.sheldan.abstracto.moderation.service.management.WarnManagementService
import dev.sheldan.abstracto.core.management.ServerManagementService;
import dev.sheldan.abstracto.core.management.UserManagementService;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.models.database.PostTarget;
import dev.sheldan.abstracto.core.service.Bot;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.templating.TemplateService;
@@ -28,6 +26,7 @@ import org.springframework.stereotype.Component;
@Component
public class WarnServiceBean implements WarnService {
public static final String WARN_LOG_TARGET = "warnLog";
@Autowired
private UserManagementService userManagementService;
@@ -86,8 +85,8 @@ public class WarnServiceBean implements WarnService {
private void sendWarnLog(ServerContext warnLogModel) {
String warnLogMessage = templateService.renderContextAwareTemplate(WARN_LOG_TEMPLATE, warnLogModel);
postTargetService.sendTextInPostTarget(warnLogMessage, PostTarget.WARN_LOG, warnLogModel.getServer().getId());
postTargetService.sendTextInPostTarget(warnLogMessage, WARN_LOG_TARGET, warnLogModel.getServer().getId());
MessageEmbed embed = templateService.renderEmbedTemplate("warn_log", warnLogModel);
postTargetService.sendEmbedInPostTarget(embed, PostTarget.WARN_LOG, warnLogModel.getServer().getId());
postTargetService.sendEmbedInPostTarget(embed, WARN_LOG_TARGET, warnLogModel.getServer().getId());
}
}

View File

@@ -0,0 +1,10 @@
package dev.sheldan.abstracto.utility.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:utility.properties")
public class UtilityConfig {
}

View File

@@ -2,7 +2,6 @@ package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.management.EmoteManagementService;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.models.database.PostTarget;
import dev.sheldan.abstracto.core.service.Bot;
import dev.sheldan.abstracto.core.service.MessageService;
import dev.sheldan.abstracto.core.service.PostTargetService;
@@ -24,8 +23,9 @@ import org.springframework.stereotype.Component;
public class SuggestionServiceBean implements SuggestionService {
public static final String SUGGESTION_LOG_TEMPLATE = "suggest_log";
public static final String SUGGESTION_YES_EMOTE = "SUGGESTION_YES";
public static final String SUGGESTION_NO_EMOTE = "SUGGESTION_NO";
private static final String SUGGESTION_YES_EMOTE = "SUGGESTION_YES";
private static final String SUGGESTION_NO_EMOTE = "SUGGESTION_NO";
public static final String SUGGESTIONS_TARGET = "suggestions";
@Autowired
private SuggestionManagementService suggestionManagementService;
@@ -57,7 +57,7 @@ public class SuggestionServiceBean implements SuggestionService {
JDA instance = botService.getInstance();
Guild guildById = instance.getGuildById(guildId);
if(guildById != null) {
postTargetService.sendEmbedInPostTarget(embed, PostTarget.SUGGESTIONS, guildId).thenAccept(message -> {
postTargetService.sendEmbedInPostTarget(embed, SUGGESTIONS_TARGET, guildId).thenAccept(message -> {
messageService.addReactionToMessage(SUGGESTION_YES_EMOTE, guildId, message);
messageService.addReactionToMessage(SUGGESTION_NO_EMOTE, guildId, message);
suggestionManagementService.setPostedMessage(suggestion, message);

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service.management;
import dev.sheldan.abstracto.core.models.database.PostTarget;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.templating.TemplateService;
import dev.sheldan.abstracto.utility.models.template.SuggestionLog;
@@ -15,6 +14,7 @@ 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
@@ -34,7 +34,7 @@ public class AsyncSuggestionServiceBean {
suggestionLog.setReason(text);
suggestionLog.setText(suggestionEmbed.getDescription());
MessageEmbed embed = templateService.renderEmbedTemplate(SUGGESTION_LOG_TEMPLATE, suggestionLog);
postTargetService.sendEmbedInPostTarget(embed, PostTarget.SUGGESTIONS, suggestionLog.getServer().getId());
postTargetService.sendEmbedInPostTarget(embed, SUGGESTIONS_TARGET, suggestionLog.getServer().getId());
}
}

View File

@@ -1 +0,0 @@
abstracto.postTargets.utility=suggestions

View File

@@ -0,0 +1,2 @@
abstracto.postTargets.utility=suggestions
abstracto.emoteNames.suggestion=SUGGESTION_YES,SUGGESTION_NO

View File

@@ -28,7 +28,7 @@ public class ExceptionPostExecution implements PostCommandExecution {
String text = templateService.renderTemplate(exception.getTemplateName(), exception.getTemplateModel());
commandContext.getChannel().sendMessage(text).queue();
} else {
commandContext.getChannel().sendMessage("Exception: " + result.getThrowable().getClass() + ": " + result.getMessage()).queue();
commandContext.getChannel().sendMessage(result.getThrowable().getClass().getSimpleName() + ": " + result.getMessage()).queue();
}
}
}

View File

@@ -0,0 +1,40 @@
package dev.sheldan.abstracto.core;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@Component
@Getter
@Setter
@ConfigurationProperties(prefix = "abstracto")
public class DynamicKeyLoader {
private HashMap<String, String> postTargets = new HashMap<>();
private HashMap<String, String> emoteNames = new HashMap<>();
public List<String> getPostTargetsAsList() {
return getHashMapAsList(postTargets);
}
public List<String> getEmoteNamesAsList() {
return getHashMapAsList(emoteNames);
}
@NotNull
private List<String> getHashMapAsList(HashMap<String, String> emoteNames) {
List<String> emotes = new ArrayList<>();
if (emoteNames == null || emoteNames.size() == 0) {
return emotes;
}
emoteNames.values().forEach(s -> emotes.addAll(Arrays.asList(s.split(","))));
return emotes;
}
}

View File

@@ -1,31 +0,0 @@
package dev.sheldan.abstracto.core;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@Component
@Getter
@Setter
@PropertySource("classpath:abstracto.properties")
@ConfigurationProperties(prefix = "abstracto")
public class PostTargetLoader {
private HashMap<String, String> postTargets = new HashMap<>();
public List<String> getPostTargetsAsList() {
List<String> targets = new ArrayList<>();
if(postTargets == null || postTargets.size() == 0) {
return targets;
}
postTargets.values().forEach(s -> targets.addAll(Arrays.asList(s.split(","))));
return targets;
}
}

View File

@@ -5,7 +5,9 @@ import dev.sheldan.abstracto.command.execution.CommandConfiguration;
import dev.sheldan.abstracto.command.execution.CommandContext;
import dev.sheldan.abstracto.command.execution.Parameter;
import dev.sheldan.abstracto.command.execution.Result;
import dev.sheldan.abstracto.core.exception.ConfigurationException;
import dev.sheldan.abstracto.core.management.EmoteManagementService;
import dev.sheldan.abstracto.core.service.EmoteService;
import net.dv8tion.jda.api.entities.Emote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -19,6 +21,9 @@ public class SetEmote implements Command {
@Autowired
private EmoteManagementService emoteManagementService;
@Autowired
private EmoteService emoteService;
@Override
public Result execute(CommandContext commandContext) {
String emoteKey = (String) commandContext.getParameters().getParameters().get(0);
@@ -28,7 +33,11 @@ public class SetEmote implements Command {
emoteManagementService.setEmoteToDefaultEmote(emoteKey, emote, commandContext.getGuild().getIdLong());
} else {
Emote emote = (Emote) o;
emoteManagementService.setEmoteToCustomEmote(emoteKey, emote, commandContext.getGuild().getIdLong());
if(emoteService.isEmoteUsableByBot(emote)) {
emoteManagementService.setEmoteToCustomEmote(emoteKey, emote, commandContext.getGuild().getIdLong());
} else {
throw new ConfigurationException("Emote is not usable by bot.");
}
}
return Result.fromSuccess();
}

View File

@@ -1,7 +0,0 @@
package dev.sheldan.abstracto.core.exception;
public class PostTargetException extends RuntimeException {
public PostTargetException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,24 @@
package dev.sheldan.abstracto.core.service;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.Guild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class EmoteServiceBean implements EmoteService {
@Autowired
private Bot botService;
@Override
public boolean isEmoteUsableByBot(Emote emote) {
for (Guild guild : botService.getInstance().getGuilds()) {
Emote emoteById = guild.getEmoteById(emote.getId());
if(emoteById != null) {
return true;
}
}
return false;
}
}

View File

@@ -1,5 +1,7 @@
package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.DynamicKeyLoader;
import dev.sheldan.abstracto.core.exception.ConfigurationException;
import dev.sheldan.abstracto.core.management.EmoteManagementService;
import dev.sheldan.abstracto.core.management.ServerManagementService;
import dev.sheldan.abstracto.core.models.database.AEmote;
@@ -9,6 +11,8 @@ import net.dv8tion.jda.api.entities.Emote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class EmoteManagementServiceBean implements EmoteManagementService {
@@ -18,37 +22,97 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
@Autowired
private ServerManagementService serverManagementService;
@Autowired
private DynamicKeyLoader dynamicKeyLoader;
@Override
public AEmote loadEmote(Long id) {
return repository.getOne(id);
}
@Override
public AEmote createCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated, Long serverId) {
AServer server = serverManagementService.loadServer(serverId);
return this.createCustomEmote(name, emoteKey, emoteId, animated, server);
}
@Override
public AEmote createCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated, AServer server) {
validateEmoteName(name);
AEmote emoteToCreate = AEmote
.builder()
.custom(true)
.name(name)
.animated(animated)
.emoteId(emoteId)
.emoteKey(emoteKey)
.serverRef(server)
.build();
repository.save(emoteToCreate);
return emoteToCreate;
}
@Override
public AEmote createDefaultEmote(String name, String emoteKey, Long serverId) {
AServer server = serverManagementService.loadServer(serverId);
return createDefaultEmote(name, emoteKey, server);
}
@Override
public AEmote createDefaultEmote(String name, String emoteKey, AServer server) {
validateEmoteName(name);
AEmote emoteToCreate = AEmote
.builder()
.custom(false)
.name(name)
.emoteKey(emoteKey)
.serverRef(server)
.build();
repository.save(emoteToCreate);
return emoteToCreate;
}
@Override
public AEmote loadEmoteByName(String name, Long serverId) {
AServer server = serverManagementService.loadServer(serverId);
return loadEmoteByName(name, server);
}
@Override
public AEmote loadEmoteByName(String name, AServer server) {
return repository.findAEmoteByNameAndServerRef(name, server);
}
@Override
public AEmote setEmoteToCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated, Long serverId) {
AEmote existing = loadEmoteByName(name, serverId);
existing.setEmoteKey(emoteKey);
existing.setEmoteId(emoteId);
existing.setAnimated(animated);
existing.setCustom(true);
repository.save(existing);
return existing;
AServer server = serverManagementService.loadServer(serverId);
AEmote emote;
if(!emoteExists(name, server)) {
emote = this.createCustomEmote(name, emoteKey, emoteId, animated, server);
} else {
emote = loadEmoteByName(name, server);
emote.setEmoteKey(emoteKey);
emote.setEmoteId(emoteId);
emote.setAnimated(animated);
emote.setCustom(true);
repository.save(emote);
}
return emote;
}
@Override
public AEmote setEmoteToCustomEmote(String name, Emote emote, Long serverId) {
AEmote existing = loadEmoteByName(name, serverId);
existing.setCustom(true);
existing.setAnimated(emote.isAnimated());
existing.setEmoteId(emote.getIdLong());
existing.setEmoteKey(emote.getName());
repository.save(existing);
return existing;
AServer server = serverManagementService.loadServer(serverId);
AEmote emoteBeingSet;
if(!emoteExists(name, server)) {
emoteBeingSet = this.createDefaultEmote(name, emote.getName(), server);
} else {
emoteBeingSet = loadEmoteByName(name, serverId);
emoteBeingSet.setCustom(false);
emoteBeingSet.setEmoteKey(emote.getName());
repository.save(emoteBeingSet);
}
return emoteBeingSet;
}
@Override
@@ -60,6 +124,17 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
return existing;
}
@Override
public boolean emoteExists(String name, Long serverId) {
AServer server = serverManagementService.loadServer(serverId);
return emoteExists(name, server);
}
@Override
public boolean emoteExists(String name, AServer server) {
return repository.existsByNameAndServerRef(name, server);
}
@Override
public AEmote createCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated) {
AEmote emote = AEmote.builder()
@@ -83,4 +158,11 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
repository.save(emote);
return emote;
}
private void validateEmoteName(String name) {
List<String> possibleEmotes = dynamicKeyLoader.getEmoteNamesAsList();
if(!possibleEmotes.contains(name)) {
throw new ConfigurationException("Emote `" + name + "` is not defined. Possible values are: " + String.join(", ", possibleEmotes));
}
}
}

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.PostTargetLoader;
import dev.sheldan.abstracto.core.exception.PostTargetException;
import dev.sheldan.abstracto.core.DynamicKeyLoader;
import dev.sheldan.abstracto.core.exception.ConfigurationException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.PostTarget;
@@ -14,6 +14,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Slf4j
public class PostTargetManagementBean implements PostTargetManagement {
@@ -27,12 +29,13 @@ public class PostTargetManagementBean implements PostTargetManagement {
private ServerManagementService serverManagementService;
@Autowired
private PostTargetLoader postTargetLoader;
private DynamicKeyLoader dynamicKeyLoader;
@Override
public void createPostTarget(String name, AServer server, AChannel targetChannel) {
if(!postTargetLoader.getPostTargetsAsList().contains(name)) {
throw new PostTargetException("PostTarget not found");
List<String> possiblePostTargets = dynamicKeyLoader.getPostTargetsAsList();
if(!possiblePostTargets.contains(name)) {
throw new ConfigurationException("PostTarget not found. Possible values are: " + String.join(", ", possiblePostTargets));
}
log.info("Creating post target {} pointing towards {}", name, targetChannel);
postTargetRepository.save(PostTarget.builder().name(name).channelReference(targetChannel).serverReference(server).build());

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.listener;
import dev.sheldan.abstracto.core.models.database.PostTarget;
import dev.sheldan.abstracto.core.service.PostTargetService;
import dev.sheldan.abstracto.core.management.ServerManagementService;
import dev.sheldan.abstracto.templating.TemplateService;
@@ -23,6 +22,8 @@ public class JoinLeaveListener extends ListenerAdapter {
private static final String USER_JOIN_TEMPLATE = "user_join";
private static final String USER_LEAVE_TEMPLATE = "user_leave";
private static final String JOIN_LOG_TARGET = "joinLog";
private static final String LEAVE_LOG_TARGET = "leaveLog";
@Autowired
private ServerManagementService serverManagementService;
@@ -37,14 +38,14 @@ public class JoinLeaveListener extends ListenerAdapter {
@Transactional
public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) {
String text = getRenderedEvent(event.getUser(), USER_JOIN_TEMPLATE);
postTargetService.sendTextInPostTarget(text, PostTarget.JOIN_LOG, event.getGuild().getIdLong());
postTargetService.sendTextInPostTarget(text, JOIN_LOG_TARGET, event.getGuild().getIdLong());
}
@Override
@Transactional
public void onGuildMemberLeave(@Nonnull GuildMemberLeaveEvent event) {
String text = getRenderedEvent(event.getUser(), USER_LEAVE_TEMPLATE);
postTargetService.sendTextInPostTarget(text, PostTarget.LEAVE_LOG, event.getGuild().getIdLong());
postTargetService.sendTextInPostTarget(text, LEAVE_LOG_TARGET, event.getGuild().getIdLong());
}
@NotNull

View File

@@ -2,10 +2,12 @@ package dev.sheldan.abstracto.repository;
import dev.sheldan.abstracto.core.models.database.AEmote;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmoteRepository extends JpaRepository<AEmote, Long> {
AEmote findAEmoteByNameAndServerRef(String name, AServer server);
boolean existsByNameAndServerRef(String name, AServer server);
}

View File

@@ -1,14 +1,22 @@
package dev.sheldan.abstracto.core.management;
import dev.sheldan.abstracto.core.models.database.AEmote;
import dev.sheldan.abstracto.core.models.database.AServer;
import net.dv8tion.jda.api.entities.Emote;
public interface EmoteManagementService {
AEmote loadEmote(Long id);
AEmote createCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated, Long serverId);
AEmote createCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated, AServer server);
AEmote createDefaultEmote(String name, String emoteKey, Long serverId);
AEmote createDefaultEmote(String name, String emoteKey, AServer server);
AEmote loadEmoteByName(String name, Long serverId);
AEmote loadEmoteByName(String name, AServer server);
AEmote setEmoteToCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated, Long serverId);
AEmote setEmoteToCustomEmote(String name, Emote emote, Long serverId);
AEmote setEmoteToDefaultEmote(String name, String emoteKey, Long serverId);
boolean emoteExists(String name, Long serverId);
boolean emoteExists(String name, AServer server);
AEmote createCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated);
AEmote createDefaultEmote(String name, String emoteKey);
}

View File

@@ -32,14 +32,4 @@ public class PostTarget {
@Getter @Setter
private AServer serverReference;
public static String JOIN_LOG = "joinLog";
public static String LEAVE_LOG = "leaveLog";
public static String WARN_LOG = "warnLog";
public static String KICK_LOG = "kickLog";
public static String BAN_LOG = "banLog";
public static String EDIT_LOG = "editLog";
public static String DELETE_LOG = "deleteLog";
public static String SUGGESTIONS = "suggestions";
public static List<String> AVAILABLE_POST_TARGETS = Arrays.asList(JOIN_LOG, LEAVE_LOG, WARN_LOG, KICK_LOG, BAN_LOG, EDIT_LOG, DELETE_LOG, SUGGESTIONS);
}

View File

@@ -0,0 +1,7 @@
package dev.sheldan.abstracto.core.service;
import net.dv8tion.jda.api.entities.Emote;
public interface EmoteService {
boolean isEmoteUsableByBot(Emote emote);
}