added assignable role place module including: setting up, configuring, deleting commands and documentation

upgraded JDA version to 4.2.0
refactored multiple interfaces to be more convenient/contain more information (reaction added/removed now gets the actual event)
added generic way to check for conditions. these conditions are provided by modules and are loosely connected via condition context and a condition name
added changeable flag to emotes to indicate that they can be updated via setEmote
refactored emote parsing in command parameters, the command parameters will now contain a fake emote
added feature to embed templates for fields to force a new message regardless of the discord limit
added some more functionality to message and channel service regarding field edit/embed sending
introduced the full emote parameter, to have both the emote (if custom) and a fake aemote at hand
refactored some methods to already throw exceptions within the retrieval methods, instead of optionals which need to be dealt outside
changed getEmotes to getEmotesBag to have duplicates of emotes
fixed setEmote to behave correctly with new parameter types
fixed creation of emotes, which previously created additional instances
fixed templating multiple fields handling
refactored command handling to allow async commands, they are the same interface, but configuration dicates whether or not it is async
added generic exception reporting for async commands
refactored a bunch of service methods to be named optional, and the non optional methods throw exceptions in case nothing is found
added a few more customized exceptions
added clearing freemarker internal template cache to clear cache
added feature to skip, not use, embeds if they look to be empty (no fields, no description, no attachment)
added virtual env to gitignore
fixed initial sync of roles un-marking roles as deleted
added some convenience methods to remove reactions from users directly
fixed post command handling in case it is not a templatable instance
fixed exceptions without cause in generic exception model
This commit is contained in:
Sheldan
2020-07-23 02:22:58 +02:00
parent 5317199bf4
commit fd4d784081
201 changed files with 6547 additions and 527 deletions

View File

