mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-27 23:09:05 +00:00
reworked exception handling, different exceptions whether or why it failed, all of which are unchecked
added feature flags for commands/listeners added enable/disable command added feature flag handling to command received handler added boolean parameter support fixed 1 parameter commands fixed posttargets not working on multiple servers, removed unique constraint on posttarget names added setup validation to suggestions added quartz property loader moved join/leave logger to moderation reworked the way the message listener are handled (separate listener around)
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package dev.sheldan.abstracto.utility.command;
|
||||
|
||||
import dev.sheldan.abstracto.command.Command;
|
||||
import dev.sheldan.abstracto.command.HelpInfo;
|
||||
import dev.sheldan.abstracto.command.*;
|
||||
import dev.sheldan.abstracto.command.execution.*;
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import dev.sheldan.abstracto.utility.Utility;
|
||||
import dev.sheldan.abstracto.utility.config.UtilityFeatures;
|
||||
import dev.sheldan.abstracto.utility.models.template.ShowEmoteLog;
|
||||
import net.dv8tion.jda.api.entities.Emote;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -14,7 +14,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class ShowEmote implements Command {
|
||||
public class ShowEmote extends AbstractFeatureFlaggedCommand {
|
||||
|
||||
private static final String SHOW_EMOTE_RESPONSE_TEMPLATE = "showEmote_response";
|
||||
|
||||
@@ -22,18 +22,18 @@ public class ShowEmote implements Command {
|
||||
private TemplateService templateService;
|
||||
|
||||
@Override
|
||||
public Result execute(CommandContext commandContext) {
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
Object emoteParameter = parameters.get(0);
|
||||
if(!(emoteParameter instanceof Emote)) {
|
||||
return Result.fromError("No custom emote found.");
|
||||
return CommandResult.fromError("No custom emote found.");
|
||||
}
|
||||
Emote emote = (Emote) emoteParameter;
|
||||
ShowEmoteLog emoteLog = (ShowEmoteLog) ContextConverter.fromCommandContext(commandContext, ShowEmoteLog.class);
|
||||
emoteLog.setEmote(emote);
|
||||
String message = templateService.renderTemplate(SHOW_EMOTE_RESPONSE_TEMPLATE, emoteLog);
|
||||
commandContext.getChannel().sendMessage(message).queue();
|
||||
return Result.fromSuccess();
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -50,4 +50,9 @@ public class ShowEmote implements Command {
|
||||
.help(helpInfo)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFeature() {
|
||||
return UtilityFeatures.UTILITY;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package dev.sheldan.abstracto.utility.command;
|
||||
|
||||
import dev.sheldan.abstracto.command.Command;
|
||||
import dev.sheldan.abstracto.command.AbstractFeatureFlaggedCommand;
|
||||
import dev.sheldan.abstracto.command.HelpInfo;
|
||||
import dev.sheldan.abstracto.command.execution.CommandConfiguration;
|
||||
import dev.sheldan.abstracto.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.command.execution.Parameter;
|
||||
import dev.sheldan.abstracto.command.execution.Result;
|
||||
import dev.sheldan.abstracto.core.models.embed.MessageToSend;
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import dev.sheldan.abstracto.utility.Utility;
|
||||
import dev.sheldan.abstracto.utility.config.UtilityFeatures;
|
||||
import dev.sheldan.abstracto.utility.models.template.starboard.StarStatsModel;
|
||||
import dev.sheldan.abstracto.utility.service.StarboardService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -18,7 +19,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class StarStats implements Command {
|
||||
public class StarStats extends AbstractFeatureFlaggedCommand {
|
||||
|
||||
public static final String STARSTATS_RESPONSE_TEMPLATE = "starStats_response";
|
||||
@Autowired
|
||||
@@ -28,11 +29,11 @@ public class StarStats implements Command {
|
||||
private TemplateService templateService;
|
||||
|
||||
@Override
|
||||
public Result execute(CommandContext commandContext) {
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
StarStatsModel result = starboardService.retrieveStarStats(commandContext.getGuild().getIdLong());
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(STARSTATS_RESPONSE_TEMPLATE, result);
|
||||
commandContext.getChannel().sendMessage(messageToSend.getEmbed()).queue();
|
||||
return Result.fromSuccess();
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -48,4 +49,9 @@ public class StarStats implements Command {
|
||||
.help(helpInfo)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFeature() {
|
||||
return UtilityFeatures.STARBOARD;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package dev.sheldan.abstracto.utility.command.remind;
|
||||
|
||||
import dev.sheldan.abstracto.command.Command;
|
||||
import dev.sheldan.abstracto.command.HelpInfo;
|
||||
import dev.sheldan.abstracto.command.*;
|
||||
import dev.sheldan.abstracto.command.execution.*;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.utility.config.UtilityFeatures;
|
||||
import dev.sheldan.abstracto.utility.Utility;
|
||||
import dev.sheldan.abstracto.utility.models.template.ReminderModel;
|
||||
import dev.sheldan.abstracto.utility.service.ReminderService;
|
||||
@@ -16,13 +16,13 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class Remind implements Command {
|
||||
public class Remind extends AbstractFeatureFlaggedCommand {
|
||||
|
||||
@Autowired
|
||||
private ReminderService remindService;
|
||||
|
||||
@Override
|
||||
public Result execute(CommandContext commandContext) {
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
Duration remindTime = (Duration) parameters.get(0);
|
||||
String text = (String) parameters.get(1);
|
||||
@@ -31,7 +31,7 @@ public class Remind implements Command {
|
||||
remindModel.setMessage(commandContext.getMessage());
|
||||
remindModel.setRemindText(text);
|
||||
remindService.createReminderInForUser(aUserInAServer, text, remindTime, remindModel);
|
||||
return Result.fromSuccess();
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,4 +49,9 @@ public class Remind implements Command {
|
||||
.help(helpInfo)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFeature() {
|
||||
return UtilityFeatures.REMIND;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package dev.sheldan.abstracto.utility.command.suggest;
|
||||
|
||||
import dev.sheldan.abstracto.command.Command;
|
||||
import dev.sheldan.abstracto.command.AbstractFeatureFlaggedCommand;
|
||||
import dev.sheldan.abstracto.command.HelpInfo;
|
||||
import dev.sheldan.abstracto.command.execution.*;
|
||||
import dev.sheldan.abstracto.utility.Utility;
|
||||
import dev.sheldan.abstracto.utility.config.UtilityFeatures;
|
||||
import dev.sheldan.abstracto.utility.models.template.SuggestionLog;
|
||||
import dev.sheldan.abstracto.utility.service.SuggestionService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -13,19 +14,20 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class Accept implements Command {
|
||||
public class Accept extends AbstractFeatureFlaggedCommand {
|
||||
|
||||
@Autowired
|
||||
private SuggestionService suggestionService;
|
||||
|
||||
@Override
|
||||
public Result execute(CommandContext commandContext) {
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
suggestionService.validateSetup(commandContext.getGuild().getIdLong());
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
Long suggestionId = (Long) parameters.get(0);
|
||||
String text = parameters.size() == 2 ? (String) parameters.get(1) : "";
|
||||
SuggestionLog suggestionModel = (SuggestionLog) ContextConverter.fromCommandContext(commandContext, SuggestionLog.class);
|
||||
suggestionService.acceptSuggestion(suggestionId, text, suggestionModel);
|
||||
return Result.fromSuccess();
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,4 +45,9 @@ public class Accept implements Command {
|
||||
.help(helpInfo)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFeature() {
|
||||
return UtilityFeatures.SUGGEST;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package dev.sheldan.abstracto.utility.command.suggest;
|
||||
|
||||
import dev.sheldan.abstracto.command.Command;
|
||||
import dev.sheldan.abstracto.command.AbstractFeatureFlaggedCommand;
|
||||
import dev.sheldan.abstracto.command.HelpInfo;
|
||||
import dev.sheldan.abstracto.command.execution.*;
|
||||
import dev.sheldan.abstracto.utility.Utility;
|
||||
import dev.sheldan.abstracto.utility.config.UtilityFeatures;
|
||||
import dev.sheldan.abstracto.utility.models.template.SuggestionLog;
|
||||
import dev.sheldan.abstracto.utility.service.SuggestionService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -13,18 +14,20 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class Reject implements Command {
|
||||
public class Reject extends AbstractFeatureFlaggedCommand {
|
||||
|
||||
@Autowired
|
||||
private SuggestionService suggestionService;
|
||||
|
||||
@Override
|
||||
public Result execute(CommandContext commandContext) {
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
suggestionService.validateSetup(commandContext.getGuild().getIdLong());
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
Long suggestionId = (Long) parameters.get(0);
|
||||
String text = parameters.size() == 2 ? (String) parameters.get(1) : "";
|
||||
SuggestionLog suggestionModel = (SuggestionLog) ContextConverter.fromCommandContext(commandContext, SuggestionLog.class);
|
||||
suggestionService.rejectSuggestion(suggestionId, text, suggestionModel);
|
||||
return Result.fromSuccess();
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -42,4 +45,9 @@ public class Reject implements Command {
|
||||
.help(helpInfo)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFeature() {
|
||||
return UtilityFeatures.SUGGEST;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,33 @@
|
||||
package dev.sheldan.abstracto.utility.command.suggest;
|
||||
|
||||
import dev.sheldan.abstracto.command.Command;
|
||||
import dev.sheldan.abstracto.command.AbstractFeatureFlaggedCommand;
|
||||
import dev.sheldan.abstracto.command.HelpInfo;
|
||||
import dev.sheldan.abstracto.command.execution.*;
|
||||
import dev.sheldan.abstracto.utility.Utility;
|
||||
import dev.sheldan.abstracto.utility.config.UtilityFeatures;
|
||||
import dev.sheldan.abstracto.utility.models.template.SuggestionLog;
|
||||
import dev.sheldan.abstracto.utility.service.SuggestionService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class Suggest implements Command {
|
||||
public class Suggest extends AbstractFeatureFlaggedCommand {
|
||||
|
||||
@Autowired
|
||||
private SuggestionService suggestionService;
|
||||
|
||||
@Override
|
||||
public Result execute(CommandContext commandContext) {
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
suggestionService.validateSetup(commandContext.getGuild().getIdLong());
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
String text = (String) parameters.get(0);
|
||||
SuggestionLog suggestLogModel = (SuggestionLog) ContextConverter.fromCommandContext(commandContext, SuggestionLog.class);
|
||||
suggestLogModel.setSuggester(commandContext.getAuthor());
|
||||
suggestionService.createSuggestion(commandContext.getAuthor(), text, suggestLogModel);
|
||||
return Result.fromSuccess();
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,4 +44,9 @@ public class Suggest implements Command {
|
||||
.help(helpInfo)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFeature() {
|
||||
return UtilityFeatures.SUGGEST;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
package dev.sheldan.abstracto.utility.config;
|
||||
|
||||
import dev.sheldan.abstracto.core.listener.ServerConfigListener;
|
||||
import dev.sheldan.abstracto.core.management.ServerManagementService;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.management.ConfigManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.ConfigManagementService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package dev.sheldan.abstracto.utility.config;
|
||||
|
||||
public class UtilityFeatures {
|
||||
public static String REMIND = "remind";
|
||||
public static String STARBOARD = "starboard";
|
||||
public static String SUGGEST = "suggestion";
|
||||
public static String UTILITY = "utility";
|
||||
public static String LINK_EMBEDS = "embeds";
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package dev.sheldan.abstracto.utility.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.listener.MessageReceivedListener;
|
||||
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserManagementService;
|
||||
import dev.sheldan.abstracto.core.models.CachedMessage;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.models.listener.MessageEmbeddedModel;
|
||||
import dev.sheldan.abstracto.core.service.Bot;
|
||||
import dev.sheldan.abstracto.core.service.MessageCache;
|
||||
import dev.sheldan.abstracto.core.models.embed.MessageToSend;
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import dev.sheldan.abstracto.utility.config.UtilityFeatures;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
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 javax.annotation.Nonnull;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class MessageEmbedListener implements MessageReceivedListener {
|
||||
|
||||
@Autowired
|
||||
private MessageCache messageCache;
|
||||
|
||||
public static final String MESSAGE_EMBED_TEMPLATE = "message";
|
||||
|
||||
private Pattern messageRegex = Pattern.compile("(?<whole>https://discordapp.com/channels/(?<server>\\d+)/(?<channel>\\d+)/(?<message>\\d+)(?:.*?))+");
|
||||
|
||||
@Autowired
|
||||
private MessageEmbedListener self;
|
||||
|
||||
@Autowired
|
||||
private ChannelManagementService channelManagementService;
|
||||
|
||||
@Autowired
|
||||
private ServerManagementService serverManagementService;
|
||||
|
||||
@Autowired
|
||||
private UserManagementService userManagementService;
|
||||
|
||||
@Autowired
|
||||
private Bot bot;
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void createEmbedAndPostEmbed(@Nonnull Message postedMessage, CachedMessage message) {
|
||||
MessageEmbeddedModel messageEmbeddedModel = buildTemplateParameter(postedMessage, message);
|
||||
MessageToSend embed = templateService.renderEmbedTemplate(MESSAGE_EMBED_TEMPLATE, messageEmbeddedModel);
|
||||
if(StringUtils.isBlank(embed.getMessage())) {
|
||||
postedMessage.getChannel().sendMessage(embed.getEmbed()).queue();
|
||||
} else {
|
||||
postedMessage.getChannel().sendMessage(embed.getMessage()).embed(embed.getEmbed()).queue();
|
||||
}
|
||||
}
|
||||
|
||||
private MessageEmbeddedModel buildTemplateParameter(Message message, CachedMessage embeddedMessage) {
|
||||
AChannel channel = channelManagementService.loadChannel(message.getChannel().getIdLong());
|
||||
AServer server = serverManagementService.loadOrCreate(message.getGuild().getIdLong());
|
||||
AUserInAServer user = userManagementService.loadUser(message.getMember());
|
||||
Member author = bot.getMemberInServer(embeddedMessage.getServerId(), embeddedMessage.getAuthorId());
|
||||
TextChannel sourceChannel = bot.getTextChannelFromServer(embeddedMessage.getServerId(), embeddedMessage.getChannelId()).get();
|
||||
return MessageEmbeddedModel
|
||||
.builder()
|
||||
.channel(channel)
|
||||
.server(server)
|
||||
.member(message.getMember())
|
||||
.aUserInAServer(user)
|
||||
.author(author)
|
||||
.sourceChannel(sourceChannel)
|
||||
.embeddingUser(message.getMember())
|
||||
.user(user.getUserReference())
|
||||
.messageChannel(message.getChannel())
|
||||
.guild(message.getGuild())
|
||||
.embeddedMessage(embeddedMessage)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Message message) {
|
||||
String messageRaw = message.getContentRaw();
|
||||
Matcher matcher = messageRegex.matcher(messageRaw);
|
||||
boolean matched = false;
|
||||
while(matcher.find()) {
|
||||
matched = true;
|
||||
String serverId = matcher.group("server");
|
||||
String channelId = matcher.group("channel");
|
||||
String messageId = matcher.group("message");
|
||||
String wholeLink = matcher.group("whole");
|
||||
if(message.getGuild().getId().equals(serverId)) {
|
||||
Long serverIdLong = Long.parseLong(serverId);
|
||||
Long channelIdLong = Long.parseLong(channelId);
|
||||
Long messageIdLong = Long.parseLong(messageId);
|
||||
messageRaw = messageRaw.replace(wholeLink, "");
|
||||
messageCache.getMessageFromCache(serverIdLong, channelIdLong, messageIdLong).thenAccept(cachedMessage -> {
|
||||
self.createEmbedAndPostEmbed(message, cachedMessage);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
if(StringUtils.isBlank(messageRaw) && matched) {
|
||||
message.delete().queue();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFeature() {
|
||||
return UtilityFeatures.LINK_EMBEDS;
|
||||
}
|
||||
}
|
||||
@@ -2,17 +2,18 @@ package dev.sheldan.abstracto.utility.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.listener.ReactedAddedListener;
|
||||
import dev.sheldan.abstracto.core.listener.ReactedRemovedListener;
|
||||
import dev.sheldan.abstracto.core.management.EmoteManagementService;
|
||||
import dev.sheldan.abstracto.core.management.UserManagementService;
|
||||
import dev.sheldan.abstracto.core.models.CachedMessage;
|
||||
import dev.sheldan.abstracto.core.models.CachedReaction;
|
||||
import dev.sheldan.abstracto.core.models.database.AEmote;
|
||||
import dev.sheldan.abstracto.core.models.database.AUser;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.service.Bot;
|
||||
import dev.sheldan.abstracto.core.management.ConfigManagementService;
|
||||
import dev.sheldan.abstracto.core.service.MessageCache;
|
||||
import dev.sheldan.abstracto.core.service.management.ConfigManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.EmoteManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserManagementService;
|
||||
import dev.sheldan.abstracto.core.utils.EmoteUtils;
|
||||
import dev.sheldan.abstracto.utility.config.UtilityFeatures;
|
||||
import dev.sheldan.abstracto.utility.models.StarboardPost;
|
||||
import dev.sheldan.abstracto.utility.service.StarboardService;
|
||||
import dev.sheldan.abstracto.utility.service.management.StarboardPostManagementService;
|
||||
@@ -69,17 +70,18 @@ public class StarboardListener implements ReactedAddedListener, ReactedRemovedLi
|
||||
if(aEmote.isPresent()) {
|
||||
AEmote emote = aEmote.get();
|
||||
MessageReaction.ReactionEmote reactionEmote = addedReaction.getReactionEmote();
|
||||
Optional<Emote> emoteInGuild = bot.getEmote(guildId, emote);
|
||||
Optional<Emote> emoteInGuild = null;
|
||||
emoteInGuild = bot.getEmote(guildId, emote);
|
||||
if(EmoteUtils.isReactionEmoteAEmote(reactionEmote, emote, emoteInGuild.orElse(null))) {
|
||||
Optional<CachedReaction> reactionOptional = EmoteUtils.getReactionFromMessageByEmote(message, emote);
|
||||
updateStarboardPost(message, reactionOptional.orElse(null), userAdding, true);
|
||||
updateStarboardPost(message, reactionOptional.orElse(null), userAdding, true);
|
||||
}
|
||||
} else {
|
||||
log.warn("Emote {} is not defined for guild {}. Starboard not functional.", STAR_EMOTE, guildId);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateStarboardPost(CachedMessage message, CachedReaction reaction, AUserInAServer userReacting, boolean adding) {
|
||||
private void updateStarboardPost(CachedMessage message, CachedReaction reaction, AUserInAServer userReacting, boolean adding) {
|
||||
Optional<StarboardPost> starboardPostOptional = starboardPostManagementService.findByMessageId(message.getMessageId());
|
||||
if(reaction != null) {
|
||||
List<AUser> userExceptAuthor = getUsersExcept(reaction.getUsers(), message.getAuthorId());
|
||||
@@ -99,14 +101,18 @@ public class StarboardListener implements ReactedAddedListener, ReactedRemovedLi
|
||||
starboardService.createStarboardPost(message, userExceptAuthor, userReacting, author);
|
||||
}
|
||||
} else {
|
||||
starboardPostOptional.ifPresent(this::completelyRemoveStarboardPost);
|
||||
if(starboardPostOptional.isPresent()) {
|
||||
this.completelyRemoveStarboardPost(starboardPostOptional.get());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
starboardPostOptional.ifPresent(this::completelyRemoveStarboardPost);
|
||||
if(starboardPostOptional.isPresent()) {
|
||||
this.completelyRemoveStarboardPost(starboardPostOptional.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void completelyRemoveStarboardPost(StarboardPost starboardPost) {
|
||||
private void completelyRemoveStarboardPost(StarboardPost starboardPost) {
|
||||
starboardPostReactorManagementService.removeReactors(starboardPost);
|
||||
starboardService.removeStarboardPost(starboardPost);
|
||||
starboardPostManagementService.removePost(starboardPost);
|
||||
@@ -126,7 +132,7 @@ public class StarboardListener implements ReactedAddedListener, ReactedRemovedLi
|
||||
Optional<Emote> emoteInGuild = bot.getEmote(guildId, emote);
|
||||
if(EmoteUtils.isReactionEmoteAEmote(reactionEmote, emote, emoteInGuild.orElse(null))) {
|
||||
Optional<CachedReaction> reactionOptional = EmoteUtils.getReactionFromMessageByEmote(message, emote);
|
||||
updateStarboardPost(message, reactionOptional.orElse(null), userRemoving, false);
|
||||
updateStarboardPost(message, reactionOptional.orElse(null), userRemoving, false);
|
||||
}
|
||||
} else {
|
||||
log.warn("Emote {} is not defined for guild {}. Starboard not functional.", STAR_EMOTE, guildId);
|
||||
@@ -140,4 +146,9 @@ public class StarboardListener implements ReactedAddedListener, ReactedRemovedLi
|
||||
private List<AUser> getUsersExcept(List<AUser> users, Long userId) {
|
||||
return users.stream().filter(user -> !user.getId().equals(userId)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFeature() {
|
||||
return UtilityFeatures.STARBOARD;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package dev.sheldan.abstracto.utility.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.listener.MessageDeletedListener;
|
||||
import dev.sheldan.abstracto.core.models.CachedMessage;
|
||||
import dev.sheldan.abstracto.utility.config.UtilityFeatures;
|
||||
import dev.sheldan.abstracto.utility.models.StarboardPost;
|
||||
import dev.sheldan.abstracto.utility.service.management.StarboardPostManagementService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -22,4 +23,9 @@ public class StarboardPostDeletedListener implements MessageDeletedListener {
|
||||
starboardPostManagementService.setStarboardPostIgnored(messageBefore.getMessageId(), true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFeature() {
|
||||
return UtilityFeatures.STARBOARD;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package dev.sheldan.abstracto.utility.repository.converter;
|
||||
|
||||
import dev.sheldan.abstracto.core.management.UserManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserManagementService;
|
||||
import dev.sheldan.abstracto.core.models.database.AUser;
|
||||
import dev.sheldan.abstracto.core.service.Bot;
|
||||
import dev.sheldan.abstracto.utility.models.template.starboard.StarStatsUser;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package dev.sheldan.abstracto.utility.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.models.AServerAChannelAUser;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
@@ -15,6 +15,7 @@ import dev.sheldan.abstracto.utility.models.template.ExecutedReminderModel;
|
||||
import dev.sheldan.abstracto.utility.models.template.ReminderModel;
|
||||
import dev.sheldan.abstracto.utility.service.management.ReminderManagementService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -70,9 +71,9 @@ public class RemindServiceBean implements ReminderService {
|
||||
MessageToSend message = templateService.renderEmbedTemplate(REMINDER_EMBED_KEY, reminderModel);
|
||||
String messageText = message.getMessage();
|
||||
if(StringUtils.isBlank(messageText)) {
|
||||
reminderModel.getTextChannel().sendMessage(message.getEmbed()).queue();
|
||||
reminderModel.getMessageChannel().sendMessage(message.getEmbed()).queue();
|
||||
} else {
|
||||
reminderModel.getTextChannel().sendMessage(messageText).embed(message.getEmbed()).queue();
|
||||
reminderModel.getMessageChannel().sendMessage(messageText).embed(message.getEmbed()).queue();
|
||||
}
|
||||
|
||||
if(remindIn.getSeconds() < 60) {
|
||||
@@ -89,24 +90,29 @@ public class RemindServiceBean implements ReminderService {
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void executeReminder(Long reminderId) {
|
||||
public void executeReminder(Long reminderId) {
|
||||
Reminder reminderToRemindFor = reminderManagementService.loadReminder(reminderId);
|
||||
AServer server = reminderToRemindFor.getServer();
|
||||
AChannel channel = reminderToRemindFor.getChannel();
|
||||
Optional<TextChannel> channelToAnswerIn = bot.getTextChannelFromServer(server.getId(), channel.getId());
|
||||
// only send the message if the channel still exists, if not, only set the reminder to reminded.
|
||||
if(channelToAnswerIn.isPresent()) {
|
||||
AUser userReference = reminderToRemindFor.getToBeReminded().getUserReference();
|
||||
Member memberInServer = bot.getMemberInServer(server.getId(), userReference.getId());
|
||||
ExecutedReminderModel build = ExecutedReminderModel
|
||||
.builder()
|
||||
.reminder(reminderToRemindFor)
|
||||
.member(memberInServer)
|
||||
.build();
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate("remind_reminder", build);
|
||||
channelToAnswerIn.get().sendMessage(messageToSend.getMessage()).embed(messageToSend.getEmbed()).queue();
|
||||
Optional<Guild> guildToAnswerIn = bot.getGuildById(server.getId());
|
||||
if(guildToAnswerIn.isPresent()) {
|
||||
Optional<TextChannel> channelToAnswerIn = bot.getTextChannelFromServer(server.getId(), channel.getId());
|
||||
// only send the message if the channel still exists, if not, only set the reminder to reminded.
|
||||
if(channelToAnswerIn.isPresent()) {
|
||||
AUser userReference = reminderToRemindFor.getToBeReminded().getUserReference();
|
||||
Member memberInServer = bot.getMemberInServer(server.getId(), userReference.getId());
|
||||
ExecutedReminderModel build = ExecutedReminderModel
|
||||
.builder()
|
||||
.reminder(reminderToRemindFor)
|
||||
.member(memberInServer)
|
||||
.build();
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate("remind_reminder", build);
|
||||
channelToAnswerIn.get().sendMessage(messageToSend.getMessage()).embed(messageToSend.getEmbed()).queue();
|
||||
} else {
|
||||
log.warn("Channel {} in server {} to remind user did not exist anymore. Ignoring reminder {}", channel.getId(), server.getId(), reminderId);
|
||||
}
|
||||
} else {
|
||||
log.warn("Channel {} in server {} to remind user did not exist anymore. Ignoring.", channel.getId(), server.getId());
|
||||
log.warn("Guild {} to remind user in did not exist anymore. Ignoring reminder {}.", server.getId(), reminderId);
|
||||
}
|
||||
reminderManagementService.setReminded(reminderToRemindFor);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package dev.sheldan.abstracto.utility.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.management.EmoteManagementService;
|
||||
import dev.sheldan.abstracto.core.management.PostTargetManagement;
|
||||
import dev.sheldan.abstracto.core.management.UserManagementService;
|
||||
import dev.sheldan.abstracto.core.exception.ChannelException;
|
||||
import dev.sheldan.abstracto.core.exception.GuildException;
|
||||
import dev.sheldan.abstracto.core.service.management.EmoteManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.PostTargetManagement;
|
||||
import dev.sheldan.abstracto.core.service.management.UserManagementService;
|
||||
import dev.sheldan.abstracto.core.models.AServerChannelMessage;
|
||||
import dev.sheldan.abstracto.core.models.CachedMessage;
|
||||
import dev.sheldan.abstracto.core.models.database.*;
|
||||
@@ -36,6 +38,8 @@ import java.util.stream.Collectors;
|
||||
@Slf4j
|
||||
public class StarboardServiceBean implements StarboardService {
|
||||
|
||||
public static final String STARBOARD_POSTTARGET = "starboard";
|
||||
public static final String STARBOARD_POST_TEMPLATE = "starboard_post";
|
||||
@Autowired
|
||||
private Bot bot;
|
||||
|
||||
@@ -70,11 +74,11 @@ public class StarboardServiceBean implements StarboardService {
|
||||
private EmoteService emoteService;
|
||||
|
||||
@Override
|
||||
public void createStarboardPost(CachedMessage message, List<AUser> userExceptAuthor, AUserInAServer userReacting, AUserInAServer starredUser) {
|
||||
public void createStarboardPost(CachedMessage message, List<AUser> userExceptAuthor, AUserInAServer userReacting, AUserInAServer starredUser) {
|
||||
StarboardPostModel starboardPostModel = buildStarboardPostModel(message, userExceptAuthor.size());
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate("starboard_post", starboardPostModel);
|
||||
PostTarget starboard = postTargetManagement.getPostTarget("starboard", message.getServerId());
|
||||
postTargetService.sendEmbedInPostTarget(messageToSend, "starboard", message.getServerId()).thenAccept(message1 -> {
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(STARBOARD_POST_TEMPLATE, starboardPostModel);
|
||||
PostTarget starboard = postTargetManagement.getPostTarget(STARBOARD_POSTTARGET, message.getServerId());
|
||||
postTargetService.sendEmbedInPostTarget(messageToSend, STARBOARD_POSTTARGET, message.getServerId()).thenAccept(message1 -> {
|
||||
AServerChannelMessage aServerChannelMessage = AServerChannelMessage
|
||||
.builder()
|
||||
.messageId(message1.getIdLong())
|
||||
@@ -90,7 +94,7 @@ public class StarboardServiceBean implements StarboardService {
|
||||
|
||||
}
|
||||
|
||||
private StarboardPostModel buildStarboardPostModel(CachedMessage message, Integer starCount) {
|
||||
private StarboardPostModel buildStarboardPostModel(CachedMessage message, Integer starCount) {
|
||||
Member member = bot.getMemberInServer(message.getServerId(), message.getAuthorId());
|
||||
Optional<TextChannel> channel = bot.getTextChannelFromServer(message.getServerId(), message.getChannelId());
|
||||
Optional<Guild> guild = bot.getGuildById(message.getServerId());
|
||||
@@ -121,7 +125,7 @@ public class StarboardServiceBean implements StarboardService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateStarboardPost(StarboardPost post, CachedMessage message, List<AUser> userExceptAuthor) {
|
||||
public void updateStarboardPost(StarboardPost post, CachedMessage message, List<AUser> userExceptAuthor) {
|
||||
StarboardPostModel starboardPostModel = buildStarboardPostModel(message, userExceptAuthor.size());
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate("starboard_post", starboardPostModel);
|
||||
CompletableFuture<Message> future = new CompletableFuture<>();
|
||||
@@ -132,13 +136,13 @@ public class StarboardServiceBean implements StarboardService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeStarboardPost(StarboardPost message) {
|
||||
public void removeStarboardPost(StarboardPost message) {
|
||||
AChannel starboardChannel = message.getStarboardChannel();
|
||||
bot.deleteMessage(starboardChannel.getServer().getId(), starboardChannel.getId(), message.getStarboardMessageId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public StarStatsModel retrieveStarStats(Long serverId) {
|
||||
public StarStatsModel retrieveStarStats(Long serverId) {
|
||||
int count = 3;
|
||||
List<StarboardPost> starboardPosts = starboardPostManagementService.retrieveTopPosts(serverId, count);
|
||||
List<StarStatsUser> topStarGivers = starboardPostReactorManagementService.retrieveTopStarGiver(serverId, count);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.sheldan.abstracto.utility.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.management.EmoteManagementService;
|
||||
import dev.sheldan.abstracto.core.service.EmoteService;
|
||||
import dev.sheldan.abstracto.core.service.management.EmoteManagementService;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import dev.sheldan.abstracto.core.models.embed.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.service.Bot;
|
||||
@@ -45,6 +46,9 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
@Autowired
|
||||
private EmoteManagementService emoteManagementService;
|
||||
|
||||
@Autowired
|
||||
private EmoteService emoteService;
|
||||
|
||||
@Autowired
|
||||
private MessageService messageService;
|
||||
|
||||
@@ -52,7 +56,7 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
private SuggestionServiceBean self;
|
||||
|
||||
@Override
|
||||
public void createSuggestion(Member member, String text, SuggestionLog suggestionLog) {
|
||||
public void createSuggestion(Member member, String text, SuggestionLog suggestionLog) {
|
||||
Suggestion suggestion = suggestionManagementService.createSuggestion(member, text);
|
||||
suggestionLog.setSuggestion(suggestion);
|
||||
suggestionLog.setText(text);
|
||||
@@ -62,9 +66,9 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
Guild guildById = instance.getGuildById(guildId);
|
||||
if(guildById != null) {
|
||||
postTargetService.sendEmbedInPostTarget(messageToSend, SUGGESTIONS_TARGET, guildId).thenAccept(message -> {
|
||||
suggestionManagementService.setPostedMessage(suggestion, message);
|
||||
messageService.addReactionToMessage(SUGGESTION_YES_EMOTE, guildId, message);
|
||||
messageService.addReactionToMessage(SUGGESTION_NO_EMOTE, guildId, message);
|
||||
suggestionManagementService.setPostedMessage(suggestion, message);
|
||||
});
|
||||
} else {
|
||||
log.warn("Guild {} or member {} was not found when creating suggestion.", member.getGuild().getIdLong(), member.getIdLong());
|
||||
@@ -105,7 +109,7 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void updateSuggestionMessageText(String text, SuggestionLog suggestionLog, Message message) {
|
||||
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();
|
||||
@@ -122,4 +126,11 @@ public class SuggestionServiceBean implements SuggestionService {
|
||||
suggestionManagementService.setSuggestionState(suggestion, SuggestionState.REJECTED);
|
||||
updateSuggestion(text, log, suggestion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validateSetup(Long serverId) {
|
||||
emoteService.throwIfEmoteDoesNotExist(SUGGESTION_YES_EMOTE, serverId);
|
||||
emoteService.throwIfEmoteDoesNotExist(SUGGESTION_NO_EMOTE, serverId);
|
||||
postTargetService.throwIfPostTargetIsNotDefined(SUGGESTION_YES_EMOTE, serverId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package dev.sheldan.abstracto.utility.service.management;
|
||||
|
||||
import dev.sheldan.abstracto.core.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.management.ServerManagementService;
|
||||
import dev.sheldan.abstracto.core.management.UserManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserManagementService;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
|
||||
@@ -14,3 +14,9 @@ abstracto.scheduling.jobs.reminderJob.group=utility
|
||||
abstracto.scheduling.jobs.reminderJob.clazz=dev.sheldan.abstracto.utility.jobs.ReminderJob
|
||||
abstracto.scheduling.jobs.reminderJob.standAlone=false
|
||||
abstracto.scheduling.jobs.reminderJob.active=true
|
||||
|
||||
abstracto.features.starboard=false
|
||||
abstracto.features.reminder=false
|
||||
abstracto.features.suggestion=false
|
||||
abstracto.features.utility=false
|
||||
abstracto.features.embeds=true
|
||||
Reference in New Issue
Block a user