[AB-138] improving logging at various places fixing various issues regarding async commands and exception handling, fixing role role calculation being done twice

This commit is contained in:
Sheldan
2020-10-07 09:29:56 +02:00
parent a391381ff6
commit 0145e7670d
165 changed files with 1129 additions and 513 deletions

View File

@@ -15,7 +15,6 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Component
public class SetAssignableRolePosition extends AbstractConditionableCommand {
@@ -24,14 +23,14 @@ public class SetAssignableRolePosition extends AbstractConditionableCommand {
private AssignableRolePlaceService service;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
public CommandResult execute(CommandContext commandContext) {
checkParameters(commandContext);
List<Object> parameters = commandContext.getParameters().getParameters();
String name = (String) parameters.get(0);
FullEmote emote = (FullEmote) parameters.get(1);
Integer newPosition = (Integer) parameters.get(2);
return service.setEmoteToPosition(commandContext.getUserInitiatedContext().getServer(), name, emote, newPosition)
.thenApply(aVoid -> CommandResult.fromSuccess());
service.setEmoteToPosition(commandContext.getUserInitiatedContext().getServer(), name, emote, newPosition);
return CommandResult.fromSuccess();
}
@Override
@@ -46,7 +45,6 @@ public class SetAssignableRolePosition extends AbstractConditionableCommand {
.module(AssignableRoleModule.ASSIGNABLE_ROLES)
.templated(true)
.causesReaction(true)
.async(true)
.supportsEmbedException(true)
.parameters(parameters)
.help(helpInfo)

View File

@@ -71,6 +71,7 @@ public class AssignablePostReactionAdded implements ReactedAddedListener {
}
MessageReaction.ReactionEmote reactionEmote = event.getReactionEmote();
if(assignablePlacePost.getAssignablePlace().getActive()) {
log.info("User {} added reaction to assignable role place {} in server {}. Handling added event.", userAdding.getUserReference().getId(), assignablePlacePost.getId(), event.getGuild().getId());
addAppropriateRoles(event, reaction, assignablePlacePost, reactionEmote, userAdding);
} else {
reaction.removeReaction(event.getUser()).submit();
@@ -84,13 +85,16 @@ public class AssignablePostReactionAdded implements ReactedAddedListener {
AssignableRolePlace assignableRolePlace = assignablePlacePost.getAssignablePlace();
List<CompletableFuture<Void>> futures = new ArrayList<>();
for (AssignableRole assignableRole : assignablePlacePost.getAssignableRoles()) {
log.trace("Checking emote {} if it was reaction for assignable role place.", assignableRole.getEmote().getId());
if (emoteService.isReactionEmoteAEmote(reactionEmote, assignableRole.getEmote())) {
if(assignableRolePlace.getUniqueRoles()) {
log.trace("Assignable role place {} has unique roles configured. Removing existing reactions and roles.", assignableRolePlace.getId());
Optional<AssignedRoleUser> byUserInServer = assignedRoleUserManagementService.findByUserInServerOptional(userAdding);
byUserInServer.ifPresent(user -> futures.add(assignableRolePlaceService.removeExistingReactionsAndRoles(assignableRolePlace, user)));
}
Long assignableRoleId = assignableRole.getId();
log.info("User added {} reaction {} and gets assignable role {} in server {}.", userAdding.getUserReference().getId(), assignableRole.getEmote().getId(), assignableRoleId, userAdding.getServerReference().getId());
CompletableFuture<Void> roleAdditionFuture = assignableRoleServiceBean.assignAssignableRoleToUser(assignableRoleId, event.getMember());
futures.add(CompletableFuture.allOf(roleAdditionFuture));
@@ -99,6 +103,7 @@ public class AssignablePostReactionAdded implements ReactedAddedListener {
}
}
if(!validReaction) {
log.trace("Reaction was not found in the configuration of assignable role place {}, removing reaction.", assignableRolePlace.getId());
futures.add(reaction.removeReaction(event.getUser()).submit());
}
Long assignableRolePlaceId = assignableRolePlace.getId();
@@ -112,9 +117,11 @@ public class AssignablePostReactionAdded implements ReactedAddedListener {
AssignableRolePlace place = assignableRolePlaceManagementService.findByPlaceId(assignableRolePlaceId);
AUserInAServer userInAServer = userInServerManagementService.loadUser(userAdding);
if(place.getUniqueRoles()) {
log.trace("Assignable role place {} has unique roles. Deleting all existing references.", assignableRolePlaceId);
assignableRoleServiceBean.clearAllRolesOfUserInPlace(place, userInAServer);
}
AssignableRole role = assignableRoleManagementService.getRoleForReactionEmote(reactionEmote, place);
log.info("Adding role to assignable role {} to user {} in server {}.", role.getId(), userInAServer.getUserReference().getId(), userInAServer.getServerReference().getId());
assignableRoleServiceBean.addRoleToUser(role.getId(), userInAServer);
}

View File

@@ -49,6 +49,8 @@ public class AssignablePostReactionRemoved implements ReactedRemovedListener {
assignablePlacePost.getAssignableRoles().forEach(assignableRole -> {
if(emoteService.isReactionEmoteAEmote(reactionEmote, assignableRole.getEmote())) {
Long assignableRoleId = assignableRole.getId();
log.info("Removing assignable role {} for user {} in server {} from assignable role place {}.", assignableRoleId,
userRemoving.getUserReference().getId(), userRemoving.getServerReference().getId(), assignablePlacePost.getAssignablePlace().getId());
assignableRoleService.fullyRemoveAssignableRoleFromUser(assignableRole, event.getMember()).exceptionally(throwable -> {
log.error("Failed to remove assignable role {} from user {}.", assignableRoleId, event.getMember(), throwable);
return null;

View File

@@ -109,7 +109,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
@Override
public CompletableFuture<Void> setEmoteToPosition(AServer server, String placeName, FullEmote emote, Integer position) {
public void setEmoteToPosition(AServer server, String placeName, FullEmote emote, Integer position) {
if(isPositionUsed(server, placeName, position)) {
throw new AbstractoTemplatedException("Position is already used", "assignable_role_place_position_exists_exception");
}
@@ -118,12 +118,15 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
Integer emoteId = emote.getFakeEmote().getId();
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByServerAndKey(server, placeName);
log.info("Setting emote {} to position {} in assignable role place {} in server {}.",
emoteId, position, assignableRolePlace.getId(), assignableRolePlace.getServer().getId());
Optional<AssignableRole> emoteOptional = assignableRolePlace.getAssignableRoles().stream().filter(role -> role.getEmote().getId().equals(emoteId)).findFirst();
if(emoteOptional.isPresent()) {
AssignableRole toChange = emoteOptional.get();
toChange.setPosition(position);
} else {
throw new EmoteNotInAssignableRolePlaceException(emote, placeName);
}
throw new EmoteNotInAssignableRolePlaceException(emote, placeName);
}
@Override
@@ -145,6 +148,8 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
boolean emoteUsable = true;
if(fakeEmote.getEmote() != null) {
// it only may be unusable if its a custom emote
log.trace("Using custom emote {} to create assignable role {} for assignable role place {} in server {}.",
fakeEmote.getEmote().getId(), roleId, placeId, serverId);
emoteUsable = emoteService.isEmoteUsableByBot(fakeEmote.getEmote()) && fakeEmote.getEmote().isAvailable();
}
if(emoteUsable) {
@@ -152,13 +157,17 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
existingMessagePosts.sort(Comparator.comparingLong(AssignableRolePlacePost::getId));
if(!assignableRolePlace.getMessagePosts().isEmpty()){
log.trace("There are already message posts on for the assignable role place {}.", assignableRolePlace.getId());
AssignableRolePlacePost latestPost = existingMessagePosts.get(assignableRolePlace.getMessagePosts().size() - 1);
AssignablePostMessage model = prepareAssignablePostMessageModel(assignableRolePlace);
boolean forceNewMessage = latestPost.getAssignableRoles().size() >= 20;
log.info("We need to add a new message post {} for assignable role place {} in server {} in channel {}.",
forceNewMessage, placeId, serverId, assignableRolePlace.getChannel().getId());
AssignablePostRole newAssignableRole = AssignablePostRole
.builder()
.description(description)
.emote(fakeEmote)
.forceNewMessage(latestPost.getAssignableRoles().size() >= 20)
.forceNewMessage(forceNewMessage)
.build();
model.getRoles().add(newAssignableRole);
MessageToSend messageToSend = templateService.renderEmbedTemplate(ASSIGNABLE_ROLES_POST_TEMPLATE_KEY, model);
@@ -167,15 +176,19 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
if(channelOptional.isPresent()) {
TextChannel textChannel = channelOptional.get();
if(latestPost.getAssignableRoles().size() < 20) {
return addReactionToExistingAssignableRolePlacePost(fakeEmote, description, assignableRolePlace, placeId, roleId, serverId, latestPost, messageToSend, textChannel);
log.trace("Adding reaction to existing post {} in channel {} in server {} for assignable role place {}.",
latestPost.getId(), assignableRolePlace.getChannel().getId(), serverId, placeId);
return addReactionToExistingAssignableRolePlacePost(fakeEmote, description, roleId, latestPost, messageToSend, textChannel);
} else {
return addNewMessageToAssignableRolePlace(placeName, fakeEmote, description, roleId, serverId, messageToSend, textChannel);
log.trace("Adding new post to assignable role place {} in channel {} in server {}.",
placeId, assignableRolePlace.getChannel().getId(), server.getId());
return addNewMessageToAssignableRolePlace(placeId, fakeEmote, description, roleId, serverId, messageToSend, textChannel);
}
} else {
throw new ChannelNotFoundException(latestPost.getUsedChannel().getId());
}
} else {
log.info("Added emote to assignable place {} in server {}, but no message post yet.", placeName, serverId);
log.trace("Added emote to assignable place {} in server {}, but no message post yet.", placeId, serverId);
self.addAssignableRoleInstanceWithoutPost(placeId, roleId, fakeEmote, description, serverId);
}
} else {
@@ -184,41 +197,50 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
return CompletableFuture.completedFuture(null);
}
private CompletableFuture<Void> addReactionToExistingAssignableRolePlacePost(FullEmote fakeEmote, String description, AssignableRolePlace assignableRolePlace, Long placeId, Long roleId, Long serverId, AssignableRolePlacePost latestPost, MessageToSend messageToSend, TextChannel textChannel) {
private CompletableFuture<Void> addReactionToExistingAssignableRolePlacePost(FullEmote fakeEmote, String description, Long roleId, AssignableRolePlacePost latestPost, MessageToSend messageToSend, TextChannel textChannel) {
// TODO maybe refactor to use the same message object, so we dont need to retrieve it twice and do in parallel
Long serverId = latestPost.getAssignablePlace().getServer().getId();
Long placeId = latestPost.getAssignablePlace().getId();
Long latestPostId = latestPost.getId();
int messagePostSize = latestPost.getAssignablePlace().getMessagePosts().size();
return textChannel.retrieveMessageById(latestPost.getId()).submit()
.thenCompose(message -> messageService.addReactionToMessageWithFuture(fakeEmote.getFakeEmote(), serverId, message))
.thenCompose(aVoid -> {
MessageEmbed embedToUse = messageToSend.getEmbeds().get(assignableRolePlace.getMessagePosts().size() - 1);
return channelService.editEmbedMessageInAChannel(embedToUse, textChannel, latestPost.getId());
})
.thenCompose(message -> {
self.addAssignableRoleInstanceWithPost(message.getIdLong(), placeId, roleId, description, fakeEmote, serverId);
return CompletableFuture.completedFuture(null);
log.trace("Adding reaction to message {} in server {} for assignable role place {}.", message.getId(), serverId, placeId);
return messageService.addReactionToMessageWithFuture(fakeEmote.getFakeEmote(), serverId, message);
}).thenCompose(aVoid -> {
log.trace("Editing embed for assignable role place post {} in assignable role place {} in server {}.", latestPostId, placeId, serverId);
MessageEmbed embedToUse = messageToSend.getEmbeds().get(messagePostSize - 1);
return channelService.editEmbedMessageInAChannel(embedToUse, textChannel, latestPostId);
}).thenAccept(message ->
self.addAssignableRoleInstanceWithPost(message.getIdLong(), placeId, roleId, description, fakeEmote, serverId)
);
}
private CompletableFuture<Void> addNewMessageToAssignableRolePlace(Long placeId, FullEmote fakeEmote, String description, Long roleId, Long serverId, MessageToSend messageToSend, TextChannel textChannel) {
MessageEmbed embedToUse = messageToSend.getEmbeds().get(messageToSend.getEmbeds().size() - 1);
return channelService.sendEmbedToChannel(embedToUse, textChannel)
.thenCompose(message -> {
log.trace("Adding reaction for role {} to newly created message {} for assignable role place {} in server {}.", roleId, message.getId(), placeId, serverId);
return messageService.addReactionToMessageWithFuture(fakeEmote.getFakeEmote(), serverId, message)
.thenAccept(aVoid ->
self.addNewlyCreatedAssignablePlacePost(placeId, description, roleId, serverId, message, fakeEmote)
);
});
}
private CompletableFuture<Void> addNewMessageToAssignableRolePlace(String placeName, FullEmote fakeEmote, String description, Long roleId, Long serverId, MessageToSend messageToSend, TextChannel textChannel) {
MessageEmbed embedToUse = messageToSend.getEmbeds().get(messageToSend.getEmbeds().size() - 1);
return channelService.sendEmbedToChannel(embedToUse, textChannel)
.thenCompose(message -> messageService.addReactionToMessageWithFuture(fakeEmote.getFakeEmote(), serverId, message).thenAccept(aVoid ->
self.addNewlyCreatedAssignablePlacePost(placeName, description, roleId, serverId, textChannel, message, fakeEmote)
));
}
@Transactional
public void addNewlyCreatedAssignablePlacePost(String placeName, String description,Long roleId, Long serverId, TextChannel textChannel, Message message, FullEmote fakeEmote) {
AChannel loadedChannel = channelManagementService.loadChannel(textChannel.getIdLong());
AServer loadedServer = serverManagementService.loadOrCreate(serverId);
public void addNewlyCreatedAssignablePlacePost(Long placeId, String description,Long roleId, Long serverId, Message message, FullEmote fakeEmote) {
log.info("Storing newly created assignable role place post {} for place {} in server {}.", message.getId(), placeId, serverId);
ARole role = roleManagementService.findRole(roleId);
AssignableRolePlace loadedPlace = rolePlaceManagementService.findByServerAndKey(loadedServer, placeName);
AssignableRolePlace loadedPlace = rolePlaceManagementService.findByPlaceId(placeId);
AEmote emote = emoteManagementService.createEmote(null, fakeEmote.getFakeEmote(), serverId, false);
emote.setChangeable(false);
log.trace("Setting emote {} to not changeable, because it is part of an assignable role place {} in server {}.", emote.getId(), placeId, serverId);
AssignableRolePlacePost newPost = AssignableRolePlacePost
.builder()
.id(message.getIdLong())
.usedChannel(loadedChannel)
.usedChannel(loadedPlace.getChannel())
.assignablePlace(loadedPlace)
.build();
@@ -228,25 +250,32 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
@Transactional
public void addAssignableRoleInstanceWithPost(Long messageId, Long placeId, Long roleId, String description, FullEmote fakeEmote, Long serverId) {
log.info("Storing newly created assignable role {} to post {} to assignable role place {} in server {}.", roleId, messageId, placeId, serverId);
AEmote emote = emoteManagementService.createEmote(null, fakeEmote.getFakeEmote(), serverId, false);
emote.setChangeable(false);
log.trace("Setting emote {} to not changeable, because it is part of an assignable role place {} in server {}.", emote.getId(), placeId, serverId);
assignableRoleManagementServiceBean.addRoleToPlace(placeId, emote.getId(), roleId, description, messageId);
}
@Transactional
public void addAssignableRoleInstanceWithoutPost(Long placeId, Long roleId, FullEmote fakeEmote, String description, Long serverId) {
log.info("Storing newly created assignable role {} without post to assignable role place {} in server {}.", roleId, placeId, serverId);
AEmote emote = emoteManagementService.createEmote(null, fakeEmote.getFakeEmote(), serverId, false);
emote.setChangeable(false);
log.trace("Setting emote {} to not changeable, because it is part of an assignable role place {} in server {}.", emote.getId(), placeId, serverId);
assignableRoleManagementServiceBean.addRoleToPlace(placeId, emote.getId(), roleId, description);
}
@Override
public CompletableFuture<Void> removeRoleFromAssignableRolePlace(AServer server, String placeName, FullEmote emote) {
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByServerAndKey(server, placeName);
Long assignableRolePlaceId = assignableRolePlace.getId();
for (AssignableRole assignableRole : assignableRolePlace.getAssignableRoles()) {
if(emoteService.compareAEmote(assignableRole.getEmote(), emote.getFakeEmote())) {
log.info("Removing assignable role {} identified by emote {} from assignable role place {} in server {}.",
assignableRole.getId(), assignableRole.getEmote().getId(), assignableRolePlace.getId(), assignableRolePlace.getServer().getId());
return removeRoleFromAssignablePlace(assignableRole, assignableRolePlace).thenAccept(aVoid ->
self.deleteAssignableRoleFromPlace(server.getId(), placeName, assignableRole.getId())
self.deleteAssignableRoleFromPlace(assignableRolePlaceId, assignableRole.getId())
);
}
}
@@ -254,9 +283,9 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
@Transactional
public void deleteAssignableRoleFromPlace(Long serverId, String placeName, Long assignableRoleId) {
AServer server = serverManagementService.loadOrCreate(serverId);
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByServerAndKey(server, placeName);
public void deleteAssignableRoleFromPlace(Long placeId, Long assignableRoleId) {
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByPlaceId(placeId);
log.info("Deleting the entry for assignable role {} in assignable role place {}.", assignableRoleId, placeId);
Optional<AssignableRole> roleToRemoveOptional = assignableRolePlace.getAssignableRoles().stream().filter(role -> role.getId().equals(assignableRoleId)).findAny();
roleToRemoveOptional.ifPresent(assignableRole -> {
assignableRolePlace.getAssignableRoles().remove(assignableRole);
@@ -272,11 +301,13 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
List<AssignableRole> assignableRoles = assignableRolePlace.getAssignableRoles();
assignableRoles.sort(Comparator.comparing(AssignableRole::getPosition));
Long messageId = post.getId();
log.trace("Removing field describing assignable role {} in assignable role place {} from post {}.", role.getId(), assignableRolePlace.getId(), messageId);
CompletableFuture<Message> fieldEditing = channelService.removeFieldFromMessage(textChannel, messageId, assignableRoles.indexOf(role));
log.trace("Clearing reaction for emote {} on assignable role post {} in assignable role place {}.", role.getEmote().getId(), messageId, assignableRolePlace.getId());
CompletableFuture<Void> reactionRemoval = messageService.clearReactionFromMessageWithFuture(role.getEmote(), assignableRolePlace.getServer().getId(), role.getAssignableRolePlacePost().getUsedChannel().getId(), role.getAssignableRolePlacePost().getId());
return CompletableFuture.allOf(fieldEditing, reactionRemoval);
} else {
// this case comes from the situation in which, the emote was deleted ant he initial post setup failed
// this case comes from the situation in which, the emote was deleted and he initial post setup failed
log.warn("Reaction {} to remove does not have a post attached. The post needs to be setup again, it is most likely not functioning currently anyway.", role.getEmote().getEmoteId());
return CompletableFuture.completedFuture(null);
}
@@ -285,14 +316,16 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
@Override
public CompletableFuture<Void> setupAssignableRolePlace(AServer server, String name) {
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByServerAndKey(server, name);
log.info("Setting up assignable role place {} in server {} towards channel {}.", assignableRolePlace.getId(), server.getId(), assignableRolePlace.getChannel().getId());
List<CompletableFuture<Void>> oldPostDeletionFutures = deleteExistingMessagePostsForPlace(assignableRolePlace);
assignableRolePlace.getMessagePosts().clear();
assignableRolePlace.getAssignableRoles().forEach(assignableRole ->
assignableRole.setAssignableRolePlacePost(null)
);
Long serverId = server.getId();
Long assignablePlaceId = assignableRolePlace.getId();
return CompletableFuture.allOf(oldPostDeletionFutures.toArray(new CompletableFuture[0]))
.thenCompose(aVoid -> self.createAssignableRolePlacePosts(serverId, name));
.thenCompose(aVoid -> self.createAssignableRolePlacePosts(serverId, assignablePlaceId));
}
@Override
@@ -303,6 +336,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
@Override
public CompletableFuture<Void> refreshAssignablePlacePosts(AssignableRolePlace place) {
log.info("Refreshing assignable role place posts for assignable role place {} in server {}.", place.getId(), place.getServer().getId());
MessageToSend messageToSend = renderAssignablePlacePosts(place);
List<AssignableRolePlacePost> existingMessagePosts = place.getMessagePosts();
existingMessagePosts.sort(Comparator.comparingLong(AssignableRolePlacePost::getId));
@@ -313,6 +347,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
TextChannel textChannel = channelOptional.get();
Iterator<MessageEmbed> iterator = messageToSend.getEmbeds().iterator();
place.getMessagePosts().forEach(post -> {
log.trace("Refreshing the posts for message post {} in channel {} in assignable role place {} in server {}.", post.getId(), textChannel.getId(), place.getId(), place.getServer().getId());
CompletableFuture<Message> messageCompletableFuture = channelService.editEmbedMessageInAChannel(iterator.next(), textChannel, post.getId());
futures.add(messageCompletableFuture);
});
@@ -326,12 +361,14 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
List<AssignableRolePlacePost> existingMessagePosts = place.getMessagePosts();
if(!existingMessagePosts.isEmpty()) {
MessageToSend renderedMessage = renderAssignablePlacePosts(place);
log.trace("There are {} current posts known for the assignable role place {}.", existingMessagePosts.size(), place.getId());
existingMessagePosts.sort(Comparator.comparingLong(AssignableRolePlacePost::getId));
AssignableRolePlacePost latestPost = existingMessagePosts.get(0);
Long channelId = latestPost.getUsedChannel().getId();
AssignableRolePlacePost firstPost = existingMessagePosts.get(0);
Long channelId = firstPost.getUsedChannel().getId();
Optional<TextChannel> channelOptional = channelService.getTextChannelInGuild(place.getServer().getId(), channelId);
if(channelOptional.isPresent()) {
return channelService.editEmbedMessageInAChannel(renderedMessage.getEmbeds().get(0), channelOptional.get(), latestPost.getId()).thenCompose(message -> CompletableFuture.completedFuture(null));
log.info("Refreshing text for assignable role place {} in channel {} in post {}.", place.getId(), channelId, firstPost.getId());
return channelService.editEmbedMessageInAChannel(renderedMessage.getEmbeds().get(0), channelOptional.get(), firstPost.getId()).thenCompose(message -> CompletableFuture.completedFuture(null));
}
throw new ChannelNotFoundException(channelId);
}
@@ -350,9 +387,11 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
private List<CompletableFuture<Void>> deleteExistingMessagePostsForPlace(AssignableRolePlace assignableRolePlace) {
List<CompletableFuture<Void>> oldPostDeletionFutures = new ArrayList<>();
assignableRolePlace.getMessagePosts().forEach(assignableRolePlacePost ->
oldPostDeletionFutures.add(messageService.deleteMessageInChannelInServer(assignableRolePlace.getServer().getId(), assignableRolePlacePost.getUsedChannel().getId(), assignableRolePlacePost.getId()))
);
assignableRolePlace.getMessagePosts().forEach(assignableRolePlacePost -> {
log.info("Deleting existing message post with id {} in channel{} for assignable role place {} in server {}.",
assignableRolePlacePost.getId(), assignableRolePlacePost.getUsedChannel().getId(), assignableRolePlace.getId(), assignableRolePlace.getServer().getId());
oldPostDeletionFutures.add(messageService.deleteMessageInChannelInServer(assignableRolePlace.getServer().getId(), assignableRolePlacePost.getUsedChannel().getId(), assignableRolePlacePost.getId()));
});
return oldPostDeletionFutures;
}
@@ -484,6 +523,8 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
if(firstEmoteOptional.isPresent() && secondEmoteOptional.isPresent()) {
AssignableRole firstRole = firstEmoteOptional.get();
AssignableRole secondRole = secondEmoteOptional.get();
log.info("Swapping positions of emotes {} and {} in assignable role place {} in server {}: first: {} -> {}, second: {} -> {}",
firstRole.getEmote().getId(), secondRole.getEmote().getId(), place.getId(), server.getId(), firstRole.getPosition(), secondRole.getPosition(), secondRole.getPosition(), firstRole.getPosition());
int firstPosition = firstRole.getPosition();
firstRole.setPosition(secondRole.getPosition());
secondRole.setPosition(firstPosition);
@@ -500,6 +541,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
public CompletableFuture<Void> testAssignableRolePlace(AServer server, String name, MessageChannel channel) {
AssignableRolePlace place = rolePlaceManagementService.findByServerAndKey(server, name);
MessageToSend messageToSend = renderAssignablePlacePosts(place);
log.info("Testing assignable role place {} in channel {} on server {}.", place.getId(), channel.getId(), server.getId());
List<CompletableFuture<Message>> completableFutures = channelService.sendMessageToSendToChannel(messageToSend, channel);
return CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0]));
}
@@ -509,6 +551,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
AssignableRolePlace place = rolePlaceManagementService.findByServerAndKey(server, name);
List<AssignablePostConfigRole> roles = new ArrayList<>();
Guild guild = botService.getGuildByIdNullable(server.getId());
log.info("Showing assignable role place config for place {} in channel {} on server {}.", place.getId(), channel.getId(), server.getId());
List<AssignableRole> assignableRoles = place.getAssignableRoles().stream().sorted(Comparator.comparingInt(AssignableRole::getPosition)).collect(Collectors.toList());
for (AssignableRole role : assignableRoles) {
AEmote emoteForRole = role.getEmote();
@@ -521,6 +564,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
.position(role.getPosition())
.awardedRole(jdaRole)
.build();
log.trace("Displaying config for role {} with emote {} in position {}.", role.getId(), emoteForRole.getId(), role.getPosition());
roles.add(postRole);
}
AssignableRolePlaceConfig configModel = AssignableRolePlaceConfig
@@ -554,6 +598,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
@Override
public CompletableFuture<Void> changeText(AServer server, String name, String newText) {
AssignableRolePlace place = rolePlaceManagementService.findByServerAndKey(server, name);
log.info("Changing text of assignable role place {} in server {}.", place.getId(), server.getId());
place.setText(newText);
return refreshTextFromPlace(place);
}
@@ -562,10 +607,13 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
public CompletableFuture<Void> removeExistingReactionsAndRoles(AssignableRolePlace place, AssignedRoleUser user) {
Member memberInServer = botService.getMemberInServer(user.getUser());
List<CompletableFuture<Void>> futures = new ArrayList<>();
log.info("Removing all existing reactions and roles by user {} on assignable role place {} in server {}.", user.getId(), place.getId(), user.getUser().getServerReference().getId());
user.getRoles().forEach(assignableRole -> {
futures.add(roleService.removeAssignableRoleFromUser(assignableRole, memberInServer));
log.trace("Removing role {} from user {} in server {} because of assignable role clearing.", assignableRole.getRole().getId(), memberInServer.getId(), place.getServer().getId());
AEmote emoteToUseObject = emoteManagementService.loadEmote(assignableRole.getEmote().getId());
AssignableRolePlacePost assignablePlacePost = assignableRole.getAssignableRolePlacePost();
log.trace("Removing reaction with emote {} from user {} in server {} because of assignable role clearing.", emoteToUseObject.getId(), user.getUser().getUserReference().getId(), place.getServer().getId());
futures.add(messageService.removeReactionOfUserFromMessageWithFuture(emoteToUseObject, place.getServer().getId(),
assignablePlacePost.getUsedChannel().getId(), assignablePlacePost.getId(), memberInServer));
});
@@ -599,6 +647,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
public CompletableFuture<Void> showAllAssignableRolePlaces(AServer server, MessageChannel channel) {
List<AssignableRolePlace> assignableRolePlaces = rolePlaceManagementService.findAllByServer(server);
AssignablePlaceOverview overViewModel = AssignablePlaceOverview.builder().places(assignableRolePlaces).build();
log.info("Showing overview over all assignable role places for server {} in channel {}.", server.getId(), channel.getId());
List<CompletableFuture<Message>> promises = channelService.sendEmbedTemplateInChannel(ASSIGNABLE_ROLE_PLACES_OVERVIEW_TEMPLATE_KEY, overViewModel, channel);
return CompletableFuture.allOf(promises.toArray(new CompletableFuture[0]));
}
@@ -608,6 +657,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
private void deleteEmotesFromAssignableRolePlace(AssignableRolePlace place) {
log.info("Deleting all emotes associated with assignable role place {} in server {}.", place.getId(), place.getServer().getId());
place.getAssignableRoles().forEach(role ->
emoteManagementService.deleteEmote(role.getEmote())
);
@@ -652,6 +702,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
current = rolesToAddIterator.next();
}
} else if(startOfNewMessage && lastAddedRole != null) {
log.trace("Forcing new message for post of assignable role place {}.", place.getId());
lastAddedRole.setForceNewMessage(true);
}
}
@@ -665,29 +716,30 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
@Transactional
public CompletableFuture<Void> createAssignableRolePlacePosts(Long serverId, String name) {
AServer server = serverManagementService.loadOrCreate(serverId);
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByServerAndKey(server, name);
public CompletableFuture<Void> createAssignableRolePlacePosts(Long serverId, Long assignablePlaceId) {
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByPlaceId(assignablePlaceId);
Optional<TextChannel> channelOptional = botService.getTextChannelFromServerOptional(serverId, assignableRolePlace.getChannel().getId());
if(channelOptional.isPresent()) {
MessageChannel channel = channelOptional.get();
log.info("Sending assignable role place posts for place {} in channel {} in server {}.", assignableRolePlace.getId(), channel.getId(), serverId);
List<CompletableFuture<Message>> messageFutures = sendAssignablePostMessages(assignableRolePlace, channel);
return CompletableFuture.allOf(messageFutures.toArray(new CompletableFuture[0]))
.thenCompose(aVoid -> self.addEmotes(messageFutures, name));
.thenCompose(aVoid -> self.addEmotes(messageFutures, assignablePlaceId));
} else {
log.warn("Channel to create assignable role post in does not exist.");
throw new AssignableRolePlaceChannelDoesNotExist(assignableRolePlace.getChannel().getId(), name);
throw new AssignableRolePlaceChannelDoesNotExist(assignableRolePlace.getChannel().getId(), assignableRolePlace.getKey());
}
}
@Transactional
public CompletableFuture<Void> addEmotes(List<CompletableFuture<Message>> assignablePlacePostsMessageFutures, String placeKey) {
public CompletableFuture<Void> addEmotes(List<CompletableFuture<Message>> assignablePlacePostsMessageFutures, Long assignablePlaceId) {
Message firstMessage = assignablePlacePostsMessageFutures.get(0).join();
Long serverId = firstMessage.getGuild().getIdLong();
AServer innerServer = serverManagementService.loadOrCreate(serverId);
AssignableRolePlace innerRolePlace = rolePlaceManagementService.findByServerAndKey(innerServer, placeKey);
AssignableRolePlace innerRolePlace = rolePlaceManagementService.findByPlaceId(assignablePlaceId);
log.info("Adding emotes to assignable role place {}.", innerRolePlace);
log.trace("We have {} posts and {} roles.", assignablePlacePostsMessageFutures.size(), innerRolePlace.getAssignableRoles().size());
List<AssignableRole> roleStream = innerRolePlace.getAssignableRoles().stream().sorted(Comparator.comparingInt(AssignableRole::getPosition)).collect(Collectors.toList());
List<CompletableFuture<Void>> reactionFutures = new ArrayList<>();
@@ -698,20 +750,21 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
MessageEmbed embed = sentMessage.getEmbeds().get(0);
List<AssignableRole> firstRoles = roleStream.subList(usedEmotes, usedEmotes + embed.getFields().size());
usedEmotes += embed.getFields().size();
log.trace("Adding {} emotes to message {} for place {}. In total {} were added.", embed.getFields().size(), sentMessage.getId(), innerRolePlace.getId(), usedEmotes);
List<Integer> usedEmoteIds = firstRoles.stream().map(assignableRole -> assignableRole.getEmote().getId()).collect(Collectors.toList());
CompletableFuture<Void> firstMessageFuture = createAssignableRolePlacePost(sentMessage, serverId, usedEmoteIds);
CompletableFuture<Void> firstMessageFuture = addingReactionsToAssignableRolePlacePost(sentMessage, serverId, usedEmoteIds);
reactionFutures.add(firstMessageFuture);
}
return CompletableFuture.allOf(reactionFutures.toArray(new CompletableFuture[0])).thenCompose(aVoid -> {
self.storeCreatedAssignableRolePlacePosts(placeKey, serverId, assignablePlacePostsMessageFutures);
self.storeCreatedAssignableRolePlacePosts(assignablePlaceId, serverId, assignablePlacePostsMessageFutures);
return CompletableFuture.completedFuture(null);
});
}
@Transactional
public void storeCreatedAssignableRolePlacePosts(String name, Long serverId, List<CompletableFuture<Message>> futures) {
AServer server = serverManagementService.loadOrCreate(serverId);
AssignableRolePlace updatedPlace = rolePlaceManagementService.findByServerAndKey(server, name);
public void storeCreatedAssignableRolePlacePosts(Long assignablePlaceId, Long serverId, List<CompletableFuture<Message>> futures) {
AssignableRolePlace updatedPlace = rolePlaceManagementService.findByPlaceId(assignablePlaceId);
log.info("Storing {} messages for assignable role place {} in server {}.", futures.size(), assignablePlaceId, serverId);
List<AssignableRole> rolesToAdd = updatedPlace.getAssignableRoles().stream().sorted(Comparator.comparingInt(AssignableRole::getPosition)).collect(Collectors.toList());
int usedEmotes = 0;
for (int i = 0; i < futures.size(); i++) {
@@ -721,6 +774,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
Message sentMessage = messageCompletableFuture.get();
// this uses the actual embed count as a limit, so this relies on fields to be used for description, if this changes, this needs to be changed
MessageEmbed embed = sentMessage.getEmbeds().get(0);
log.trace("Storing post {} with {} fields.", message.getId(), embed.getFields().size());
List<AssignableRole> firstRoles = rolesToAdd.subList(usedEmotes, usedEmotes + embed.getFields().size());
usedEmotes += embed.getFields().size();
AssignableRolePlacePost post = AssignableRolePlacePost
@@ -740,9 +794,10 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
@Transactional
public CompletableFuture<Void> createAssignableRolePlacePost(Message message, Long server, List<Integer> emotesToAdd) {
public CompletableFuture<Void> addingReactionsToAssignableRolePlacePost(Message message, Long server, List<Integer> emotesToAdd) {
// TODO might need to guarantee the order
List<CompletableFuture<Void>> futures = new ArrayList<>();
log.info("Adding {} emotes to assignable role place post {} in server {}.", emotesToAdd.size(), message.getId(), server);
emotesToAdd.forEach(emotesToUse -> {
AEmote emoteToUseObject = emoteManagementService.loadEmote(emotesToUse);
futures.add(messageService.addReactionToMessageWithFuture(emoteToUseObject, server, message));

View File

@@ -9,6 +9,7 @@ import dev.sheldan.abstracto.assignableroles.service.management.AssignedRoleUser
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.RoleService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -17,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.concurrent.CompletableFuture;
@Component
@Slf4j
public class AssignableRoleServiceBean implements AssignableRoleService {
@Autowired
@@ -40,12 +42,15 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
@Override
public CompletableFuture<Void> assignAssignableRoleToUser(Long assignableRoleId, Member toAdd) {
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);
log.info("Assigning role {} to member {} in server {}.", assignableRoleId, toAdd.getId(), toAdd.getGuild().getId());
return roleService.addRoleToMemberFuture(toAdd, role.getRole());
}
@Override
public void clearAllRolesOfUserInPlace(AssignableRolePlace place, AUserInAServer userInAServer) {
AssignedRoleUser user = assignedRoleUserManagementServiceBean.findByUserInServer(userInAServer);
log.info("Clearing all {} assignable roles in place {} for user {} in server {}.",
user.getRoles().size(), place.getId(), userInAServer.getUserReference().getId(), userInAServer.getServerReference().getId());
user.getRoles().forEach(assignableRole -> {
if(assignableRole.getAssignablePlace().equals(place)) {
assignableRole.getAssignedUsers().remove(user);
@@ -63,6 +68,7 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
@Override
public CompletableFuture<Void> removeAssignableRoleFromUser(AssignableRole assignableRole, Member member) {
log.info("Removing assignable role {} from user {} in server {}.", assignableRole.getId(), member.getId(), member.getGuild().getId());
return roleService.removeRoleFromMemberFuture(member, assignableRole.getRole());
}
@@ -82,11 +88,15 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
@Override
public void addRoleToUser(AssignableRole assignableRole, AUserInAServer aUserInAServer) {
log.info("Persisting storing adding assignable role {} to user {} in server {}.",
assignableRole.getId(), aUserInAServer.getUserReference().getId(), aUserInAServer.getServerReference().getId());
assignedRoleUserManagementServiceBean.addAssignedRoleToUser(assignableRole, aUserInAServer);
}
@Override
public void removeRoleFromUser(AssignableRole assignableRole, AUserInAServer aUserInAServer) {
log.info("Persisting storing removing assignable role {} to user {} in server {}.",
assignableRole.getId(), aUserInAServer.getUserReference().getId(), aUserInAServer.getServerReference().getId());
assignedRoleUserManagementServiceBean.removeAssignedRoleFromUser(assignableRole, aUserInAServer);
}
@@ -94,7 +104,7 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
public void addRoleToUser(Long assignableRoleId, Member member) {
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);
AUserInAServer aUserInAServer = userInServerManagementService.loadUser(member);
assignedRoleUserManagementServiceBean.addAssignedRoleToUser(role, aUserInAServer);
addRoleToUser(role, aUserInAServer);
}
@Transactional
@@ -107,6 +117,6 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
public void removeRoleFromUser(Long assignableRoleId, Member member) {
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);
AUserInAServer aUserInAServer = userInServerManagementService.loadUser(member);
assignedRoleUserManagementServiceBean.removeAssignedRoleFromUser(role, aUserInAServer);
removeRoleFromUser(role, aUserInAServer);
}
}

View File

@@ -11,11 +11,13 @@ import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.service.EmoteService;
import dev.sheldan.abstracto.core.service.management.EmoteManagementService;
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.MessageReaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class AssignableRoleManagementServiceBean implements AssignableRoleManagementService {
@Autowired
@@ -53,6 +55,7 @@ public class AssignableRoleManagementServiceBean implements AssignableRoleManage
.assignableRolePlacePost(post)
.build();
place.getAssignableRoles().add(roleToAdd);
log.info("Adding role {} to assignable role place {}. There are now {} roles.", role.getId(), place.getId(), place.getAssignableRoles().size());
return roleToAdd;
}

View File

@@ -5,6 +5,7 @@ import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace
import dev.sheldan.abstracto.assignableroles.repository.AssignableRolePlaceRepository;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -12,6 +13,7 @@ import java.util.List;
import java.util.Optional;
@Component
@Slf4j
public class AssignableRolePlaceManagementServiceBean implements AssignableRolePlaceManagementService {
@Autowired
@@ -27,6 +29,7 @@ public class AssignableRolePlaceManagementServiceBean implements AssignableRoleP
.key(name)
.build();
repository.save(place);
log.info("Creating assignable role place in channel {} on server {}.", channel.getId(), server.getId());
return place;
}
@@ -54,17 +57,21 @@ public class AssignableRolePlaceManagementServiceBean implements AssignableRoleP
@Override
public void moveAssignableRolePlace(AServer server, String name, AChannel newChannel) {
AssignableRolePlace assignablePlaceToChange = findByServerAndKey(server, name);
log.info("Moving assignable role place {} in server {} from channel {} to channel {}.",
assignablePlaceToChange.getId(), server.getId(), assignablePlaceToChange.getChannel().getId(), newChannel.getId());
assignablePlaceToChange.setChannel(newChannel);
}
@Override
public void changeAssignableRolePlaceDescription(AServer server, String name, String newDescription) {
AssignableRolePlace assignablePlaceToChange = findByServerAndKey(server, name);
log.info("Changing description of assignable role place {} in server {}.", assignablePlaceToChange.getId(), server.getId());
assignablePlaceToChange.setText(newDescription);
}
@Override
public void deleteAssignablePlace(AssignableRolePlace toDelete) {
log.info("Deleting assignable role place {} in server {} which was in server {}.", toDelete.getId(), toDelete.getChannel().getId(), toDelete.getServer().getId());
repository.delete(toDelete);
}

View File

@@ -5,12 +5,14 @@ import dev.sheldan.abstracto.assignableroles.models.database.AssignableRole;
import dev.sheldan.abstracto.assignableroles.models.database.AssignedRoleUser;
import dev.sheldan.abstracto.assignableroles.repository.AssignedRoleUserRepository;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
@Slf4j
public class AssignedRoleUserManagementServiceBean implements AssignedRoleUserManagementService {
@Autowired
@@ -19,6 +21,9 @@ public class AssignedRoleUserManagementServiceBean implements AssignedRoleUserMa
@Override
public void addAssignedRoleToUser(AssignableRole assignableRole, AUserInAServer aUserInAServer) {
Optional<AssignedRoleUser> optional = findByUserInServerOptional(aUserInAServer);
log.info("Adding assignable role {} to user {} in server {} because of assignable role place {}.",
assignableRole.getId(), aUserInAServer.getUserReference().getId(), aUserInAServer.getServerReference().getId(),
assignableRole.getAssignablePlace().getId());
AssignedRoleUser user = optional.orElseGet(() -> createAssignedRoleUser(aUserInAServer));
assignableRole.getAssignedUsers().add(user);
user.getRoles().add(assignableRole);
@@ -26,6 +31,9 @@ public class AssignedRoleUserManagementServiceBean implements AssignedRoleUserMa
@Override
public void removeAssignedRoleFromUser(AssignableRole assignableRole, AUserInAServer aUserInAServer) {
log.info("Removing assignable role {} from user {} in server {} in assignable role place {}.",
assignableRole.getId(), aUserInAServer.getUserReference().getId(), aUserInAServer.getServerReference().getId(),
assignableRole.getAssignablePlace().getId());
AssignedRoleUser user = findByUserInServer(aUserInAServer);
assignableRole.getAssignedUsers().remove(user);
user.getRoles().remove(assignableRole);
@@ -33,6 +41,7 @@ public class AssignedRoleUserManagementServiceBean implements AssignedRoleUserMa
@Override
public AssignedRoleUser createAssignedRoleUser(AUserInAServer aUserInAServer) {
log.info("Creating assigned role user for user {} in server {}.", aUserInAServer.getUserReference().getId(), aUserInAServer.getServerReference().getId());
AssignedRoleUser newUser = AssignedRoleUser.builder().user(aUserInAServer).id(aUserInAServer.getUserInServerId()).build();
return repository.save(newUser);
}
@@ -40,6 +49,7 @@ public class AssignedRoleUserManagementServiceBean implements AssignedRoleUserMa
@Override
public void clearAllAssignedRolesOfUser(AUserInAServer userInAServer) {
AssignedRoleUser user = findByUserInServer(userInAServer);
log.info("Clearing all assignable roles for user {} in server {}.", userInAServer.getUserReference().getId(), userInAServer.getServerReference().getId());
user.getRoles().forEach(assignableRole ->
assignableRole.getAssignedUsers().remove(user)
);