@@ -15,6 +15,7 @@ import dev.sheldan.abstracto.utility.service.management.MessageEmbedPostManageme
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.MessageReaction;
import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -40,13 +41,13 @@ public class MessageEmbedRemovalReactionListener implements ReactedAddedListener
@Override
public void executeReactionAdded(CachedMessage message, MessageReaction reaction, AUserInAServer userAdding) {
public void executeReactionAdded(CachedMessage message, GuildMessageReactionAddEvent event, AUserInAServer userAdding) {
Long guildId = message.getServerId();
AEmote aEmote = emoteService.getEmoteOrFakeEmote(REMOVAL_EMOTE, guildId);
MessageReaction.ReactionEmote reactionEmote = reaction.getReactionEmote();
AEmote aEmote = emoteService.getEmoteOrDefaultEmote(REMOVAL_EMOTE, guildId);
MessageReaction.ReactionEmote reactionEmote = event.getReactionEmote();
Optional<Emote> emoteInGuild = botService.getEmote(guildId, aEmote);
log.trace("Removing embed in message {} in channel {} in server {} because of a user reaction.", message.getMessageId(), message.getChannelId(), message.getServerId());
if(emoteService.isReactionEmoteAEmote(reactionEmote, aEmote, emoteInGuild.orElse(null))) {
if(emoteService.isReactionEmoteAEmote(reactionEmote, aEmote)) {
Optional<EmbeddedMessage> embeddedMessageOptional = messageEmbedPostManagementService.findEmbeddedPostByMessageId(message.getMessageId());
if(embeddedMessageOptional.isPresent()) {
EmbeddedMessage embeddedMessage = embeddedMessageOptional.get();

View File

@@ -18,8 +18,9 @@ import dev.sheldan.abstracto.utility.service.StarboardService;
import dev.sheldan.abstracto.utility.service.management.StarboardPostManagementService;
import dev.sheldan.abstracto.utility.service.management.StarboardPostReactorManagementService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.MessageReaction;
import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent;
import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionRemoveEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@@ -59,15 +60,14 @@ public class StarboardListener implements ReactedAddedListener, ReactedRemovedLi
@Override
@Transactional
public void executeReactionAdded(CachedMessage message, MessageReaction addedReaction, AUserInAServer userAdding) {
public void executeReactionAdded(CachedMessage message, GuildMessageReactionAddEvent addedReaction, AUserInAServer userAdding) {
if(userAdding.getUserReference().getId().equals(message.getAuthorId())) {
return;
}
Long guildId = message.getServerId();
AEmote aEmote = emoteService.getEmoteOrFakeEmote(STAR_EMOTE, guildId);
AEmote aEmote = emoteService.getEmoteOrDefaultEmote(STAR_EMOTE, guildId);
MessageReaction.ReactionEmote reactionEmote = addedReaction.getReactionEmote();
Optional<Emote> emoteInGuild = botService.getEmote(guildId, aEmote);
if(emoteService.isReactionEmoteAEmote(reactionEmote, aEmote, emoteInGuild.orElse(null))) {
if(emoteService.isReactionEmoteAEmote(reactionEmote, aEmote)) {
log.trace("User {} in server {} reacted with star to put a message {} on starboard.", userAdding.getUserReference().getId(), userAdding.getServerReference().getId(), message.getMessageId());
Optional<CachedReaction> reactionOptional = emoteService.getReactionFromMessageByEmote(message, aEmote);
handleStarboardPostChange(message, reactionOptional.orElse(null), userAdding, true);
@@ -122,15 +122,14 @@ public class StarboardListener implements ReactedAddedListener, ReactedRemovedLi
@Override
@Transactional
public void executeReactionRemoved(CachedMessage message, MessageReaction removedReaction, AUserInAServer userRemoving) {
public void executeReactionRemoved(CachedMessage message, GuildMessageReactionRemoveEvent removedReaction, AUserInAServer userRemoving) {
if(message.getAuthorId().equals(userRemoving.getUserReference().getId())) {
return;
}
Long guildId = message.getServerId();
AEmote aEmote = emoteService.getEmoteOrFakeEmote(STAR_EMOTE, guildId);
AEmote aEmote = emoteService.getEmoteOrDefaultEmote(STAR_EMOTE, guildId);
MessageReaction.ReactionEmote reactionEmote = removedReaction.getReactionEmote();
Optional<Emote> emoteInGuild = botService.getEmote(guildId, aEmote);
if(emoteService.isReactionEmoteAEmote(reactionEmote, aEmote, emoteInGuild.orElse(null))) {
if(emoteService.isReactionEmoteAEmote(reactionEmote, aEmote)) {
log.trace("User {} in server {} removed star reaction from message {} on starboard.",
userRemoving.getUserReference().getId(), userRemoving.getServerReference().getId(), message.getMessageId());
Optional<CachedReaction> reactionOptional = emoteService.getReactionFromMessageByEmote(message, aEmote);

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.template.listener.MessageEmbeddedModel;
import dev.sheldan.abstracto.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
@@ -144,13 +143,12 @@ public class MessageEmbedServiceBean implements MessageEmbedService {
}
private MessageEmbeddedModel buildTemplateParameter(Message message, CachedMessage embeddedMessage) {
Optional<AChannel> channelOpt = channelManagementService.loadChannel(message.getChannel().getIdLong());
AServer server = serverManagementService.loadOrCreate(message.getGuild().getIdLong());
AUserInAServer user = userInServerManagementService.loadUser(message.getMember());
Member author = botService.getMemberInServer(embeddedMessage.getServerId(), embeddedMessage.getAuthorId());
Optional<TextChannel> textChannelFromServer = botService.getTextChannelFromServer(embeddedMessage.getServerId(), embeddedMessage.getChannelId());
Optional<TextChannel> textChannelFromServer = botService.getTextChannelFromServerOptional(embeddedMessage.getServerId(), embeddedMessage.getChannelId());
TextChannel sourceChannel = textChannelFromServer.orElse(null);
AChannel channel = channelOpt.orElseThrow(() -> new ChannelNotFoundException(message.getChannel().getIdLong(), server.getId()));
AChannel channel = channelManagementService.loadChannel(message.getChannel().getIdLong());
return MessageEmbeddedModel
.builder()
.channel(channel)

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.models.AServerAChannelAUser;
@@ -64,7 +63,7 @@ public class RemindServiceBean implements ReminderService {
@Override
public Reminder createReminderInForUser(AUserInAServer user, String remindText, Duration remindIn, Message message) {
AChannel channel = channelManagementService.loadChannel(message.getChannel().getIdLong()).orElseThrow(() -> new ChannelNotFoundException(message.getChannel().getIdLong(), message.getGuild().getIdLong()));
AChannel channel = channelManagementService.loadChannel(message.getChannel().getIdLong());
AServerAChannelAUser aServerAChannelAUser = AServerAChannelAUser
.builder()
.user(user.getUserReference())
@@ -109,7 +108,7 @@ public class RemindServiceBean implements ReminderService {
log.info("Executing reminder {}.", reminderId);
Optional<Guild> guildToAnswerIn = botService.getGuildById(server.getId());
if(guildToAnswerIn.isPresent()) {
Optional<TextChannel> channelToAnswerIn = botService.getTextChannelFromServer(server.getId(), channel.getId());
Optional<TextChannel> channelToAnswerIn = botService.getTextChannelFromServerOptional(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.getRemindedUser().getUserReference();

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.exception.UserInServerNotFoundException;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
@@ -105,7 +104,7 @@ public class StarboardServiceBean implements StarboardService {
public void persistPost(CachedMessage message, List<Long> userExceptAuthorIds, List<CompletableFuture<Message>> completableFutures, Long starboardChannelId, Long starredUserId, Long userReactingId) {
AUserInAServer innerStarredUser = userInServerManagementService.loadUser(starredUserId).orElseThrow(() -> new UserInServerNotFoundException(starredUserId));
try {
AChannel starboardChannel = channelManagementService.loadChannel(starboardChannelId).orElseThrow(() -> new ChannelNotFoundException(starboardChannelId, message.getServerId()));
AChannel starboardChannel = channelManagementService.loadChannel(starboardChannelId);
Message message1 = completableFutures.get(0).get();
AServerAChannelMessage aServerAChannelMessage = AServerAChannelMessage
.builder()
@@ -129,7 +128,7 @@ public class StarboardServiceBean implements StarboardService {
private StarboardPostModel buildStarboardPostModel(CachedMessage message, Integer starCount) {
Member member = botService.getMemberInServer(message.getServerId(), message.getAuthorId());
Optional<TextChannel> channel = botService.getTextChannelFromServer(message.getServerId(), message.getChannelId());
Optional<TextChannel> channel = botService.getTextChannelFromServerOptional(message.getServerId(), message.getChannelId());
Optional<Guild> guild = botService.getGuildById(message.getServerId());
AChannel aChannel = AChannel.builder().id(message.getChannelId()).build();
AUser user = AUser.builder().id(message.getAuthorId()).build();

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service.management;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
@@ -43,8 +42,8 @@ public class MessageEmbedPostManagementServiceBean implements MessageEmbedPostMa
if(!embeddedServer.getId().equals(embeddingServer.getId())) {
throw new CrossServerEmbedException(String.format("Message %s is not from server %s", embeddedMessage.getMessageUrl(), embeddingServer.getId()));
}
AChannel embeddingChannel = channelManagementService.loadChannel(messageContainingEmbed.getChannel().getIdLong()).orElseThrow(() -> new ChannelNotFoundException(messageContainingEmbed.getChannel().getIdLong(), messageContainingEmbed.getGuild().getIdLong()));
AChannel embeddedChannel = channelManagementService.loadChannel(embeddedMessage.getChannelId()).orElseThrow(() -> new ChannelNotFoundException(embeddedMessage.getChannelId(), embeddedMessage.getServerId()));
AChannel embeddingChannel = channelManagementService.loadChannel(messageContainingEmbed.getChannel().getIdLong());
AChannel embeddedChannel = channelManagementService.loadChannel(embeddedMessage.getChannelId());
AUserInAServer embeddedAuthor = userInServerManagementService.loadUser(embeddedMessage.getServerId(), embeddedMessage.getAuthorId());
EmbeddedMessage messageEmbedPost = EmbeddedMessage
.builder()

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service.management;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.database.AChannel;
@@ -28,7 +27,7 @@ public class StarboardPostManagementServiceBean implements StarboardPostManageme
@Override
public StarboardPost createStarboardPost(CachedMessage starredMessage, AUserInAServer starredUser, AServerAChannelMessage starboardPost) {
AChannel build = channelManagementService.loadChannel(starredMessage.getChannelId()).orElseThrow(() -> new ChannelNotFoundException(starredMessage.getChannelId(), starredMessage.getServerId()));
AChannel build = channelManagementService.loadChannel(starredMessage.getChannelId());
StarboardPost post = StarboardPost
.builder()
.author(starredUser)

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service.management;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
@@ -60,12 +59,8 @@ public class SuggestionManagementServiceBean implements SuggestionManagementServ
@Override
public void setPostedMessage(Suggestion suggestion, Message message) {
long channelId = message.getChannel().getIdLong();
Optional<AChannel> channelOptional = channelManagementService.loadChannel(channelId);
if(channelOptional.isPresent()) {
suggestion.setChannel(channelOptional.get());
} else {
throw new ChannelNotFoundException(channelId, suggestion.getServer().getId());
}
AChannel channel = channelManagementService.loadChannel(channelId);
suggestion.setChannel(channel);
suggestion.setMessageId(message.getIdLong());
suggestionRepository.save(suggestion);
}

View File

@@ -12,6 +12,7 @@ import dev.sheldan.abstracto.utility.models.database.EmbeddedMessage;
import dev.sheldan.abstracto.utility.service.management.MessageEmbedPostManagementService;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.MessageReaction;
import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
@@ -42,7 +43,7 @@ public class MessageEmbedRemovalReactionListenerTest {
private EmoteService emoteService;
@Mock
private MessageReaction messageReaction;
private GuildMessageReactionAddEvent messageReaction;
@Mock
private MessageReaction.ReactionEmote reactionEmote;
@@ -100,10 +101,10 @@ public class MessageEmbedRemovalReactionListenerTest {
.channelId(channelId)
.build();
AEmote reactedEmote = AEmote.builder().build();
when(emoteService.getEmoteOrFakeEmote(MessageEmbedRemovalReactionListener.REMOVAL_EMOTE, serverId)).thenReturn(reactedEmote);
when(emoteService.getEmoteOrDefaultEmote(MessageEmbedRemovalReactionListener.REMOVAL_EMOTE, serverId)).thenReturn(reactedEmote);
when(messageReaction.getReactionEmote()).thenReturn(reactionEmote);
when(botService.getEmote(serverId, reactedEmote)).thenReturn(Optional.of(emote));
when(emoteService.isReactionEmoteAEmote(reactionEmote, reactedEmote, emote)).thenReturn(true);
when(emoteService.isReactionEmoteAEmote(reactionEmote, reactedEmote)).thenReturn(true);
EmbeddedMessage message = EmbeddedMessage
.builder()
.embeddingUser(embeddingUser)
@@ -131,10 +132,10 @@ public class MessageEmbedRemovalReactionListenerTest {
.build();
AUserInAServer userInAServer = MockUtils.getUserObject(5L, server);
AEmote reactedEmote = AEmote.builder().build();
when(emoteService.getEmoteOrFakeEmote(MessageEmbedRemovalReactionListener.REMOVAL_EMOTE, serverId)).thenReturn(reactedEmote);
when(emoteService.getEmoteOrDefaultEmote(MessageEmbedRemovalReactionListener.REMOVAL_EMOTE, serverId)).thenReturn(reactedEmote);
when(messageReaction.getReactionEmote()).thenReturn(reactionEmote);
when(botService.getEmote(serverId, reactedEmote)).thenReturn(Optional.of(emote));
when(emoteService.isReactionEmoteAEmote(reactionEmote, reactedEmote, emote)).thenReturn(wasCorrectEmote);
when(emoteService.isReactionEmoteAEmote(reactionEmote, reactedEmote)).thenReturn(wasCorrectEmote);
testUnit.executeReactionAdded(cachedMessage, messageReaction, userInAServer);
verify(messageService, times(0)).deleteMessageInChannelInServer(serverId, channelId, messageId);
}

View File

@@ -16,6 +16,8 @@ import dev.sheldan.abstracto.utility.service.StarboardService;
import dev.sheldan.abstracto.utility.service.management.StarboardPostManagementService;
import dev.sheldan.abstracto.utility.service.management.StarboardPostReactorManagementService;
import net.dv8tion.jda.api.entities.MessageReaction;
import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent;
import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionRemoveEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
@@ -57,7 +59,10 @@ public class StarboardListenerTest {
private EmoteService emoteService;
@Mock
private MessageReaction messageReaction;
private GuildMessageReactionAddEvent addEvent;
@Mock
private GuildMessageReactionRemoveEvent removeEvent;
@Mock
private MessageReaction.ReactionEmote reactionEmote;
@@ -72,8 +77,8 @@ public class StarboardListenerTest {
.serverId(serverId)
.build();
AUserInAServer userAdding = MockUtils.getUserObject(authorId, MockUtils.getServer(serverId));
testUnit.executeReactionAdded(cachedMessage, messageReaction, userAdding);
verify(emoteService, times(0)).getEmoteOrFakeEmote(StarboardListener.STAR_EMOTE, serverId);
testUnit.executeReactionAdded(cachedMessage, addEvent, userAdding);
verify(emoteService, times(0)).getEmoteOrDefaultEmote(StarboardListener.STAR_EMOTE, serverId);
}
@Test
@@ -84,8 +89,8 @@ public class StarboardListenerTest {
AEmote starEmote = AEmote.builder().build();
AUserInAServer userAdding = MockUtils.getUserObject(reactionUserId, MockUtils.getServer(serverId));
CachedMessage cachedMessage = setupWrongEmote(serverId, authorId, starEmote);
testUnit.executeReactionAdded(cachedMessage, messageReaction, userAdding);
verify(emoteService, times(1)).getEmoteOrFakeEmote(StarboardListener.STAR_EMOTE, serverId);
testUnit.executeReactionAdded(cachedMessage, addEvent, userAdding);
verify(emoteService, times(1)).getEmoteOrDefaultEmote(StarboardListener.STAR_EMOTE, serverId);
verify(emoteService, times(0)).getReactionFromMessageByEmote(any(CachedMessage.class), eq(starEmote));
}
@@ -144,8 +149,8 @@ public class StarboardListenerTest {
.serverId(serverId)
.build();
AUserInAServer userAdding = MockUtils.getUserObject(authorId, MockUtils.getServer(serverId));
testUnit.executeReactionRemoved(cachedMessage, messageReaction, userAdding);
verify(emoteService, times(0)).getEmoteOrFakeEmote(StarboardListener.STAR_EMOTE, serverId);
testUnit.executeReactionRemoved(cachedMessage, removeEvent, userAdding);
verify(emoteService, times(0)).getEmoteOrDefaultEmote(StarboardListener.STAR_EMOTE, serverId);
}
@Test
@@ -156,8 +161,8 @@ public class StarboardListenerTest {
AEmote starEmote = AEmote.builder().build();
AUserInAServer userAdding = MockUtils.getUserObject(reactionUserId, MockUtils.getServer(serverId));
CachedMessage cachedMessage = setupWrongEmote(serverId, authorId, starEmote);
testUnit.executeReactionRemoved(cachedMessage, messageReaction, userAdding);
verify(emoteService, times(1)).getEmoteOrFakeEmote(StarboardListener.STAR_EMOTE, serverId);
testUnit.executeReactionRemoved(cachedMessage, removeEvent, userAdding);
verify(emoteService, times(1)).getEmoteOrDefaultEmote(StarboardListener.STAR_EMOTE, serverId);
verify(emoteService, times(0)).getReactionFromMessageByEmote(any(CachedMessage.class), eq(starEmote));
}
@@ -235,19 +240,18 @@ public class StarboardListenerTest {
.serverId(serverId)
.messageId(messageId)
.build();
when(messageReaction.getReactionEmote()).thenReturn(reactionEmote);
when(removeEvent.getReactionEmote()).thenReturn(reactionEmote);
AEmote starEmote = AEmote.builder().build();
when(emoteService.getEmoteOrFakeEmote(StarboardListener.STAR_EMOTE, serverId)).thenReturn(starEmote);
when(botService.getEmote(serverId, starEmote)).thenReturn(Optional.empty());
when(emoteService.isReactionEmoteAEmote(reactionEmote, starEmote, null)).thenReturn(true);
when(emoteService.getEmoteOrDefaultEmote(StarboardListener.STAR_EMOTE, serverId)).thenReturn(starEmote);
when(emoteService.isReactionEmoteAEmote(reactionEmote, starEmote)).thenReturn(true);
CachedReaction reaction = CachedReaction.builder().userInServersIds(remainingUsers).build();
when(emoteService.getReactionFromMessageByEmote(cachedMessage, starEmote)).thenReturn(Optional.of(reaction));
when(starboardPostManagementService.findByMessageId(messageId)).thenReturn(Optional.ofNullable(post));
when(userInServerManagementService.loadUser(serverId, author.getUserReference().getId())).thenReturn(author);
when(userInServerManagementService.loadUser(remainingUser.getUserReference().getId())).thenReturn(Optional.of(remainingUser));
when(configManagementService.loadConfig(serverId, StarboardListener.FIRST_LEVEL_THRESHOLD_KEY)).thenReturn(AConfig.builder().longValue(requiredStars).build());
testUnit.executeReactionRemoved(cachedMessage, messageReaction, userRemoving);
verify(emoteService, times(1)).getEmoteOrFakeEmote(StarboardListener.STAR_EMOTE, serverId);
testUnit.executeReactionRemoved(cachedMessage, removeEvent, userRemoving);
verify(emoteService, times(1)).getEmoteOrDefaultEmote(StarboardListener.STAR_EMOTE, serverId);
verify(emoteService, times(1)).getReactionFromMessageByEmote(cachedMessage, starEmote);
}
@@ -260,19 +264,18 @@ public class StarboardListenerTest {
.serverId(serverId)
.messageId(messageId)
.build();
when(messageReaction.getReactionEmote()).thenReturn(reactionEmote);
when(addEvent.getReactionEmote()).thenReturn(reactionEmote);
AEmote starEmote = AEmote.builder().build();
when(emoteService.getEmoteOrFakeEmote(StarboardListener.STAR_EMOTE, serverId)).thenReturn(starEmote);
when(botService.getEmote(serverId, starEmote)).thenReturn(Optional.empty());
when(emoteService.isReactionEmoteAEmote(reactionEmote, starEmote, null)).thenReturn(true);
when(emoteService.getEmoteOrDefaultEmote(StarboardListener.STAR_EMOTE, serverId)).thenReturn(starEmote);
when(emoteService.isReactionEmoteAEmote(reactionEmote, starEmote)).thenReturn(true);
CachedReaction reaction = CachedReaction.builder().userInServersIds(Arrays.asList(userAdding.getUserReference().getId())).build();
when(emoteService.getReactionFromMessageByEmote(cachedMessage, starEmote)).thenReturn(Optional.of(reaction));
when(starboardPostManagementService.findByMessageId(messageId)).thenReturn(Optional.ofNullable(existingPost));
when(userInServerManagementService.loadUser(serverId, author.getUserReference().getId())).thenReturn(author);
when(userInServerManagementService.loadUser(userAdding.getUserReference().getId())).thenReturn(Optional.of(userAdding));
when(configManagementService.loadConfig(serverId, StarboardListener.FIRST_LEVEL_THRESHOLD_KEY)).thenReturn(AConfig.builder().longValue(requiredStars).build());
testUnit.executeReactionAdded(cachedMessage, messageReaction, userAdding);
verify(emoteService, times(1)).getEmoteOrFakeEmote(StarboardListener.STAR_EMOTE, serverId);
testUnit.executeReactionAdded(cachedMessage, addEvent, userAdding);
verify(emoteService, times(1)).getEmoteOrDefaultEmote(StarboardListener.STAR_EMOTE, serverId);
verify(emoteService, times(1)).getReactionFromMessageByEmote(cachedMessage, starEmote);
}
@@ -282,10 +285,9 @@ public class StarboardListenerTest {
.authorId(authorId)
.serverId(serverId)
.build();
when(messageReaction.getReactionEmote()).thenReturn(reactionEmote);
when(emoteService.getEmoteOrFakeEmote(StarboardListener.STAR_EMOTE, serverId)).thenReturn(starEmote);
when(botService.getEmote(serverId, starEmote)).thenReturn(Optional.empty());
when(emoteService.isReactionEmoteAEmote(reactionEmote, starEmote, null)).thenReturn(false);
when(addEvent.getReactionEmote()).thenReturn(reactionEmote);
when(emoteService.getEmoteOrDefaultEmote(StarboardListener.STAR_EMOTE, serverId)).thenReturn(starEmote);
when(emoteService.isReactionEmoteAEmote(reactionEmote, starEmote)).thenReturn(false);
return cachedMessage;
}
}

View File

@@ -203,10 +203,10 @@ public class MessageEmbedServiceBeanTest {
when(embeddingMessage.getChannel()).thenReturn(textChannel);
when(userInServerManagementService.loadUser(embeddingMember)).thenReturn(embeddingUser);
when(userInServerManagementService.loadUser(userEmbeddingUserInServerId)).thenReturn(Optional.of(embeddingUser));
when(channelManagementService.loadChannel(channelId)).thenReturn(Optional.of(aChannel));
when(channelManagementService.loadChannel(channelId)).thenReturn(aChannel);
when(serverManagementService.loadOrCreate(serverId)).thenReturn(server);
when(botService.getMemberInServer(cachedMessage.getServerId(), cachedMessage.getAuthorId())).thenReturn(author);
when(botService.getTextChannelFromServer(cachedMessage.getServerId(), cachedMessage.getChannelId())).thenReturn(Optional.of(textChannel));
when(botService.getTextChannelFromServerOptional(cachedMessage.getServerId(), cachedMessage.getChannelId())).thenReturn(Optional.of(textChannel));
MessageToSend messageToSend = MessageToSend.builder().build();
when(templateService.renderEmbedTemplate(eq(MessageEmbedServiceBean.MESSAGE_EMBED_TEMPLATE), any(MessageEmbeddedModel.class))).thenReturn(messageToSend);
Message messageContainingEmbed = Mockito.mock(Message.class);

View File

@@ -87,7 +87,7 @@ public class RemindServiceBeanTest {
AChannel aChannel = MockUtils.getTextChannel(server, 5L);
String remindText = "text";
Duration duration = Duration.ofSeconds(62);
when(channelManagementService.loadChannel(channel.getIdLong())).thenReturn(Optional.of(aChannel));
when(channelManagementService.loadChannel(channel.getIdLong())).thenReturn(aChannel);
Long reminderId = 5L;
Reminder createdReminder = Reminder.builder().targetDate(Instant.now().plus(duration)).text(remindText).id(reminderId).build();
Long messageId = 5L;
@@ -109,7 +109,7 @@ public class RemindServiceBeanTest {
AChannel aChannel = MockUtils.getTextChannel(server, 5L);
String remindText = "text";
Duration duration = Duration.ofSeconds(50);
when(channelManagementService.loadChannel(channel.getIdLong())).thenReturn(Optional.of(aChannel));
when(channelManagementService.loadChannel(channel.getIdLong())).thenReturn(aChannel);
Long reminderId = 5L;
Reminder createdReminder = Reminder.builder().targetDate(Instant.now().plus(duration)).text(remindText).id(reminderId).build();
Long messageId = 5L;
@@ -130,7 +130,7 @@ public class RemindServiceBeanTest {
when(reminderManagementService.loadReminder(reminderId)).thenReturn(Optional.of(remindedReminder));
Guild guildMock = Mockito.mock(Guild.class);
when(botService.getGuildById(server.getId())).thenReturn(Optional.of(guildMock));
when(botService.getTextChannelFromServer(server.getId(), aChannel.getId())).thenReturn(Optional.of(channel));
when(botService.getTextChannelFromServerOptional(server.getId(), aChannel.getId())).thenReturn(Optional.of(channel));
Member mockedMember = Mockito.mock(Member.class);
when(botService.getMemberInServer(server.getId(), remindedUser.getUserReference().getId())).thenReturn(mockedMember);
MessageToSend messageToSend = MessageToSend.builder().build();
@@ -149,7 +149,7 @@ public class RemindServiceBeanTest {
when(reminderManagementService.loadReminder(reminderId)).thenReturn(Optional.of(remindedReminder));
Guild guildMock = Mockito.mock(Guild.class);
when(botService.getGuildById(server.getId())).thenReturn(Optional.of(guildMock));
when(botService.getTextChannelFromServer(server.getId(), aChannel.getId())).thenReturn(Optional.empty());
when(botService.getTextChannelFromServerOptional(server.getId(), aChannel.getId())).thenReturn(Optional.empty());
testUnit.executeReminder(reminderId);
verify(reminderManagementService, times(1)).setReminded(remindedReminder);
}

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.exception.UserInServerNotFoundException;
import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
@@ -117,7 +116,7 @@ public class StarboardServiceBeanTest {
.build();
Member authorMember = Mockito.mock(Member.class);
when(botService.getMemberInServer(message.getServerId(), message.getAuthorId())).thenReturn(authorMember);
when(botService.getTextChannelFromServer(server.getId(), channelId)).thenReturn(Optional.of(mockedTextChannel));
when(botService.getTextChannelFromServerOptional(server.getId(), channelId)).thenReturn(Optional.of(mockedTextChannel));
when(botService.getGuildById(server.getId())).thenReturn(Optional.of(guild));
MessageToSend postMessage = MessageToSend.builder().build();
when(templateService.renderEmbedTemplate(eq(StarboardServiceBean.STARBOARD_POST_TEMPLATE), starboardPostModelArgumentCaptor.capture())).thenReturn(postMessage);
@@ -157,7 +156,7 @@ public class StarboardServiceBeanTest {
when(userInServerManagementService.loadUser(starredUser.getUserInServerId())).thenReturn(Optional.of(starredUser));
when(userInServerManagementService.loadUser(userReacting.getUserInServerId())).thenReturn(Optional.of(userReacting));
AChannel channel = MockUtils.getTextChannel(server, channelId);
when(channelManagementService.loadChannel(channelId)).thenReturn(Optional.of(channel));
when(channelManagementService.loadChannel(channelId)).thenReturn(channel);
StarboardPost post = StarboardPost.builder().build();
when(starboardPostManagementService.createStarboardPost(eq(message), eq(starredUser), any(AServerAChannelMessage.class))).thenReturn(post);
AUserInAServer secondStarrerUserObj = MockUtils.getUserObject(secondStarrerUserId, server);
@@ -223,17 +222,6 @@ public class StarboardServiceBeanTest {
executeLoadErrorTest(server, userReacting, starredUser, 10L);
}
@Test(expected = ChannelNotFoundException.class)
public void testPersistingOfNotFoundChannel() {
AServer server = MockUtils.getServer();
AUserInAServer userReacting = MockUtils.getUserObject(4L, server);
AUserInAServer starredUser = MockUtils.getUserObject(5L, server);
when(userInServerManagementService.loadUser(starredUser.getUserInServerId())).thenReturn(Optional.of(starredUser));
Long channelId = 10L;
when(channelManagementService.loadChannel(channelId)).thenReturn(Optional.empty());
executeLoadErrorTest(server, userReacting, starredUser, channelId);
}
@Test
public void testRetrieveStarStats() {
AServer server = MockUtils.getServer();

View File

@@ -52,7 +52,7 @@ public class MessageEmbedPostManagementServiceBeanTest {
AUserInAServer embeddedUser = MockUtils.getUserObject(7L, server);
AChannel channel = MockUtils.getTextChannel(server, 8L);
when(serverManagementService.loadOrCreate(server.getId())).thenReturn(server);
when(channelManagementService.loadChannel(channel.getId())).thenReturn(Optional.of(channel));
when(channelManagementService.loadChannel(channel.getId())).thenReturn(channel);
Long embeddedMessageId = 5L;
Long embeddingMessageId = 7L;
CachedMessage cachedMessage = CachedMessage

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service.management;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.database.AChannel;
@@ -57,7 +56,7 @@ public class StarboardPostManagementServiceBeanTest {
.channel(starboardChannel)
.messageId(starboardPostId)
.build();
when(channelManagementService.loadChannel(starredMessage.getChannelId())).thenReturn(Optional.ofNullable(sourceChannel));
when(channelManagementService.loadChannel(starredMessage.getChannelId())).thenReturn(sourceChannel);
StarboardPost createdStarboardPost = testUnit.createStarboardPost(starredMessage, userInAServer, postInStarboard);
verify(repository, times(1)).save(createdStarboardPost);
Assert.assertEquals(postInStarboard.getChannel().getId(), createdStarboardPost.getStarboardChannel().getId());
@@ -69,30 +68,6 @@ public class StarboardPostManagementServiceBeanTest {
Assert.assertFalse(createdStarboardPost.isIgnored());
}
@Test(expected = ChannelNotFoundException.class)
public void testCreateStarboardPostForNonExistingChannel() {
AServer server = MockUtils.getServer();
AUserInAServer userInAServer = MockUtils.getUserObject(7L, server);
AChannel sourceChannel = MockUtils.getTextChannel(server, 9L);
AChannel starboardChannel = MockUtils.getTextChannel(server, 10L);
Long starboardPostId = 5L;
Long starredMessageId = 8L;
CachedMessage starredMessage = CachedMessage
.builder()
.channelId(sourceChannel.getId())
.messageId(starredMessageId)
.serverId(server.getId())
.build();
AServerAChannelMessage postInStarboard = AServerAChannelMessage
.builder()
.server(server)
.channel(starboardChannel)
.messageId(starboardPostId)
.build();
when(channelManagementService.loadChannel(starredMessage.getChannelId())).thenReturn(Optional.empty());
testUnit.createStarboardPost(starredMessage, userInAServer, postInStarboard);
}
@Test
public void setStarboardMessageId(){
StarboardPost post = StarboardPost

View File

@@ -1,6 +1,5 @@
package dev.sheldan.abstracto.utility.service.management;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
@@ -99,26 +98,13 @@ public class SuggestionManagementServiceBeanTest {
when(channel.getIdLong()).thenReturn(channelId);
when(message.getIdLong()).thenReturn(messageId);
AChannel aChannel = AChannel.builder().id(channelId).build();
when(channelManagementService.loadChannel(channelId)).thenReturn(Optional.of(aChannel));
when(channelManagementService.loadChannel(channelId)).thenReturn(aChannel);
testUnit.setPostedMessage(suggestion, message);
Assert.assertEquals(messageId, suggestion.getMessageId());
Assert.assertEquals(channelId, suggestion.getChannel().getId());
verify(suggestionRepository, times(1)).save(suggestion);
}
@Test(expected = ChannelNotFoundException.class)
public void testSetPostedMessageChannelNotFound() {
Long channelId = 6L;
AServer server = MockUtils.getServer();
Suggestion suggestion = Suggestion.builder().server(server).build();
Message message = Mockito.mock(Message.class);
MessageChannel channel = Mockito.mock(MessageChannel.class);
when(message.getChannel()).thenReturn(channel);
when(channel.getIdLong()).thenReturn(channelId);
when(channelManagementService.loadChannel(channelId)).thenReturn(Optional.empty());
testUnit.setPostedMessage(suggestion, message);
}
@Test
public void setSuggestionState() {
Suggestion suggestion = Suggestion.builder().build();