[AB-60] improving java doc for assignable role module

This commit is contained in:
Sheldan
2021-03-03 23:51:26 +01:00
parent 373dfac001
commit 9c6333281b
65 changed files with 1357 additions and 210 deletions

View File

@@ -17,6 +17,9 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* Command used to activate an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class ActivateAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -23,6 +23,10 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to add an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole assignableRole}
* to an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class AddRoleToAssignableRolePost extends AbstractConditionableCommand {

View File

@@ -20,6 +20,9 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to change one attribute of an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
@Slf4j
public class ChangeAssignableRolePlaceConfig extends AbstractConditionableCommand {

View File

@@ -13,7 +13,6 @@ import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import net.dv8tion.jda.api.entities.TextChannel;
@@ -42,8 +41,7 @@ public class CreateAssignableRolePost extends AbstractConditionableCommand {
TextChannel channel = (TextChannel) parameters.get(1);
String text = (String) parameters.get(2);
AChannel chosenChannel = channelManagementService.loadChannel(channel.getIdLong());
AServer server = serverManagementService.loadServer(commandContext.getGuild());
service.createAssignableRolePlace(server, name, chosenChannel, text);
service.createAssignableRolePlace(name, chosenChannel, text);
return CommandResult.fromSuccess();
}

View File

@@ -17,6 +17,9 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* Command used to deactive an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class DeactivateAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -18,6 +18,9 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to delete an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class DeleteAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -21,7 +21,9 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to change the text of an {@link AssignableRolePlace place}
*/
@Component
public class EditAssignableRolePlaceText extends AbstractConditionableCommand {
@@ -37,7 +39,7 @@ public class EditAssignableRolePlaceText extends AbstractConditionableCommand {
String name = (String) parameters.get(0);
String newText = (String) parameters.get(1);
AServer server = serverManagementService.loadServer(commandContext.getGuild());
return service.changeText(server, name, newText)
return service.changeTextAsync(server, name, newText)
.thenApply(aVoid -> CommandResult.fromSuccess());
}

View File

@@ -9,7 +9,6 @@ import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import net.dv8tion.jda.api.entities.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
@@ -18,6 +17,10 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* Command used to move an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
* to another {@link dev.sheldan.abstracto.core.models.database.AChannel channel}
*/
@Component
public class MoveAssignableRolePlace extends AbstractConditionableCommand {
@@ -32,8 +35,7 @@ public class MoveAssignableRolePlace extends AbstractConditionableCommand {
List<Object> parameters = commandContext.getParameters().getParameters();
String name = (String) parameters.get(0);
TextChannel newChannel = (TextChannel) parameters.get(1);
AServer server = serverManagementService.loadServer(commandContext.getGuild());
placeManagementService.moveAssignableRolePlace(server, name, newChannel);
placeManagementService.moveAssignableRolePlace(name, newChannel);
return CommandResult.fromSuccess();
}

View File

@@ -19,6 +19,10 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to remove a {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole role}
* from an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class RemoveRoleFromAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -18,6 +18,10 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* Command used to set the position of one individual {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole assignableRole}
* within an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class SetAssignableRolePosition extends AbstractConditionableCommand {

View File

@@ -20,6 +20,10 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to render the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
* and create the {@link net.dv8tion.jda.api.entities.Message messages} used to add {@link net.dv8tion.jda.api.entities.MessageReaction reactions} to
*/
@Component
@Slf4j
public class SetupAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -16,7 +16,11 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to show the configuration of an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class ShowAssignableRolePlaceConfig extends AbstractConditionableCommand {
@@ -28,13 +32,13 @@ public class ShowAssignableRolePlaceConfig extends AbstractConditionableCommand
private ServerManagementService serverManagementService;
@Override
public CommandResult execute(CommandContext commandContext) {
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
String name = (String) parameters.get(0);
AServer server = serverManagementService.loadServer(commandContext.getGuild());
// TODO refactor to return something to be posted in this command here instead of relying it to be posted somewhere else
service.showAssignablePlaceConfig(server, name, commandContext.getChannel());
return CommandResult.fromIgnored();
return service.showAssignablePlaceConfig(server, name, commandContext.getChannel())
.thenApply(unused -> CommandResult.fromIgnored());
}
@Override
@@ -46,6 +50,7 @@ public class ShowAssignableRolePlaceConfig extends AbstractConditionableCommand
.name("showAssignableRolePlaceConfig")
.module(AssignableRoleModule.ASSIGNABLE_ROLES)
.templated(true)
.async(true)
.causesReaction(true)
.supportsEmbedException(true)
.parameters(parameters)

View File

@@ -15,7 +15,10 @@ import org.springframework.stereotype.Component;
import java.util.concurrent.CompletableFuture;
/**
* Command used to show all {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace places}
* within the current {@link AServer server}
*/
@Component
public class ShowAssignableRolePlaces extends AbstractConditionableCommand {

View File

@@ -20,6 +20,10 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* Command used to swap the positions of two {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole roles}
* within one {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Component
public class SwapAssignableRolePosition extends AbstractConditionableCommand {

View File

@@ -18,6 +18,10 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Command used to show how an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
* would look like in the current {@link net.dv8tion.jda.api.entities.TextChannel channel}
*/
@Component
public class TestAssignableRolePlace extends AbstractConditionableCommand {

View File

@@ -20,6 +20,10 @@ public class AssignablePostDeletedListener implements AsyncMessageDeletedListene
@Autowired
private AssignableRolePlacePostManagementService service;
/**
* This method deletes one individual {@link AssignableRolePlacePost post}, because its message has been deleted
* @param messageBefore The {@link CachedMessage message} which was deleted
*/
@Override
public void execute(CachedMessage messageBefore) {
Optional<AssignableRolePlacePost> messageOptional = service.findByMessageIdOptional(messageBefore.getMessageId());

View File

@@ -69,6 +69,16 @@ public class AssignablePostReactionAdded implements AsyncReactionAddedListener {
@Autowired
private MemberService memberService;
/**
* Determines if the {@link net.dv8tion.jda.api.entities.Message message} a reaction was added to, belongs to a
* {@link AssignableRolePlacePost post}.
* If the {@link AssignableRolePlacePost post} belong to an inactive {@link AssignableRolePlace place} this method
* will automatically remove the reaction, self reactions are ignored. Otherwise the logic according to the configuration
* of the {@link AssignableRolePlace place} will be executed.
* @param message The {@link CachedMessage message} on which a reaction was added
* @param cachedReaction The {@link CachedReaction reaction} which was added
* @param serverUser The {@link ServerUser serverUser} who added the reaction
*/
@Override
public void executeReactionAdded(CachedMessage message, CachedReactions cachedReaction, ServerUser serverUser) {
Optional<AssignableRolePlacePost> messageOptional = service.findByMessageIdOptional(message.getMessageId());
@@ -79,13 +89,13 @@ public class AssignablePostReactionAdded implements AsyncReactionAddedListener {
return;
}
CachedReaction specificReaction = cachedReaction.getReactionForSpecificUser(serverUser);
Long assignableRolePlaceId = assignablePlacePost.getId();
Long assignableRolePlacePostId = assignablePlacePost.getId();
if(assignablePlacePost.getAssignablePlace().getActive()) {
log.info("User {} added reaction to assignable role place {} in server {}. Handling added event.", serverUser.getUserId(), assignablePlacePost.getId(), serverUser.getServerId());
addAppropriateRoles(specificReaction, assignablePlacePost, serverUser, message);
} else {
reactionService.removeReactionFromMessage(specificReaction, message).exceptionally(throwable -> {
log.error("Failed to remove reaction on place {} because place is inactive.", assignableRolePlaceId, throwable);
log.error("Failed to remove reaction on place post {} because place is inactive.", assignableRolePlacePostId, throwable);
return null;
});
log.trace("Reaction for assignable place {} in sever {} was added, but place is inactive.", assignablePlacePost.getAssignablePlace().getKey(), serverUser.getServerId());
@@ -93,6 +103,19 @@ public class AssignablePostReactionAdded implements AsyncReactionAddedListener {
}
}
/**
* Iterates over all {@link AssignableRole assignableRoles} of the post and checks which {@link AssignableRole assignableRole}
* is identified by the added {@link CachedReaction reaction}. If there is no valid reaction, the {@link net.dv8tion.jda.api.entities.MessageReaction reaction}
* will be removed again. In case the {@link AssignableRolePlace place} is configured to have unique roles, this will remove the existing
* {@link net.dv8tion.jda.api.entities.MessageReaction reaction} and the assigned {@link net.dv8tion.jda.api.entities.Role role}.
* Afterwards the appropriate {@link net.dv8tion.jda.api.entities.Role role} will be added and the update
* will be stored in the database.
* @param cachedReaction The {@link CachedReaction reaction} which was added
* @param assignablePlacePost The {@link AssignableRolePlacePost post} onto which the {@link CachedReaction reaction} was added to
* @param serverUser The {@link ServerUser serverUser} who added the {@link CachedReaction reaction}
* @param message The {@link CachedMessage message} onto which the {@link net.dv8tion.jda.api.entities.MessageReaction reaction}
* was added
*/
private void addAppropriateRoles(CachedReaction cachedReaction, AssignableRolePlacePost assignablePlacePost, ServerUser serverUser, CachedMessage message) {
boolean validReaction = false;
AssignableRolePlace assignableRolePlace = assignablePlacePost.getAssignablePlace();
@@ -128,6 +151,12 @@ public class AssignablePostReactionAdded implements AsyncReactionAddedListener {
});
}
/**
* Persists the {@link AssignableRole role} changes for the user who added a reaction in the database
* @param assignableRolePlaceId The ID of the {@link AssignableRolePlace place}
* @param serverUser The {@link ServerUser serverUser} who added the {@link net.dv8tion.jda.api.entities.MessageReaction reaction}
* @param cachedReaction The {@link CachedReaction reaction} wich was added
*/
@Transactional
public void updateStoredAssignableRoles(Long assignableRolePlaceId, ServerUser serverUser, CachedReaction cachedReaction) {
AssignableRolePlace place = assignableRolePlaceManagementService.findByPlaceId(assignableRolePlaceId);

View File

@@ -1,6 +1,7 @@
package dev.sheldan.abstracto.assignableroles.listener;
import dev.sheldan.abstracto.assignableroles.config.features.AssignableRoleFeature;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost;
import dev.sheldan.abstracto.assignableroles.service.AssignableRoleService;
import dev.sheldan.abstracto.assignableroles.service.management.AssignableRolePlacePostManagementService;
@@ -38,6 +39,16 @@ public class AssignablePostReactionRemoved implements AsyncReactionRemovedListen
return AssignableRoleFeature.ASSIGNABLE_ROLES;
}
/**
* Determines if the {@link net.dv8tion.jda.api.entities.Message message} a reaction was removed from, belongs to a
* {@link AssignableRolePlacePost post}.
* If the {@link AssignableRolePlacePost post} belong to an inactive {@link AssignableRolePlace place} this method ignores the removal.
* Otherwise the logic according to the configuration
* of the {@link AssignableRolePlace place} will be executed.
* @param message The {@link CachedMessage message} on which a reaction was added
* @param reactions All the reactions which are currently known to be on the {@link CachedMessage message}
* @param userRemoving The {@link ServerUser serverUser} which removed a {@link net.dv8tion.jda.api.entities.MessageReaction reaction}
*/
@Override
public void executeReactionRemoved(CachedMessage message, CachedReactions reactions, ServerUser userRemoving) {
Optional<AssignableRolePlacePost> messageOptional = service.findByMessageIdOptional(message.getMessageId());

View File

@@ -1,16 +1,12 @@
package dev.sheldan.abstracto.assignableroles.repository;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
import java.util.Optional;
/**
* Repository to manage the access to the table managed by {@link AssignableRolePlacePost posts}
*/
@Repository
public interface AssignableRolePlacePostRepository extends JpaRepository<AssignableRolePlacePost, Long> {
@NotNull
@Override
Optional<AssignableRolePlacePost> findById(@NonNull Long aLong);
}

View File

@@ -2,24 +2,40 @@ package dev.sheldan.abstracto.assignableroles.repository;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace;
import dev.sheldan.abstracto.core.models.database.AServer;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
/**
* Repository to manage the access to the table managed by {@link AssignableRolePlace place}
*/
@Repository
public interface AssignableRolePlaceRepository extends JpaRepository<AssignableRolePlace, Long> {
/**
* Whether or not an {@link AssignableRolePlace place} exists in a {@link AServer server} with the given key
* @param server The {@link AServer server} to search in
* @param key The key to search for
* @return Whether or not a {@link AssignableRolePlace place} exists in the {@link AServer server} with the key
*/
boolean existsByServerAndKey(AServer server, String key);
/**
* Finds an {@link AssignableRolePlace place} in a {@link AServer server} with the given key,
* returns an empty {@link Optional optional} otherwise
* @param server The {@link AServer server} to search in
* @param key The key to search for
* @return An {@link Optional optional} containing the {@link AssignableRolePlace place} if it exists, empty otherwise.
*/
Optional<AssignableRolePlace> findByServerAndKey(AServer server, String key);
/**
* Finds all {@link AssignableRolePlace places} in the given {@link AServer server}
* @param server The {@link AServer server} to retrieve {@link AssignableRolePlace places} for
* @return A list of {@link AssignableRolePlace places} which were found in the given {@link AServer server}
*/
List<AssignableRolePlace> findByServer(AServer server);
@NotNull
@Override
Optional<AssignableRolePlace> findById(@NonNull Long aLong);
}

View File

@@ -1,16 +1,12 @@
package dev.sheldan.abstracto.assignableroles.repository;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRole;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
import java.util.Optional;
/**
* Repository to manage the access to the table managed by {@link AssignableRole assignableRole}
*/
@Repository
public interface AssignableRoleRepository extends JpaRepository<AssignableRole, Long> {
@NotNull
@Override
Optional<AssignableRole> findById(@NonNull Long aLong);
}

View File

@@ -1,19 +1,12 @@
package dev.sheldan.abstracto.assignableroles.repository;
import dev.sheldan.abstracto.assignableroles.models.database.AssignedRoleUser;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
import java.util.Optional;
/**
* Repository to manage the access to the table managed by {@link AssignedRoleUser assignedRoleUser}
*/
@Repository
public interface AssignedRoleUserRepository extends JpaRepository<AssignedRoleUser, Long> {
@NotNull
@Override
Optional<AssignedRoleUser> findById(@NonNull Long aLong);
@Override
boolean existsById(@NonNull Long aLong);
}

View File

@@ -2,7 +2,7 @@ package dev.sheldan.abstracto.assignableroles.service;
import dev.sheldan.abstracto.assignableroles.config.AssignableRolePlaceParameterKey;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceAlreadyExistsException;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceChannelDoesNotExist;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceChannelDoesNotExistException;
import dev.sheldan.abstracto.assignableroles.exceptions.EmoteNotInAssignableRolePlaceException;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRole;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace;
@@ -94,17 +94,17 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
private AssignableRolePlacePostManagementServiceBean assignableRolePlacePostManagementServiceBean;
@Override
public void createAssignableRolePlace(AServer server, String name, AChannel channel, String text) {
if(rolePlaceManagementService.doesPlaceExist(server, name)) {
public void createAssignableRolePlace(String name, AChannel channel, String text) {
if(rolePlaceManagementService.doesPlaceExist(channel.getServer(), name)) {
throw new AssignableRolePlaceAlreadyExistsException(name);
}
rolePlaceManagementService.createPlace(server, name, channel, text);
rolePlaceManagementService.createPlace(name, channel, text);
}
@Override
public boolean hasAssignableRolePlaceEmote(AServer server, String placeName, AEmote emote) {
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByServerAndKey(server, placeName);
return hasAssignableRolePlaceEmote(assignableRolePlace, emote);
return hasAssignableRolePlaceEmote(assignableRolePlace, emote);
}
@Override
@@ -186,7 +186,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
} else {
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);
return addNewMessageToAssignableRolePlace(placeId, fakeEmote, description, roleId, messageToSend, textChannel);
}
} else {
throw new ChannelNotInGuildException(latestPost.getUsedChannel().getId());
@@ -201,6 +201,19 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
return CompletableFuture.completedFuture(null);
}
/**
* Adds the reaction from {@link FullEmote} to the last {@link AssignableRolePlacePost post} of its {@link AssignableRolePlace place}.
* It will also update the message representing the last {@link AssignableRolePlacePost post} with the given {@link MessageToSend messageToSend}
* in the given {@link TextChannel channel}. Afterwards the added {@link AssignableRole assignableRole} will be persisted in the database
* @param fakeEmote The *fake* {@link FullEmote emote} which represents the reaction which should be added and identifies the {@link AssignableRole role}
* @param description The description the {@link AssignableRole role} should have
* @param roleId The ID of the {@link ARole role} which should be given when a reaction is added by a {@link Member member}
* @param latestPost The last {@link AssignableRolePlacePost post} in the list of posts
* @param messageToSend The {@link MessageToSend messageToSend} which should be used to update the latest {@link AssignableRolePlacePost post} with. This
* will be done in place.
* @param textChannel The {@link TextChannel channel} in which the {@link AssignableRolePlacePost post} is
* @return A {@link CompletableFuture future} which will complete when the reaction was added, the message was edited and the database has been updated
*/
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();
@@ -220,20 +233,43 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
);
}
private CompletableFuture<Void> addNewMessageToAssignableRolePlace(Long placeId, FullEmote fakeEmote, String description, Long roleId, Long serverId, MessageToSend messageToSend, TextChannel textChannel) {
/**
* Sends a new {@link Message message}, defined by the given {@link MessageToSend messageToSend}, to the channel the {@link AssignableRolePlace place}
* is configured for, adds a reaction and stores the update in the database.
* @param placeId The ID of the {@link AssignableRolePlace place} to add a post to
* @param fakeEmote The *fake* {@link FullEmote emote} which represents the reaction which should be added and identifies the {@link AssignableRole role}
* @param description The description the {@link AssignableRole role} should have
* @param roleId The ID of the {@link ARole role} which should be given when a reaction is added by a {@link Member member}
* @param messageToSend The {@link MessageToSend messageToSend} which should be used to create the new {@link AssignableRolePlacePost post}
* @param textChannel The The {@link TextChannel channel} in which the {@link AssignableRolePlacePost post} should be in
* @return A {@link CompletableFuture future} which competes when the {@link MessageToSend messageToSend} has been sent, a {@link MessageReaction reaction}
* has been added and the changes have been persisted in the database.
*/
private CompletableFuture<Void> addNewMessageToAssignableRolePlace(Long placeId, FullEmote fakeEmote, String description, Long roleId, MessageToSend messageToSend, TextChannel textChannel) {
MessageEmbed embedToUse = messageToSend.getEmbeds().get(messageToSend.getEmbeds().size() - 1);
Long serverId = textChannel.getGuild().getIdLong();
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 reactionService.addReactionToMessageAsync(fakeEmote.getFakeEmote(), serverId, message)
.thenAccept(aVoid ->
self.addNewlyCreatedAssignablePlacePost(placeId, description, roleId, serverId, message, fakeEmote)
self.addNewlyCreatedAssignablePlacePost(placeId, description, roleId, message, fakeEmote)
);
});
}
/**
* Persisted the newly created {@link AssignableRolePlacePost post} and adds a {@link AssignableRole assignableRolePlace} to this
* @param placeId The ID of the {@link AssignableRolePlace place} to add a post to
* @param description The description the {@link AssignableRole role} should have
* @param roleId The ID of the {@link ARole role} which should be given when a reaction is added by a {@link Member member}
* @param message The {@link Message message} which was created when sending the {@link MessageToSend messageToSend} representing this
* {@link AssignableRolePlacePost post}
* @param fakeEmote The *fake* {@link FullEmote emote} which is used to identify the {@link AssignableRole role}
*/
@Transactional
public void addNewlyCreatedAssignablePlacePost(Long placeId, String description,Long roleId, Long serverId, Message message, FullEmote fakeEmote) {
public void addNewlyCreatedAssignablePlacePost(Long placeId, String description, Long roleId, Message message, FullEmote fakeEmote) {
Long serverId = message.getGuild().getIdLong();
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.findByPlaceId(placeId);
@@ -245,6 +281,18 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
assignableRoleManagementServiceBean.addRoleToPlace(loadedPlace, emote, role, description, newPost);
}
/**
* Actually creates the {@link AEmote emote} defined by the given {@link FullEmote emote} and adds a new instance
* of an {@link AssignableRole assignableRole} to the {@link AssignableRolePlace place}. This is used in case
* there already exists an {@link AssignableRolePlacePost post}
* @param messageId The ID of the {@link Message message} which identifies the {@link AssignableRolePlacePost post}
* at which this {@link AssignableRole role} is available via a {@link MessageReaction reaction}
* @param placeId The ID of the {@link AssignableRolePlace place} to add the {@link AssignableRole assignableRole} to
* @param roleId The ID of the {@link ARole role} which should be given when a reaction is added by a {@link Member member}
* @param description The description the {@link AssignableRole role} should have
* @param fakeEmote The *fake* {@link FullEmote emote} which is used to identify the {@link AssignableRole role}
* @param serverId The ID of the {@link AServer server} this should be persisted for
*/
@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);
@@ -254,6 +302,16 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
assignableRoleManagementServiceBean.addRoleToPlace(placeId, emote.getId(), roleId, description, messageId);
}
/**
* Actually creates the {@link AEmote emote} defined by the given {@link FullEmote emote} and adds a new instance
* of an {@link AssignableRole assignableRole} to the {@link AssignableRolePlace place}. This is used in case
* there doesnt exists an {@link AssignableRolePlacePost post}
* @param placeId The ID of the {@link AssignableRolePlace place} to add the {@link AssignableRole assignableRole} to
* @param roleId The ID of the {@link ARole role} which should be given when a reaction is added by a {@link Member member}
* @param description The description the {@link AssignableRole role} should have
* @param fakeEmote The *fake* {@link FullEmote emote} which is used to identify the {@link AssignableRole role}
* @param serverId The ID of the {@link AServer server} this should be persisted for
*/
@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);
@@ -279,6 +337,12 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
return CompletableFuture.completedFuture(null);
}
/**
* This method deletes the {@link AssignableRole assignableRole} from the given {@link AssignableRolePlace place}
* in the database
* @param placeId The ID of the {@link AssignableRolePlace place} to remove the {@link AssignableRole assignableRole} from
* @param assignableRoleId The ID of the {@link AssignableRole assignableRole} to remove from the {@link AssignableRolePlace place}
*/
@Transactional
public void deleteAssignableRoleFromPlace(Long placeId, Long assignableRoleId) {
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByPlaceId(placeId);
@@ -292,22 +356,31 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
});
}
private CompletableFuture<Void> removeRoleFromAssignablePlace(AssignableRole role, AssignableRolePlace assignableRolePlace) {
AssignableRolePlacePost post = role.getAssignableRolePlacePost();
/**
* The removes the {@link AssignableRole assignableRole} from the given {@link AssignableRolePlace place}. If
* there already is a {@link AssignableRolePlacePost post}, this removes the field from the post
* and also clears all {@link MessageReaction reactions}
* @param assignableRole The {@link AssignableRole role} to remove
* @param assignableRolePlace The {@link AssignableRolePlace place} to remove the role from
* @return A {@link CompletableFuture future} which completes when the message was updated and the reaction was removed, if
* no post was present it completes immediately.
*/
private CompletableFuture<Void> removeRoleFromAssignablePlace(AssignableRole assignableRole, AssignableRolePlace assignableRolePlace) {
AssignableRolePlacePost post = assignableRole.getAssignableRolePlacePost();
if(post != null) {
AServer server = assignableRolePlace.getServer();
TextChannel textChannel = channelService.getTextChannelFromServer(server.getId(), post.getUsedChannel().getId());
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 = reactionService.clearReactionFromMessageWithFuture(role.getEmote(), assignableRolePlace.getServer().getId(), role.getAssignableRolePlacePost().getUsedChannel().getId(), role.getAssignableRolePlacePost().getId());
log.trace("Removing field describing assignable role {} in assignable role place {} from post {}.", assignableRole.getId(), assignableRolePlace.getId(), messageId);
CompletableFuture<Message> fieldEditing = channelService.removeFieldFromMessage(textChannel, messageId, assignableRoles.indexOf(assignableRole));
log.trace("Clearing reaction for emote {} on assignable role post {} in assignable role place {}.", assignableRole.getEmote().getId(), messageId, assignableRolePlace.getId());
CompletableFuture<Void> reactionRemoval = reactionService.clearReactionFromMessageWithFuture(assignableRole.getEmote(), assignableRolePlace.getServer().getId(), assignableRole.getAssignableRolePlacePost().getUsedChannel().getId(), assignableRole.getAssignableRolePlacePost().getId());
return CompletableFuture.allOf(fieldEditing, reactionRemoval);
} else {
// 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());
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.", assignableRole.getEmote().getEmoteId());
return CompletableFuture.completedFuture(null);
}
}
@@ -360,7 +433,8 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
CompletableFuture<Message> messageCompletableFuture = channelService.editEmbedMessageInAChannel(iterator.next(), textChannel, post.getId());
futures.add(messageCompletableFuture);
});
} else {
throw new ChannelNotInGuildException(latestPost.getUsedChannel().getId());
}
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
}
@@ -378,8 +452,9 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
if(channelOptional.isPresent()) {
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));
} else {
throw new ChannelNotInGuildException(channelId);
}
throw new ChannelNotInGuildException(channelId);
}
return CompletableFuture.completedFuture(null);
}
@@ -394,6 +469,12 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
}
/**
* Delete all {@link Message messages} which are stored as {@link AssignableRolePlacePost posts} from the given
* {@link AssignableRolePlace place}
* @param assignableRolePlace The {@link AssignableRolePlace place} to delete the posts of
* @return A list of {@link CompletableFuture futures} each representing one deleted {@link Message message}
*/
private List<CompletableFuture<Void>> deleteExistingMessagePostsForPlace(AssignableRolePlace assignableRolePlace) {
List<CompletableFuture<Void>> oldPostDeletionFutures = new ArrayList<>();
assignableRolePlace.getMessagePosts().forEach(assignableRolePlacePost -> {
@@ -556,9 +637,9 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
@Override
public void showAssignablePlaceConfig(AServer server, String name, TextChannel channel) {
public CompletableFuture<Void> showAssignablePlaceConfig(AServer server, String name, TextChannel channel) {
AssignableRolePlace place = rolePlaceManagementService.findByServerAndKey(server, name);
List<AssignablePostConfigRole> roles = new ArrayList<>();
List<AssignableRolePlaceConfigRole> roles = new ArrayList<>();
Guild guild = guildService.getGuildById(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());
@@ -566,7 +647,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
AEmote emoteForRole = role.getEmote();
Emote jdaEmoteForRole = emoteService.getEmote(emoteForRole).orElse(null);
Role jdaRole = guild.getRoleById(role.getRole().getId());
AssignablePostConfigRole postRole = AssignablePostConfigRole
AssignableRolePlaceConfigRole postRole = AssignableRolePlaceConfigRole
.builder()
.description(role.getDescription())
.emote(FullEmote.builder().fakeEmote(emoteForRole).emote(jdaEmoteForRole).build())
@@ -581,31 +662,44 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
.roles(roles)
.place(place)
.build();
channelService.sendEmbedTemplateInTextChannelList(ASSIGNABLE_ROLES_CONFIG_POST_TEMPLATE_KEY, configModel, channel);
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInTextChannelList(ASSIGNABLE_ROLES_CONFIG_POST_TEMPLATE_KEY, configModel, channel));
}
@Override
public void moveAssignableRolePlace(AServer server, String name, TextChannel newChannel) {
public void moveAssignableRolePlace(String name, TextChannel newChannel) {
AChannel channel = channelManagementService.loadChannel(newChannel.getIdLong());
rolePlaceManagementService.moveAssignableRolePlace(server, name, channel);
rolePlaceManagementService.moveAssignableRolePlace(name, channel);
}
@Override
public void changeAssignablePlaceDescription(AServer server, String name, String newDescription) {
rolePlaceManagementService.changeAssignableRolePlaceDescription(server, name, newDescription);
public void changeText(AServer server, String name, String newText) {
rolePlaceManagementService.changeAssignableRolePlaceDescription(server, name, newText);
}
@Override
public CompletableFuture<Void> deleteAssignableRolePlace(AServer server, String name) {
AssignableRolePlace place = rolePlaceManagementService.findByServerAndKey(server, name);
Long placeId = place.getId();
List<CompletableFuture<Void>> deleteFutures = deleteExistingMessagePostsForPlace(place);
return CompletableFuture.allOf(deleteFutures.toArray(new CompletableFuture[0]))
.thenAccept(unused -> self.deleteAssignableRolePlaceInDatabase(placeId));
}
/**
* Deletes the {@link AssignableRolePlace place} in the database and all the
* {@link AEmote emotes} which were used to identify the {@link AssignableRole roles}
* @param placeId The ID of the {@link AssignableRolePlace place} to delete
*/
@Transactional
public void deleteAssignableRolePlaceInDatabase(Long placeId) {
AssignableRolePlace place = rolePlaceManagementService.findByPlaceId(placeId);
rolePlaceManagementService.deleteAssignablePlace(place);
deleteEmotesFromAssignableRolePlace(place);
List<CompletableFuture<Void>> deleteFutures = deleteExistingMessagePostsForPlace(place);
return CompletableFuture.allOf(deleteFutures.toArray(new CompletableFuture[0]));
}
@Override
public CompletableFuture<Void> changeText(AServer server, String name, String newText) {
public CompletableFuture<Void> changeTextAsync(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);
@@ -632,7 +726,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
public CompletableFuture<Void> changeConfiguration(AServer server, String name, AssignableRolePlaceParameterKey keyToChange, Object newValue) {
Boolean booleanValue = BooleanUtils.toBooleanObject(newValue.toString());
if(booleanValue == null) {
throwBooleanParameterKeyException();
throw new CommandParameterKeyValueWrongTypeException(Arrays.asList("yes", "no", "true", "false", "on", "off"));
}
switch (keyToChange) {
case INLINE:
@@ -660,10 +754,10 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
return CompletableFuture.allOf(promises.toArray(new CompletableFuture[0]));
}
private void throwBooleanParameterKeyException() {
throw new CommandParameterKeyValueWrongTypeException(Arrays.asList("yes", "no", "true", "false", "on", "off"));
}
/**
* Deletes the {@link AEmote emotes} for each {@link AssignableRole assignableRole} from a {@link AssignableRolePlace place}
* @param place The {@link AssignableRolePlace place} to delete the {@link AEmote emotes} of
*/
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 ->
@@ -671,16 +765,35 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
);
}
/**
* Renders the {@link AssignableRolePlace place} to a {@link MessageToSend messageToSend}, and
* sends it in the given {@link TextChannel channel}
* @param place The {@link AssignableRolePlace place} to render
* @param channel The {@link TextChannel channel} in which the {@link MessageToSend messageToSend} should be sent to
* @return A list of {@link CompletableFuture futures} which represent each message which was sent
*/
private List<CompletableFuture<Message>> sendAssignablePostMessages(AssignableRolePlace place, TextChannel channel) {
MessageToSend messageToSend = renderAssignablePlacePosts(place);
return channelService.sendMessageToSendToChannel(messageToSend, channel);
}
/**
* Renders the {@link AssignableRolePlace place} with the appropriate template and returns a {@link MessageToSend messageToSend}
* @param place The {@link AssignableRolePlace place} to render
* @return A {@link MessageToSend messageToSend} which can be sent, containing the individual posts
*/
private MessageToSend renderAssignablePlacePosts(AssignableRolePlace place) {
AssignablePostMessage model = prepareAssignablePostMessageModel(place);
return templateService.renderEmbedTemplate(ASSIGNABLE_ROLES_POST_TEMPLATE_KEY, model);
}
/**
* Converts individual parts of the given {@link AssignableRolePlace place} into a model which is usable
* in the template responsible to render the {@link AssignableRolePlace place}. This requires to load
* the {@link Emote emotes} and ordering according to their position
* @param place The {@link AssignableRolePlace place} which should be made available for rendering
* @return The {@link AssignablePostMessage model} which holds the necessary values to render the template
*/
private AssignablePostMessage prepareAssignablePostMessageModel(AssignableRolePlace place) {
List<AssignablePostRole> roles = new ArrayList<>();
List<AssignableRole> rolesToAdd = place.getAssignableRoles().stream().sorted(Comparator.comparingInt(AssignableRole::getPosition)).collect(Collectors.toList());
@@ -692,6 +805,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
AssignablePostRole lastAddedRole = null;
for (int position = 0; position < maxPosition + 1; position++) {
boolean legitEntry = current.getPosition().equals(position);
// TODO is this still required?
boolean startOfNewMessage = position > 0 && (position % 21) == 0;
if(legitEntry) {
AEmote emoteForRole = current.getEmote();
@@ -723,6 +837,15 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
.build();
}
/**
* Creates the {@link Message messages} for the posts required for the {@link AssignableRolePlace place} and
* adds the reactions afterwards.
* @param serverId The ID of the {@link AServer server} in which the {@link AssignableRolePlace place} posts should
* be created in
* @param assignablePlaceId The ID of the {@link AssignableRolePlace place} which should have its {@link Message messages} posted
* @return A {@link CompletableFuture future} which completes when the {@link Message messages} were posted and the {@link MessageReaction reactions}
* added
*/
@Transactional
public CompletableFuture<Void> createAssignableRolePlacePosts(Long serverId, Long assignablePlaceId) {
AssignableRolePlace assignableRolePlace = rolePlaceManagementService.findByPlaceId(assignablePlaceId);
@@ -735,15 +858,26 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
.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(), assignableRolePlace.getKey());
throw new AssignableRolePlaceChannelDoesNotExistException(assignableRolePlace.getChannel().getId(), assignableRolePlace.getKey());
}
}
/**
* Adds the appropriate {@link MessageReaction reactions} defined by the {@link AssignableRole assignableRoles} of the
* {@link AssignableRolePlace place} to the {@link Message messages} which are provided via the {@link CompletableFuture futures}.
* The mapping of reaction to assignable role is done via the index of the fields. Depending on the amount of fields
* which were created in the {@link Message messages}. Afterwards it will store the created
* {@link AssignableRolePlacePost posts}
* @param assignablePlacePostsMessageFutures A list of {@link CompletableFuture futures} which represent the individual
* {@link Message messages} which were posted when setting up
* the {@link AssignableRolePlace place}
* @param assignablePlaceId The ID of the {@link AssignableRolePlace place} which was setup
* @return A {@link CompletableFuture future} which completes when all reactions were added and the database
* was updated.
*/
@Transactional
public CompletableFuture<Void> addEmotes(List<CompletableFuture<Message>> assignablePlacePostsMessageFutures, Long assignablePlaceId) {
Message firstMessage = assignablePlacePostsMessageFutures.get(0).join();
Long serverId = firstMessage.getGuild().getIdLong();
AssignableRolePlace innerRolePlace = rolePlaceManagementService.findByPlaceId(assignablePlaceId);
log.info("Adding emotes to assignable role place {}.", innerRolePlace);
@@ -760,12 +894,12 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
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 = addingReactionsToAssignableRolePlacePost(sentMessage, serverId, usedEmoteIds);
CompletableFuture<Void> firstMessageFuture = addingReactionsToAssignableRolePlacePost(sentMessage, usedEmoteIds);
reactionFutures.add(firstMessageFuture);
}
return CompletableFuture.allOf(reactionFutures.toArray(new CompletableFuture[0])).thenCompose(aVoid -> {
try {
self.storeCreatedAssignableRolePlacePosts(assignablePlaceId, serverId, assignablePlacePostsMessageFutures);
self.storeCreatedAssignableRolePlacePosts(assignablePlaceId, assignablePlacePostsMessageFutures);
} catch (Exception e) {
log.error("Failed to persist assignable role place posts. ", e);
}
@@ -773,10 +907,19 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
});
}
/**
* Creates the {@link AssignableRolePlacePost posts} for the created {@link Message messages} and stores them in the
* database. This also uses the fields in the {@link MessageEmbed embed} in order to determine which
* {@link AssignableRole role} is in which {@link AssignableRolePlacePost post}
* @param assignablePlaceId The ID of the {@link AssignableRolePlace place} to which the {@link AssignableRolePlacePost posts}
* should be added to
* @param futures A list of {@link CompletableFuture futures} which holds the {@link Message messages} which were created
* when setting up the {@link AssignableRolePlace place}
*/
@Transactional
public void storeCreatedAssignableRolePlacePosts(Long assignablePlaceId, Long serverId, List<CompletableFuture<Message>> futures) {
public void storeCreatedAssignableRolePlacePosts(Long assignablePlaceId, List<CompletableFuture<Message>> futures) {
AssignableRolePlace updatedPlace = rolePlaceManagementService.findByPlaceId(assignablePlaceId);
log.info("Storing {} messages for assignable role place {} in server {}.", futures.size(), assignablePlaceId, serverId);
log.info("Storing {} messages for assignable role place {} in server {}.", futures.size(), assignablePlaceId, updatedPlace.getServer().getId());
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++) {
@@ -794,14 +937,21 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
}
}
/**
* Adds the given {@link AEmote emotes} identified by the ID to the given Message
* @param message The {@link Message message} on which the {@link MessageReaction reactions} should be added on
* @param emotesToAdd A list of {@link Integer integers} which contains the ID of the {@link AEmote emotes} which should be added
* @return A {@link CompletableFuture future} which complets when all reactions have been added
*/
@Transactional
public CompletableFuture<Void> addingReactionsToAssignableRolePlacePost(Message message, Long server, List<Integer> emotesToAdd) {
public CompletableFuture<Void> addingReactionsToAssignableRolePlacePost(Message message, 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);
Long serverId = message.getGuild().getIdLong();
log.info("Adding {} emotes to assignable role place post {} in server {}.", emotesToAdd.size(), message.getId(), serverId);
emotesToAdd.forEach(emotesToUse -> {
AEmote emoteToUseObject = emoteManagementService.loadEmote(emotesToUse);
futures.add(reactionService.addReactionToMessageAsync(emoteToUseObject, server, message));
futures.add(reactionService.addReactionToMessageAsync(emoteToUseObject, serverId, message));
});
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
}

View File

@@ -69,11 +69,11 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
.build();
@Override
public CompletableFuture<Void> assignAssignableRoleToUser(Long assignableRoleId, Member toAdd) {
public CompletableFuture<Void> assignAssignableRoleToUser(Long assignableRoleId, Member member) {
metricService.incrementCounter(ASSIGNABLE_ROLES_ASSIGNED);
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());
log.info("Assigning role {} to member {} in server {}.", assignableRoleId, member.getId(), member.getGuild().getId());
return roleService.addRoleToMemberFuture(member, role.getRole());
}
@Override
@@ -133,6 +133,7 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
}
@Transactional
@Override
public void addRoleToUser(Long assignableRoleId, AUserInAServer aUserInAServer) {
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);
addRoleToUser(role, aUserInAServer);
@@ -152,6 +153,11 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
assignedRoleUserManagementServiceBean.removeAssignedRoleFromUser(assignableRole, aUserInAServer);
}
/**
* Adds the given {@link AssignableRole assignableRole} identified by the ID to the given {@link Member member}
* @param assignableRoleId The ID of the {@link AssignableRole} to be added to the {@link Member member}
* @param member The {@link Member member} to add the {@link AssignableRole role} to
*/
@Transactional
public void addRoleToUser(Long assignableRoleId, Member member) {
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);
@@ -159,12 +165,22 @@ public class AssignableRoleServiceBean implements AssignableRoleService {
addRoleToUser(role, aUserInAServer);
}
/**
* Removes the given {@link AssignableRole assignableRole} identified by the ID from the given {@link AUserInAServer user}
* @param assignableRoleId The ID of the {@link AssignableRole} to be removed from the {@link Member member}
* @param aUserInAServer The {@link AUserInAServer user} to remove the {@link AssignableRole role} from
*/
@Transactional
public void removeRoleFromUser(Long assignableRoleId, AUserInAServer aUserInAServer) {
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);
removeRoleFromUser(role, aUserInAServer);
}
/**
* Stores the removal of an {@link AssignableRole assignableRole} from a {@link Member member} in the database
* @param assignableRoleId The ID of the {@link AssignableRole} to be removed from the {@link Member member}
* @param member The {@link Member member} which should get the {@link AssignableRole role} removed
*/
@Transactional
public void persistRoleRemovalFromUser(Long assignableRoleId, Member member) {
AssignableRole role = assignableRoleManagementServiceBean.getByAssignableRoleId(assignableRoleId);

View File

@@ -13,7 +13,6 @@ 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;
@@ -85,18 +84,6 @@ public class AssignableRoleManagementServiceBean implements AssignableRoleManage
return repository.findById(assignableRoleId).orElseThrow(() -> new AbstractoRunTimeException("Assignable role not found"));
}
@Override
public AssignableRole getRoleForReactionEmote(MessageReaction.ReactionEmote emote, AssignableRolePlace assignableRolePlace) {
for (AssignableRolePlacePost post : assignableRolePlace.getMessagePosts()) {
for (AssignableRole assignableRole : post.getAssignableRoles()) {
if (emoteService.isReactionEmoteAEmote(emote, assignableRole.getEmote())) {
return assignableRole;
}
}
}
throw new AbstractoRunTimeException("Role for reaction was not found.");
}
@Override
public AssignableRole getRoleForReactionEmote(CachedEmote cachedEmote, AssignableRolePlace assignableRolePlace) {
for (AssignableRolePlacePost post : assignableRolePlace.getMessagePosts()) {

View File

@@ -20,16 +20,16 @@ public class AssignableRolePlaceManagementServiceBean implements AssignableRoleP
private AssignableRolePlaceRepository repository;
@Override
public AssignableRolePlace createPlace(AServer server, String name, AChannel channel, String text) {
public AssignableRolePlace createPlace(String name, AChannel channel, String text) {
AssignableRolePlace place = AssignableRolePlace
.builder()
.channel(channel)
.server(server)
.server(channel.getServer())
.text(text)
.key(name)
.build();
repository.save(place);
log.info("Creating assignable role place in channel {} on server {}.", channel.getId(), server.getId());
log.info("Creating assignable role place in channel {} on server {}.", channel.getId(), channel.getServer().getId());
return place;
}
@@ -55,10 +55,10 @@ public class AssignableRolePlaceManagementServiceBean implements AssignableRoleP
}
@Override
public void moveAssignableRolePlace(AServer server, String name, AChannel newChannel) {
AssignableRolePlace assignablePlaceToChange = findByServerAndKey(server, name);
public void moveAssignableRolePlace(String name, AChannel newChannel) {
AssignableRolePlace assignablePlaceToChange = findByServerAndKey(newChannel.getServer(), name);
log.info("Moving assignable role place {} in server {} from channel {} to channel {}.",
assignablePlaceToChange.getId(), server.getId(), assignablePlaceToChange.getChannel().getId(), newChannel.getId());
assignablePlaceToChange.getId(), newChannel.getServer().getId(), assignablePlaceToChange.getChannel().getId(), newChannel.getId());
assignablePlaceToChange.setChannel(newChannel);
}

View File

@@ -1,6 +1,6 @@
package dev.sheldan.abstracto.assignableroles.service.management;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignablePlacePostNotFoundException;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlacePostNotFoundException;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost;
import dev.sheldan.abstracto.assignableroles.repository.AssignableRolePlacePostRepository;
@@ -22,7 +22,7 @@ public class AssignableRolePlacePostManagementServiceBean implements AssignableR
@Override
public AssignableRolePlacePost findByMessageId(Long messageId) {
return findByMessageIdOptional(messageId).orElseThrow(() -> new AssignablePlacePostNotFoundException(messageId));
return findByMessageIdOptional(messageId).orElseThrow(() -> new AssignableRolePlacePostNotFoundException(messageId));
}
@Override

View File

@@ -43,6 +43,7 @@ public class AssignedRoleUserManagementServiceBean implements AssignedRoleUserMa
removeAssignedRoleFromUser(assignableRole, user);
}
@Override
public void removeAssignedRoleFromUser(AssignableRole assignableRole, AssignedRoleUser user) {
assignableRole.getAssignedUsers().remove(user);
user.getRoles().remove(assignableRole);
@@ -55,21 +56,6 @@ public class AssignedRoleUserManagementServiceBean implements AssignedRoleUserMa
return repository.save(newUser);
}
@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)
);
user.getRoles().clear();
}
@Override
public boolean doesAssignedRoleUserExist(AUserInAServer aUserInAServer) {
return repository.existsById(aUserInAServer.getUserInServerId());
}
@Override
public Optional<AssignedRoleUser> findByUserInServerOptional(AUserInAServer aUserInAServer) {
return repository.findById(aUserInAServer.getUserInServerId());

View File

@@ -3,7 +3,10 @@ package dev.sheldan.abstracto.assignableroles.config;
import dev.sheldan.abstracto.core.command.execution.CommandParameterKey;
import lombok.Getter;
/**
* This enum is used to define the different key for which there exist properties on an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace}.
* And is used for the command parameter when changing the value of an attribute on this place.
*/
@Getter
public enum AssignableRolePlaceParameterKey implements CommandParameterKey {
INLINE, UNIQUE, AUTOREMOVE, ACTIVE

View File

@@ -5,6 +5,11 @@ import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.models.FullEmote;
import dev.sheldan.abstracto.core.templating.Templatable;
/**
* Exception thrown in case the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole} has already been
* defined for an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace}. This is identified
* via the {@link net.dv8tion.jda.api.entities.Emote} which is used to react.
*/
public class AssignableRoleAlreadyDefinedException extends AbstractoRunTimeException implements Templatable {
private final AssignableRoleAlreadyDefinedExceptionModel model;

View File

@@ -6,6 +6,11 @@ import dev.sheldan.abstracto.core.models.FullRole;
import dev.sheldan.abstracto.core.templating.Templatable;
import net.dv8tion.jda.api.entities.Guild;
/**
* Exception thrown in case the defined {@link net.dv8tion.jda.api.entities.Role role} cannot be interacted with by the bot,
* because of permissions, this can happen if the role is the same or higher than the bot, therefore cannot be given to
* {@link net.dv8tion.jda.api.entities.Member members}
*/
public class AssignableRoleNotUsableException extends AbstractoRunTimeException implements Templatable {
private final AssignableRoleNotUsableExceptionModel model;

View File

@@ -4,6 +4,10 @@ import dev.sheldan.abstracto.assignableroles.models.exception.AssignableRolePlac
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.templating.Templatable;
/**
* Exception thrown in case the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
* identified by {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace#key}
*/
public class AssignableRolePlaceAlreadyExistsException extends AbstractoRunTimeException implements Templatable {
private final AssignableRolePlaceAlreadyExistsExceptionModel model;

View File

@@ -4,11 +4,15 @@ import dev.sheldan.abstracto.assignableroles.models.exception.AssignableRolePlac
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.templating.Templatable;
public class AssignableRolePlaceChannelDoesNotExist extends AbstractoRunTimeException implements Templatable {
/**
* Exception thrown in case the {@link dev.sheldan.abstracto.core.models.database.AChannel channel} in which a
* {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place} is defined, does not exist
*/
public class AssignableRolePlaceChannelDoesNotExistException extends AbstractoRunTimeException implements Templatable {
private final AssignableRolePlaceChannelDoesNotExistExceptionModel model;
public AssignableRolePlaceChannelDoesNotExist(Long channelId, String placeName) {
public AssignableRolePlaceChannelDoesNotExistException(Long channelId, String placeName) {
super("Assignable role place channel does not exist");
this.model = AssignableRolePlaceChannelDoesNotExistExceptionModel.builder().channelId(channelId).placeName(placeName).build();
}

View File

@@ -4,6 +4,10 @@ import dev.sheldan.abstracto.assignableroles.models.exception.AssignableRolePlac
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.templating.Templatable;
/**
* Exception which is thrown in case a {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
* defined by a {@link String key} does not exist
*/
public class AssignableRolePlaceNotFoundException extends AbstractoRunTimeException implements Templatable {
private final AssignableRolePlaceNotFoundExceptionModel model;

View File

@@ -4,11 +4,15 @@ import dev.sheldan.abstracto.assignableroles.models.exception.AssignableRolePlac
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.templating.Templatable;
public class AssignablePlacePostNotFoundException extends AbstractoRunTimeException implements Templatable {
/**
* Exception which is thrown, in case the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost post}
* was not found via its message ID
*/
public class AssignableRolePlacePostNotFoundException extends AbstractoRunTimeException implements Templatable {
private final AssignableRolePlacePostNotFoundExceptionModel model;
public AssignablePlacePostNotFoundException(Long messageId) {
public AssignableRolePlacePostNotFoundException(Long messageId) {
super("Assignable place post not found.");
this.model = AssignableRolePlacePostNotFoundExceptionModel.builder().messageId(messageId).build();
}

View File

@@ -5,6 +5,10 @@ import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.templating.Templatable;
/**
* Exception which is thrown in case an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignedRoleUser user}
* was not found.
*/
public class AssignedUserNotFoundException extends AbstractoRunTimeException implements Templatable {
private final AssignedUserNotFoundExceptionModel model;

View File

@@ -5,6 +5,11 @@ import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.models.FullEmote;
import dev.sheldan.abstracto.core.templating.Templatable;
/**
* Exception which is thrown in case a given {@link dev.sheldan.abstracto.core.models.database.AEmote} was not found
* in the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace}, when it was tried to switch
* or move the emotes around.
*/
public class EmoteNotInAssignableRolePlaceException extends AbstractoRunTimeException implements Templatable {
private final EmoteNotInAssignableRolePlaceExceptionModel model;

View File

@@ -11,8 +11,12 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
/**
* A {@link ARole role} which can be mapped to an {@link AssignableRolePlace ṕlace}. This is uniquely defined by an emote on the
* respective assignable role place, but the same role can be given via different {@link AEmote emote}
*/
@Entity
@Table(name="assignable_role")
@Table(name = "assignable_role")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@@ -21,46 +25,86 @@ import java.util.List;
@EqualsAndHashCode
public class AssignableRole implements Serializable {
/**
* The unique ID of this {@link AssignableRole assignableRole}
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* The {@link AEmote emote} this role is associated with
*/
@OneToOne(fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "emote_id")
private AEmote emote;
/**
* The {@link ARole} which given via this {@link AssignableRole assignableRole}
*/
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "role_id", nullable = false)
private ARole role;
/**
* The {@link AServer server} in which this {@link AssignableRole assignableRole} is used
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "server_id", nullable = false)
private AServer server;
/**
* The {@link AssignableRolePlace} this assignable role is in
*/
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "assignable_place_id", nullable = false)
private AssignableRolePlace assignablePlace;
/**
* The {@link AssignableRolePlacePost} this assignable role is currently available as a reaction.
* This is necessary, to easier find the reaction which are valid, in case a reaction is added to a post
*/
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "place_post_id")
private AssignableRolePlacePost assignableRolePlacePost;
/**
* The {@link AssignedRoleUser users} which currently have this role assigned via this mechanism.
* This is necessary to enforce the unique property of {@link AssignableRolePlace}, in which you only may chose one
* role.
*/
@ManyToMany(mappedBy = "roles")
@Builder.Default
private List<AssignedRoleUser> assignedUsers = new ArrayList<>();
/**
* The description which is shown in the embeds of the posts of the {@link AssignableRolePlace}
*/
@Column(name = "description")
private String description;
/**
* The level in experience which is required in order to receive this {@link AssignableRole}
*/
@Column(name = "required_level")
private Integer requiredLevel;
/**
* The position of this assignable role within the {@link AssignableRole}. This is required in order to show them
* the same order as the descriptions in the fields and also to move them around and switch positions
*/
@Column(name = "position")
private Integer position;
/**
* The {@link Instant} this entity was created
*/
@Column(name = "created")
private Instant created;
/**
* The {@link Instant} this entity was updated
*/
@Column(name = "updated")
private Instant updated;
}

View File

@@ -10,8 +10,13 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
/**
* The place entity holding the {@link AssignableRole roles} and {@link AssignableRolePlacePost posts} together.
* This is also the entity holding all the configuration for the place and is identified by a key as a String, which is unique
* for each server. This place holds the {@link AChannel} in which the
*/
@Entity
@Table(name="assignable_role_place")
@Table(name = "assignable_role_place")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@@ -23,22 +28,38 @@ public class AssignableRolePlace implements Serializable {
public static final Long ASSIGNABLE_PLACE_NAME_LIMIT = 255L;
public static final Long ASSIGNABLE_PLACE_DESCRIPTION_LIMIT = 255L;
/**
* A unique ID created
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
/**
* The channel in which the {@link AssignableRolePlacePost posts} for this place should be created
*/
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name="channel_id")
private AChannel channel;
/**
* The {@link AServer server} for which this place is configured for. Unique in combination with the key
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "server_id", nullable = false)
private AServer server;
/**
* The key this place is associated with via commands. Unique per server.
*/
@Column(name = "key")
private String key;
/**
* The {@link AssignableRolePlacePost posts} which were created when this place was setup. Is empty in the beginning
* and actively maintained in case a post is deleted.
*/
@OneToMany(
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
@@ -48,6 +69,9 @@ public class AssignableRolePlace implements Serializable {
@Builder.Default
private List<AssignableRolePlacePost> messagePosts = new ArrayList<>();
/**
* A List containing the {@link AssignableRole} which are associated with this place
*/
@OneToMany(
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
@@ -57,28 +81,49 @@ public class AssignableRolePlace implements Serializable {
@Builder.Default
private List<AssignableRole> assignableRoles = new ArrayList<>();
/**
* The text which is displayed in the first description area of the created {@link AssignableRolePlacePost}
*/
@Column(name = "text")
private String text;
/**
* Whether or not the reactions placed onto the posts should be acted upon
*/
@Builder.Default
@Column(name = "active")
private Boolean active = true;
/**
* Whether or not the fields containing the descriptions should be inline
*/
@Builder.Default
@Column(name = "inline")
private Boolean inline = false;
/**
* Whether or not it should be restricted, that a {@link AssignedRoleUser} should only have one role of this place
*/
@Builder.Default
@Column(name = "unique_roles")
private Boolean uniqueRoles = false;
/**
* Whether or not the added reactions should be removed automatically
*/
@Builder.Default
@Column(name = "auto_remove")
private Boolean autoRemove = false;
/**
* The {@link Instant} this entity was created
*/
@Column(name = "created")
private Instant created;
/**
* The {@link Instant} this entity was updated
*/
@Column(name = "updated")
private Instant updated;

View File

@@ -10,8 +10,12 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
/**
* One individual {@link net.dv8tion.jda.api.entities.Message message} which was sent when setting up an {@link AssignableRolePlace place}
* and contains the embeds and the reactions were placed onto it.
*/
@Entity
@Table(name="assignable_role_place_post")
@Table(name = "assignable_role_place_post")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@@ -20,28 +24,38 @@ import java.util.List;
@EqualsAndHashCode
public class AssignableRolePlacePost implements Serializable {
/**
* The ID of the {@link net.dv8tion.jda.api.entities.Message message} which represents this post with the reactions.
*/
@Id
@Column(name = "id")
private Long id;
/**
* The actual {@link AChannel channel} in which the post ended up in
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "channel_id")
private AChannel usedChannel;
@Column(name = "created")
private Instant created;
@Column(name = "updated")
private Instant updated;
/**
* The {@link AServer server} in which this place post is posted
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "server_id", nullable = false)
private AServer server;
/**
* The {@link AssignableRolePlace place} this post is associated with
*/
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "assignable_place_id", nullable = false)
private AssignableRolePlace assignablePlace;
/**
* The actual {@link AssignableRole assignableRoles} which are associated with this post, and whose respective {@link dev.sheldan.abstracto.core.models.database.AEmote}
* have been placed as reactions
*/
@OneToMany(
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
@@ -49,4 +63,16 @@ public class AssignableRolePlacePost implements Serializable {
@Builder.Default
private List<AssignableRole> assignableRoles = new ArrayList<>();
/**
* The {@link Instant} this entity was created
*/
@Column(name = "created")
private Instant created;
/**
* The {@link Instant} this entity was updated
*/
@Column(name = "updated")
private Instant updated;
}

View File

@@ -9,8 +9,12 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
/**
* A {@link AUserInAServer userInAServer} which added a reaction to an {@link AssignableRolePlace place}.
* This is required in order to guarantee the uniqueness (if desired) of {@link AssignableRole} per place.
*/
@Entity
@Table(name="assigned_role_user")
@Table(name = "assigned_role_user")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@@ -19,6 +23,9 @@ import java.util.List;
@EqualsAndHashCode
public class AssignedRoleUser implements Serializable {
/**
* The ID of the associated {@link AUserInAServer userInAServer}
*/
@Id
@Column(name = "id")
private Long id;
@@ -27,6 +34,9 @@ public class AssignedRoleUser implements Serializable {
@PrimaryKeyJoinColumn
private AUserInAServer user;
/**
* The {@link AssignableRole assignableRoles} this user has in the server
*/
@ManyToMany
@JoinTable(
name = "assigned_role_in_user",
@@ -35,9 +45,15 @@ public class AssignedRoleUser implements Serializable {
@Builder.Default
private List<AssignableRole> roles = new ArrayList<>();
/**
* The {@link Instant} this entity was created
*/
@Column(name = "created")
private Instant created;
/**
* The {@link Instant} this entity was updated
*/
@Column(name = "updated")
private Instant updated;

View File

@@ -6,9 +6,19 @@ import lombok.Getter;
import java.io.Serializable;
/**
* Contains the model for {@link dev.sheldan.abstracto.assignableroles.exceptions.AssignableRoleAlreadyDefinedException}
*/
@Getter
@Builder
public class AssignableRoleAlreadyDefinedExceptionModel implements Serializable {
/**
* The {@link FullEmote emote} which was tried to be added to a {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
private final FullEmote emote;
/**
* The key of the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace} for which it was tried to add a
* {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole role}
*/
private final String placeName;
}

View File

@@ -7,9 +7,18 @@ import net.dv8tion.jda.api.entities.Guild;
import java.io.Serializable;
/**
* Contains the model for {@link dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceNotFoundException}
*/
@Getter
@Builder
public class AssignableRoleNotUsableExceptionModel implements Serializable {
/**
* The {@link FullRole role} which is not usable as an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole role}
*/
private final FullRole role;
/**
* The {@link Guild server} in which it was not possible to use the {@link net.dv8tion.jda.api.entities.Role role}
*/
private final transient Guild guild;
}

View File

@@ -5,8 +5,14 @@ import lombok.Getter;
import java.io.Serializable;
/**
* Contains the model for {@link dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceAlreadyExistsException}
*/
@Getter
@Builder
public class AssignableRolePlaceAlreadyExistsExceptionModel implements Serializable {
/**
* The key which collides with an existing {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace}
*/
private final String name;
}

View File

@@ -1,13 +1,24 @@
package dev.sheldan.abstracto.assignableroles.models.exception;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceChannelDoesNotExistException;
import lombok.Builder;
import lombok.Getter;
import java.io.Serializable;
/**
* Contains the model for {@link AssignableRolePlaceChannelDoesNotExistException}
*/
@Getter
@Builder
public class AssignableRolePlaceChannelDoesNotExistExceptionModel implements Serializable {
/**
* The ID of the {@link dev.sheldan.abstracto.core.models.database.AChannel channel} which does not exist in the {@link net.dv8tion.jda.api.entities.Guild}
*/
private final Long channelId;
/**
* The key of the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place} for which
* the channel does not exist anymore in the {@link net.dv8tion.jda.api.entities.Guild}
*/
private final String placeName;
}

View File

@@ -1,28 +0,0 @@
package dev.sheldan.abstracto.assignableroles.models.exception;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace;
import dev.sheldan.abstracto.core.models.FullUserInServer;
import dev.sheldan.abstracto.core.templating.Templatable;
import lombok.Builder;
import lombok.Getter;
import java.io.Serializable;
@Getter
@Builder
public class AssignableRolePlaceExceptionModel implements Serializable {
private final AssignableRolePlace rolePlace;
private final FullUserInServer user;
private final Throwable throwable;
public Templatable getTemplate() {
Throwable current = throwable;
while(!(current instanceof Templatable) && !current.getCause().equals(current)) {
current = current.getCause();
}
if(current instanceof Templatable) {
return (Templatable) current;
}
return null;
}
}

View File

@@ -5,8 +5,14 @@ import lombok.Getter;
import java.io.Serializable;
/**
* Contains the model for {@link dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceNotFoundException}
*/
@Getter
@Builder
public class AssignableRolePlaceNotFoundExceptionModel implements Serializable {
/**
* The ID of the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place} which was not found
*/
private final Long placeId;
}

View File

@@ -5,8 +5,14 @@ import lombok.Getter;
import java.io.Serializable;
/**
* Contains the model for {@link dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlacePostNotFoundException}
*/
@Getter
@Builder
public class AssignableRolePlacePostNotFoundExceptionModel implements Serializable {
/**
* The ID of the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost post} which was not found in the database
*/
private final Long messageId;
}

View File

@@ -6,8 +6,14 @@ import lombok.Getter;
import java.io.Serializable;
/**
* Contains the model for {@link dev.sheldan.abstracto.assignableroles.exceptions.AssignedUserNotFoundException}
*/
@Getter
@Builder
public class AssignedUserNotFoundExceptionModel implements Serializable {
/**
* The instance of the {@link AUserInAServer userInAServer} for which the assigned user was not found
*/
private final AUserInAServer aUserInAServer;
}

View File

@@ -6,9 +6,18 @@ import lombok.Getter;
import java.io.Serializable;
/**
* Contains the model for {@link dev.sheldan.abstracto.assignableroles.exceptions.EmoteNotInAssignableRolePlaceException}
*/
@Getter
@Builder
public class EmoteNotInAssignableRolePlaceExceptionModel implements Serializable {
/**
* The {@link FullEmote emote} which was not found in the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
private final FullEmote emote;
/**
* The key of the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place} for which the emote was not found in
*/
private final String placeName;
}

View File

@@ -8,10 +8,16 @@ import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
/**
* Model used to render the overview over all {@link AssignableRolePlace places}
*/
@Getter
@Setter
@Builder
public class AssignablePlaceOverview {
/**
* The {@link AssignableRolePlace places} in the server to display
*/
@Builder.Default
private List<AssignableRolePlace> places = new ArrayList<>();
}

View File

@@ -1,19 +0,0 @@
package dev.sheldan.abstracto.assignableroles.models.templates;
import dev.sheldan.abstracto.core.models.FullEmote;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import net.dv8tion.jda.api.entities.Role;
@Getter
@Setter
@Builder
public class AssignablePostConfigRole {
private String description;
private Integer position;
private Boolean inline;
private FullEmote emote;
private Role awardedRole;
}

View File

@@ -7,11 +7,24 @@ import lombok.Setter;
import java.util.List;
/**
* Model used to render the {@link AssignableRolePlace place}
*/
@Getter
@Setter
@Builder
public class AssignablePostMessage {
public class AssignablePostMessage {
/**
* The {@link AssignableRolePlace place} to render
*/
private AssignableRolePlace place;
/**
* The awarded {@link AssignablePostRole roles} for this {@link AssignableRolePlace place}
*/
private List<AssignablePostRole> roles;
/**
* The highest number of the position within the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole} of the
* {@link AssignableRolePlace place}
*/
private Integer maxPosition;
}

View File

@@ -6,14 +6,35 @@ import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
/**
* The model which is used to render the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost post}
* to a {@link dev.sheldan.abstracto.core.templating.model.MessageToSend messageToSend} for one individual {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole role}
*/
@Getter
@Setter
@Builder
public class AssignablePostRole {
/**
* The {@link FullEmote emote} to be used in the field
*/
private FullEmote emote;
/**
* The description to be used in the field
*/
private String description;
/**
* The relative position within the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place} of this
* {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole role}
*/
private Integer position;
/**
* Whether or not this field should be at the beginning of a new {@link net.dv8tion.jda.api.entities.Message message}
* to be send
*/
@Builder.Default
private Boolean forceNewMessage = false;
/**
* Whether or not the field should be displayed inline
*/
private Boolean inline;
}

View File

@@ -7,10 +7,19 @@ import lombok.Setter;
import java.util.List;
/**
* This model is used as a container to display the configuration of an {@link AssignableRolePlace place}
*/
@Getter
@Setter
@Builder
public class AssignableRolePlaceConfig {
/**
* The {@link AssignableRolePlace place} to show the config of
*/
private AssignableRolePlace place;
private List<AssignablePostConfigRole> roles;
/**
* The {@link AssignableRolePlaceConfig roles} which are contained in this {@link AssignableRolePlace}
*/
private List<AssignableRolePlaceConfigRole> roles;
}

View File

@@ -0,0 +1,38 @@
package dev.sheldan.abstracto.assignableroles.models.templates;
import dev.sheldan.abstracto.core.models.FullEmote;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import net.dv8tion.jda.api.entities.Role;
/**
* Model used to display the configuration of an individual {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole}
* within an {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace place}
*/
@Getter
@Setter
@Builder
public class AssignableRolePlaceConfigRole {
/**
* The description used for the field for this {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole role}
*/
private String description;
/**
* The position of this {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole role}
*/
private Integer position;
/**
* Whether or not the field is displayed inline
*/
private Boolean inline;
/**
* The {@link FullEmote emote} which is associated with this {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole role}
*/
private FullEmote emote;
/**
* The {@link dev.sheldan.abstracto.core.models.database.ARole} which is given/removed upon reacting with the emote
*/
private Role awardedRole;
}

View File

@@ -1,72 +1,425 @@
package dev.sheldan.abstracto.assignableroles.service;
import dev.sheldan.abstracto.assignableroles.config.AssignableRolePlaceParameterKey;
import dev.sheldan.abstracto.assignableroles.exceptions.EmoteNotInAssignableRolePlaceException;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRole;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost;
import dev.sheldan.abstracto.assignableroles.models.database.AssignedRoleUser;
import dev.sheldan.abstracto.core.command.exception.AbstractoTemplatedException;
import dev.sheldan.abstracto.core.exception.ChannelNotInGuildException;
import dev.sheldan.abstracto.core.models.FullEmote;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AEmote;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageReaction;
import net.dv8tion.jda.api.entities.TextChannel;
import java.util.concurrent.CompletableFuture;
/**
* Service responsible to manage actions on an {@link AssignableRolePlace assignableRolePlace}
*/
public interface AssignableRolePlaceService {
void createAssignableRolePlace(AServer server, String name, AChannel channel, String text);
/**
* Creates a {@link AssignableRolePlace place} with the given parameters
* @param name The key of the {@link AssignableRolePlace} to be used
* @param channel The {@link AChannel channel} in which the posts of this {@link AssignableRolePlace place} should be posted, also determines
* the {@link AServer server} in which the place should reside in. This {@link AServer server} needs to be unique in combination with the
* key
* @param text The description of the {@link AssignableRolePlace place} which is displayed in the first post of the place
* @throws dev.sheldan.abstracto.assignableroles.exceptions.AssignableRoleAlreadyDefinedException if the combination of {@link AServer server}
* and {@link AssignableRolePlace#key} already exists
*/
void createAssignableRolePlace(String name, AChannel channel, String text);
/**
* Whether or not the {@link AssignableRolePlace place} already has the given {@link AEmote emote} as an {@link AssignableRole role}
* with the given {@link AEmote emote}
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} should be in
* @param placeName The key of an {@link AssignableRolePlace place} to check
* @param emote The {@link AEmote emote} which is sought after
* @return Whether or not the {@link AssignableRolePlace place} already has an {@link AssignableRole role}
* with the associated {@link AEmote emote}
*/
boolean hasAssignableRolePlaceEmote(AServer server, String placeName, AEmote emote);
/**
* Whether or not the {@link AssignableRolePlace place} (identified by {@link AServer server} and placeName) already has
* the given {@link AEmote emote} as an {@link AssignableRole role}
* with the given {@link AEmote emote}
* @param place The {@link AssignableRolePlace place} in which the {@link AEmote emote} should be sought in
* @param emote The {@link AEmote emote} which is sought after
* @return Whether or not the {@link AssignableRolePlace place} already has an {@link AssignableRole role}
* with the associated {@link AEmote emote}
*/
boolean hasAssignableRolePlaceEmote(AssignableRolePlace place, AEmote emote);
/**
* Whether or not the {@link AssignableRolePlace place} has the position used. Each of the {@link AssignableRole roles}
* in the {@link AssignableRolePlace place} has a position in which it is posted. This is used to order the roles when posting them. This position is an {@link Integer integer}
* and it is not guaranteed every position up until the amount of {@link AssignableRole roles} is used.
* There can be spots, it is only used as an ordering and to swap/move {@link AssignableRole roles}
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} should be in
* @param placeName The key of the {@link AssignableRolePlace place}
* @param position The position of an {@link AssignableRole role} within the place
* @return Whether or not there exists an {@link AssignableRole role} within the
* {@link AssignableRolePlace place} identified by the {@link AServer server} and key
*/
boolean isPositionUsed(AServer server, String placeName, Integer position);
/**
* Sets the {@link AssignableRole role} identified by the
* {@link FullEmote emote} to the given {@link Integer position} in the {@link AssignableRolePlace place} identified by the
* {@link AServer server} and key.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} should be
* @param placeName The key of the {@link AssignableRolePlace place}
* @param emote The {@link FullEmote emote} which identifies the {@link AssignableRole role}
* which position should be changed
* @param position The new position of the {@link AssignableRole role}
* within the place
* @throws AbstractoTemplatedException if the position is already used or the {@link FullEmote emote} has not associated {@link AssignableRole role}
* within the place
*/
void setEmoteToPosition(AServer server, String placeName, FullEmote emote, Integer position);
/**
* Adds the given {@link ARole role} to the {@link AssignableRolePlace place} identified by the {@link AServer server} and key
* in the form as a {@link AssignableRole role}. This role is identified
* by the given {@link FullEmote emote} and has the description. If the {@link AssignableRolePlace place} is already setup, this will
* try to update the message: adding the field and the reaction. This might not always work e.g. the reaction limit was reached.
* If the update is successful, it will also store the updates in the database.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is present
* @param placeName The key of the {@link AssignableRolePlace place}
* @param role The {@link ARole role} which should be awarded when reacting with the given {@link FullEmote emote}
* @param emote The {@link FullEmote emote} which should be placed on the {@link Message message}
* to react to and identify the {@link AssignableRole}
* for this place
* @param description The description which will be displayed in the {@link Message message}
* which has the reactions
* @throws dev.sheldan.abstracto.core.exception.EmoteNotUsableException in case the {@link net.dv8tion.jda.api.entities.Emote emote}
* cannot be used by the current user. This might be an issue, if its from an external {@link net.dv8tion.jda.api.entities.Guild}
* the user is not a part of.
* @return A {@link CompletableFuture future} when the {@link Message message} has been updated
* and the {@link MessageReaction reaction} has been added. Only tries to execute this, in case there
* {@link AssignableRolePlacePost posts} known.
*/
CompletableFuture<Void> addRoleToAssignableRolePlace(AServer server, String placeName, ARole role, FullEmote emote, String description);
/**
* Removes the given {@link AssignableRole role} from the {@link AssignableRolePlace place}
* identified by the {@link AServer server} and the key. The {@link AssignableRole role} is identified
* by the {@link FullEmote emote} which is associated to it. If there are already {@link AssignableRolePlacePost posts}
* for this {@link AssignableRolePlace place} this will remove the {@link MessageReaction reaction} and
* edit the {@link Message message}. If not, the method returns immediately. If the update
* was successful or was not necessary, the data will be updated in the database as well.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param placeName The key of the {@link AssignableRolePlace place} to remove an {@link AssignableRole role}
* from
* @param emote The {@link FullEmote fullEmote} which has an {@link AEmote emote} to identify the {@link AssignableRole role}
* to remove
* @return A {@link CompletableFuture future} which completes when the {@link MessageReaction reaction}
* was removed and the {@link Message message} updated and the database updated, or, if no post is present, only if the database was updated
*/
CompletableFuture<Void> removeRoleFromAssignableRolePlace(AServer server, String placeName, FullEmote emote);
/**
* This method is used to setup the {@link AssignableRolePlacePost} for an
* {@link AssignableRolePlace place}. If there are previous {@link AssignableRolePlacePost posts},
* this method will delete them first. This method does not do the rendering, but is only the entry point for the process.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key which identifies the {@link AssignableRolePlace place} within the {@link AServer server}
* @return A {@link CompletableFuture future} which completes if the complete setup is finished successfully.
*/
CompletableFuture<Void> setupAssignableRolePlace(AServer server, String name);
/**
* This method is used to update the {@link AssignableRolePlacePost posts}
* of the {@link AssignableRolePlace place} identified by the {@link AServer server} and the key. This
* effectively re-renders the template for an {@link AssignableRolePlace place} and updates the existing
* {@link AssignableRolePlacePost posts} accordingly. In case there are no posts, this method returns immediately.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place}
* @throws ChannelNotInGuildException if the channel which contained the posts does not exist anymore
* @return A {@link CompletableFuture future} which completes successfully if *every* update was successful.
*/
CompletableFuture<Void> refreshAssignablePlacePosts(AServer server, String name);
/**
* This method is used to update the {@link AssignableRolePlacePost posts}
* of the {@link AssignableRolePlace place}. This effectively re-renders the template for an
* {@link AssignableRolePlace place} and updates the existing {@link AssignableRolePlacePost posts} accordingly.
* In case there are no posts, this method returns immediately.
* @param place The {@link AssignableRolePlace place} which should have their {@link Message messages} were updated
* @throws ChannelNotInGuildException if the channel which contained the posts does not exist anymore
* @return A {@link CompletableFuture future} which completes successfully if *every* update was successful.
*/
CompletableFuture<Void> refreshAssignablePlacePosts(AssignableRolePlace place);
/**
* This method updates the first {@link AssignableRolePlacePost posts} which contains the description of the
* {@link AssignableRolePlace place}. If there are no posts, this method will return immediately
* @param place The {@link AssignableRolePlace place} to update the
* @throws ChannelNotInGuildException if the channel which contained the post does not exist anymore
* @return A {@link CompletableFuture future} which completes when the update of the {@link Message message}
* was completed
*/
CompletableFuture<Void> refreshTextFromPlace(AssignableRolePlace place);
/**
* Sets the active attribute of the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place}
* @param newValue The new vale of the active attribute
*/
void setAssignablePlaceActiveTo(AServer server, String name, Boolean newValue);
/**
* Activates the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place} to activate
*/
void activateAssignableRolePlace(AServer server, String name);
/**
* Activates the {@link AssignableRolePlace place}, which means that the activate attribute will be set to true
* @param place The {@link AssignableRolePlace place} to activate
*/
void activateAssignableRolePlace(AssignableRolePlace place);
/**
* De-activates the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place} to de-activate
*/
void deactivateAssignableRolePlace(AServer server, String name);
/**
* De-activates the {@link AssignableRolePlace place}, which means that the activate attribute will be set to false
* @param place The {@link AssignableRolePlace place} to de-activate
*/
void deactivateAssignableRolePlace(AssignableRolePlace place);
// inline attribute
/**
* Sets the inline attribute of the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key. This method will update any existing {@link AssignableRolePlacePost posts} of the place, if there
* are any.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place}
* @param newValue The new vale of the inline attribute
*/
CompletableFuture<Void> setAssignablePlaceInlineTo(AServer server, String name, Boolean newValue);
/**
* Sets the inline attribute of the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key to true. This method will update any existing {@link AssignableRolePlacePost posts} of the place, if there
* are any.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place} to inline
*/
CompletableFuture<Void> inlineAssignableRolePlace(AServer server, String name);
/**
* Sets the inline attribute of the {@link AssignableRolePlace place} to true. This method will update any existing
* {@link AssignableRolePlacePost posts} of the place, if there are any.
* @param place The {@link AssignableRolePlace place} to inline
*/
CompletableFuture<Void> inlineAssignableRolePlace(AssignableRolePlace place);
/**
* Sets the inline attribute of the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key to false. This method will update any existing {@link AssignableRolePlacePost posts} of the place, if there
* are any.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place} to spread
*/
CompletableFuture<Void> spreadAssignableRolePlace(AServer server, String name);
/**
* Sets the inline attribute of the {@link AssignableRolePlace place} to false. This method will update any existing
* {@link AssignableRolePlacePost posts} of the place, if there are any.
* @param place The {@link AssignableRolePlace place} to spread
*/
CompletableFuture<Void> spreadAssignableRolePlace(AssignableRolePlace place);
// unique attribute
/**
* Sets the unique attribute of the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place}
* @param newValue The new vale of the unique attribute
*/
void setAssignablePlaceUniqueTo(AServer server, String name, Boolean newValue);
/**
* Sets the unique attribute of the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key to true.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place} to activate unique role handling
*/
void uniqueAssignableRolePlace(AServer server, String name);
/**
* Sets the unique attribute of the {@link AssignableRolePlace place} to true.
* @param place The {@link AssignableRolePlace place} to activate unique role handling
*/
void uniqueAssignableRolePlace(AssignableRolePlace place);
/**
* Sets the unique attribute of the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key to false.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place} to disable unique role handling
*/
void multipleAssignableRolePlace(AServer server, String name);
/**
* Sets the unique attribute of the {@link AssignableRolePlace place} to false.
* @param place The {@link AssignableRolePlace place} to disable unique role handling
*/
void multipleAssignableRolePlace(AssignableRolePlace place);
// auto remove attribute
/**
* Sets the auto-remove attribute of the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place}
* @param newValue The new vale of the auto-remove attribute
*/
void setAssignablePlaceAutoRemoveTo(AServer server, String name, Boolean newValue);
/**
* Sets the auto-remove attribute of the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key to true.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place} to activate the automatic removal of reactions
*/
void autoRemoveAssignableRolePlace(AServer server, String name);
/**
* Sets the auto-remove attribute of the {@link AssignableRolePlace place} to true.
* @param place The {@link AssignableRolePlace place} to activate the automatic removal of reactions
*/
void autoRemoveAssignableRolePlace(AssignableRolePlace place);
/**
* Sets the auto-remove attribute of the {@link AssignableRolePlace place} identified by {@link AServer server}
* and the key to false.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place} to keep reactions
*/
void keepReactionsAssignableRolePlace(AServer server, String name);
/**
* Sets the auto-remove attribute of the {@link AssignableRolePlace place} to false.
* @param place The {@link AssignableRolePlace place} to keep reactions
*/
void keepReactionsAssignableRolePlace(AssignableRolePlace place);
/**
* Swaps the positions of the two {@link AssignableRole roles} identified by the respective
* {@link FullEmote emote}. This will only update the it in the database, and require a new setup. The
* two {@link FullEmote emotes} are assumed to be different
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place}
* @param firstEmote The {@link FullEmote emote} which identifies the first {@link AssignableRole role} to swap
* @param secondEmote The {@link FullEmote emote} which identifies the second {@link AssignableRole role} to swap
* @throws EmoteNotInAssignableRolePlaceException if either one of them is not part of the {@link AssignableRolePlace place}
*
*/
void swapPositions(AServer server, String name, FullEmote firstEmote, FullEmote secondEmote);
/**
* This method renders the {@link AssignableRolePlace place} and post the created {@link MessageToSend messageToSend}
* in the current channel.
* @param server The {@link AServer server} of the {@link AssignableRolePlace place} to test
* @param name The key of the {@link AssignableRolePlace place}
* @param channel The {@link TextChannel channel} in which the resulting {@link MessageToSend messageToSend}
* should be posted in
* @return A {@link CompletableFuture future} which completes when all {@link MessageToSend messageToSend}
* have been sent
*/
CompletableFuture<Void> testAssignableRolePlace(AServer server, String name, TextChannel channel);
void showAssignablePlaceConfig(AServer server, String name, TextChannel channel);
void moveAssignableRolePlace(AServer server, String name, TextChannel newChannel);
void changeAssignablePlaceDescription(AServer server, String name, String newDescription);
/**
* This method renders the configuration of the {@link AssignableRolePlace place} identified by {@link AServer server}
* and key into an {@link MessageToSend messageToSend} and sends a message to the given {@link TextChannel channel}.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place}
* @param channel The {@link TextChannel channel} in which the resulting {@link MessageToSend messageToSend} should be posted in
* @return A {@link CompletableFuture future} which completes when the {@link MessageToSend} has been sent
*/
CompletableFuture<Void> showAssignablePlaceConfig(AServer server, String name, TextChannel channel);
/**
* Changes the {@link AChannel channel} of the {@link AssignableRole place} identified by {@link AServer server}
* and key to the given {@link TextChannel channel}. This only changes the configuration and does not impact
* any currently posted {@link AssignableRolePlacePost posts}
* @param name The key of the {@link AssignableRolePlace place}
* @param newChannel The {@link TextChannel channel} where the {@link AssignableRolePlace place} should be posted to.
*/
void moveAssignableRolePlace(String name, TextChannel newChannel);
/**
* Deletes the {@link AssignableRolePlace place} identified by {@link AServer server} and key. This method will first
* delete all the {@link AssignableRolePlacePost posts} and then remove the references from the database.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place}
* @return A {@link CompletableFuture future} which completes after all posts have been deleted
*/
CompletableFuture<Void> deleteAssignableRolePlace(AServer server, String name);
CompletableFuture<Void> changeText(AServer server, String name, String newText);
/**
* Changes the text of an {@link AssignableRolePlace place} in the database only.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place}
* @param newText The new value for the text attribute displayed in the first {@link AssignableRolePlacePost post}
*/
void changeText(AServer server, String name, String newText);
/**
* Changes the text of an {@link AssignableRolePlace place} in the database, and updates the first
* {@link AssignableRolePlacePost post}. The update of the post happens after the change in the database.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place}
* @param newText The new value for the text attribute displayed in the first {@link AssignableRolePlacePost post}
*/
CompletableFuture<Void> changeTextAsync(AServer server, String name, String newText);
/**
* Removes the reactions and the roles from the user from the place, this does not touch the stored data
* @param place The {@link AssignableRolePlace} to which remove the existing reactions and roles from
* @param user The {@link AssignedRoleUser} to the remove *all* reactions and assigned roles from
* @return A {@link CompletableFuture} which completes when both of these actions have been done for all {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRole}
* @param place The {@link AssignableRolePlace place} to which remove the existing reactions and roles from
* @param user The {@link AssignedRoleUser user} to the remove *all* reactions and assigned roles from
* @return A {@link CompletableFuture future} which completes when both of these actions have been done for all {@link AssignableRole assignableRoles}
*/
CompletableFuture<Void> removeExistingReactionsAndRoles(AssignableRolePlace place, AssignedRoleUser user);
/**
* Changes the configuration of the given {@link AssignableRolePlaceParameterKey key} to the given value of the
* {@link AssignableRolePlace place} identified by {@link AServer server} and key. If it can be updated immediately,
* this will update the {@link AssignableRolePlacePost posts} and the return {@link CompletableFuture future}
* will return afterwards. This is the case for {@link AssignableRolePlaceParameterKey#INLINE}. The rest of the
* keys will only update the configuration in the database, and the place needs a refresh at a later point.
* @param server The {@link AServer server} in which the {@link AssignableRolePlace place} is
* @param name The key of the {@link AssignableRolePlace place}
* @param keyToChange The {@link AssignableRolePlaceParameterKey key} to change
* @param newValue The new value of the attribute, but be able to convert to a boolean via {@link org.apache.commons.lang3.BooleanUtils#toBooleanObject(String)}
* @return A {@link CompletableFuture future} which completes when the configuration has been completed
*/
CompletableFuture<Void> changeConfiguration(AServer server, String name, AssignableRolePlaceParameterKey keyToChange, Object newValue);
/**
* Retrieves all {@link AssignableRolePlace places}, renders them into {@link MessageToSend messageToSend} and sends
* this message to the given {@link TextChannel channel}.
* @param server The {@link AServer server} for which the {@link AssignableRolePlace places} should be shown for
* @param channel The {@link TextChannel channel} to send the {@link MessageToSend messageToSend} in
* @return A {@link CompletableFuture future} which completes when the message has been sent
*/
CompletableFuture<Void> showAllAssignableRolePlaces(AServer server, TextChannel channel);
}

View File

@@ -8,17 +8,107 @@ import net.dv8tion.jda.api.entities.Member;
import java.util.concurrent.CompletableFuture;
/**
* Service responsible to manage actions on an {@link AssignableRole assignableRole}
*/
public interface AssignableRoleService {
CompletableFuture<Void> assignAssignableRoleToUser(Long assignableRoleId, Member toAdd);
/**
* Adds the given {@link AssignableRole assignableRole} to the given {@link Member member}
* @param assignableRoleId The ID of the {@link AssignableRole} to add
* @param member The {@link Member member} who should receive the {@link AssignableRole role}
* @return A {@link CompletableFuture future} which completes when the {@link net.dv8tion.jda.api.entities.Role role} was added to the {@link Member member}
*/
CompletableFuture<Void> assignAssignableRoleToUser(Long assignableRoleId, Member member);
/**
* Adds the given {@link AssignableRole assignableRole} to the given {@link ServerUser serverUser}
* @param assignableRoleId The ID of the {@link AssignableRole} to add
* @param serverUser The {@link ServerUser serverUser} who should receive the {@link AssignableRole role}
* @return A {@link CompletableFuture future} which completes when the {@link net.dv8tion.jda.api.entities.Role role} was added to the {@link ServerUser serverUser}
*/
CompletableFuture<Void> assignAssignableRoleToUser(Long assignableRoleId, ServerUser serverUser);
/**
* Clears all {@link AssignableRole assignableRoles} which are currently given to the {@link AUserInAServer user} of a certain
* {@link AssignableRolePlace place}
* @param place The {@link AssignableRolePlace place} from which all {@link AssignableRole roles} from an {@link AUserInAServer user} should be removed of
* @param user The {@link AUserInAServer user} to remove the {@link AssignableRole assignableRoles} of
*/
void clearAllRolesOfUserInPlace(AssignableRolePlace place, AUserInAServer user);
/**
* Assigns the given {@link AssignableRole role} to the {@link Member member} and stores the assignment as a
* {@link dev.sheldan.abstracto.assignableroles.models.database.AssignedRoleUser}
* @param assignableRoleId The ID of an {@link AssignableRole assignableRole} to be added to the {@link Member member}
* @param toAdd The {@link Member member} to add the role to
* @return A {@link CompletableFuture future} which completes when the {@link net.dv8tion.jda.api.entities.Role role} has
* been added and the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignedRoleUser user} has been persisted
*/
CompletableFuture<Void> fullyAssignAssignableRoleToUser(Long assignableRoleId, Member toAdd);
/**
* Removes the {@link AssignableRole role} from the given {@link Member member}
* @param assignableRole The {@link AssignableRole role} to remove
* @param member The {@link Member member} to remove the {@link AssignableRole role} from
* @return A {@link CompletableFuture future} which completes when the {@link net.dv8tion.jda.api.entities.Role role}
* has been removed from the {@link Member member}
*/
CompletableFuture<Void> removeAssignableRoleFromUser(AssignableRole assignableRole, Member member);
/**
* Removes the {@link AssignableRole role} from the given {@link Member member}
* @param assignableRoleId The ID of an {@link AssignableRole role} to remove
* @param member The {@link Member member} to remove the {@link AssignableRole role} from
* @return A {@link CompletableFuture future} which completes when the {@link net.dv8tion.jda.api.entities.Role role}
* has been removed from the {@link Member member}
*/
CompletableFuture<Void> removeAssignableRoleFromUser(Long assignableRoleId, Member member);
/**
* Removes the {@link AssignableRole role} from the given {@link AUserInAServer aUserInAServer}
* @param assignableRole The {@link AssignableRole role} to remove
* @param aUserInAServer The {@link AUserInAServer aUserInAServer} to remove the {@link AssignableRole role} from
* @return A {@link CompletableFuture future} which completes when the {@link net.dv8tion.jda.api.entities.Role role}
* has been removed from the {@link AUserInAServer aUserInAServer}
*/
CompletableFuture<Void> removeAssignableRoleFromUser(AssignableRole assignableRole, AUserInAServer aUserInAServer);
/**
* Removes the given {@link AssignableRole role} from the {@link ServerUser serverUser} and stores the assignment as a
* {@link dev.sheldan.abstracto.assignableroles.models.database.AssignedRoleUser}
* @param assignableRole The {@link AssignableRole assignableRole} to be removed from the {@link ServerUser serverUser}
* @param serverUser The {@link ServerUser serverUser} to remove the role from
* @return A {@link CompletableFuture future} which completes when the {@link net.dv8tion.jda.api.entities.Role role} has
* been removed and the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignedRoleUser user} has been persisted
*/
CompletableFuture<Void> fullyRemoveAssignableRoleFromUser(AssignableRole assignableRole, ServerUser serverUser);
/**
* Adds the {@link AssignableRole assignableRole} to the given {@link AUserInAServer userInAServer} in the database
* @param assignableRoleId The ID of the {@link AssignableRole role} to be added
* @param aUserInAServer The {@link AUserInAServer user} to get the {@link AssignableRole role}
*/
void addRoleToUser(Long assignableRoleId, AUserInAServer aUserInAServer);
/**
* Adds the {@link AssignableRole assignableRole} to the given {@link AUserInAServer userInAServer} in the database
* @param assignableRole The {@link AssignableRole role} to be added
* @param aUserInAServer The {@link AUserInAServer user} to get the {@link AssignableRole role}
*/
void addRoleToUser(AssignableRole assignableRole, AUserInAServer aUserInAServer);
/**
* Removes the {@link AssignableRole assignableRole} from the given {@link AUserInAServer userInAServer} in the database
* @param assignableRole The {@link AssignableRole role} to be removed
* @param aUserInAServer The {@link AUserInAServer user} to get the {@link AssignableRole role} removed
*/
void removeRoleFromUser(AssignableRole assignableRole, AUserInAServer aUserInAServer);
/**
* Removes the {@link AssignableRole assignableRole} from the given {@link AUserInAServer userInAServer} in the database
* @param assignableRoleId The ID of the {@link AssignableRole role} to be removed
* @param aUserInAServer The {@link AUserInAServer user} to get the {@link AssignableRole role} removed
*/
void removeRoleFromUser(Long assignableRoleId, AUserInAServer aUserInAServer);
}

View File

@@ -6,13 +6,62 @@ import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace
import dev.sheldan.abstracto.core.models.cache.CachedEmote;
import dev.sheldan.abstracto.core.models.database.AEmote;
import dev.sheldan.abstracto.core.models.database.ARole;
import net.dv8tion.jda.api.entities.MessageReaction;
/**
* Management service for the table of {@link AssignableRole assignableRoles}
*/
public interface AssignableRoleManagementService {
/**
* Adds the given {@link ARole role} to the {@link AssignableRolePlace place} to be identified with the given {@link AEmote emote}
* and displayed with the given description. An optional {@link AssignableRolePlacePost post} can be provided, if the
* place has already been setup.
* @param place The {@link AssignableRolePlace place} to add the the {@link ARole role} to
* @param emote The {@link AEmote emote} which is used as an reaction on the {@link AssignableRolePlacePost post}
* @param role The {@link ARole role} which should be given to the {@link dev.sheldan.abstracto.core.models.database.AUserInAServer user} placing a reaction
* @param description The description which should be displayed in the field for the given {@link ARole role}
* @param post If a {@link AssignableRolePlacePost post} already exists, it can be provided to link the newly created {@link AssignableRole directly}
* @return The created instance of the {@link AssignableRole assignableRole} according to the given parameters
*/
AssignableRole addRoleToPlace(AssignableRolePlace place, AEmote emote, ARole role, String description, AssignableRolePlacePost post);
/**
* Adds the {@link ARole role} (identified by its ID) to the {@link AssignableRolePlace place} (identified by its ID),
* which in turn is identified by the given {@link AEmote emote} (identified by its ID) and displayed with the given description.
* An optional {@link AssignableRolePlacePost post} can be provided (identified by the ID of the {@link net.dv8tion.jda.api.entities.Message}), if
* it already exists
* @param placeId The ID of the {@link AssignableRolePlace} to add an {@link AssignableRole assignableRole} to
* @param emoteId The ID of the {@link AEmote emote} which should be used as an reaction on the {@link AssignableRolePlacePost post}
* @param roleId The ID of the {@link ARole role} which should be given to the {@link dev.sheldan.abstracto.core.models.database.AUserInAServer user} placing a reaction
* @param description The description which should be displayed in the field for the given {@link ARole role}
* @param messageId If provided, this message ID will be used to identify the {@link AssignableRolePlacePost post} which already exists
* @return The created instance of the {@link AssignableRole assignableRole} according to the given parameters
*/
AssignableRole addRoleToPlace(Long placeId, Integer emoteId, Long roleId, String description, Long messageId);
/**
* Adds the {@link ARole role} (identified by its ID) to the {@link AssignableRolePlace place} (identified by its ID),
* which in turn is identified by the given {@link AEmote emote} (identified by its ID) and displayed with the given description.
* @param placeId The ID of the {@link AssignableRolePlace} to add an {@link AssignableRole assignableRole} to
* @param emoteId The ID of the {@link AEmote emote} which should be used as an reaction on the {@link AssignableRolePlacePost post}
* @param roleId The ID of the {@link ARole role} which should be given to the {@link dev.sheldan.abstracto.core.models.database.AUserInAServer user} placing a reaction
* @param description The description which should be displayed in the field for the given {@link ARole role}
* @return The created instance of the {@link AssignableRole assignableRole} according to the given parameters
*/
AssignableRole addRoleToPlace(Long placeId, Integer emoteId, Long roleId, String description);
/**
* Finds the {@link AssignableRole} given by the ID and returns it if found. Throws an exception otherwise.
* @param assignableRoleId The ID Of the {@link AssignableRole assignableRole} to find
* @return An instance of {@link AssignableRole assignableRole} if it exists for the given ID
*/
AssignableRole getByAssignableRoleId(Long assignableRoleId);
AssignableRole getRoleForReactionEmote(MessageReaction.ReactionEmote emote, AssignableRolePlace assignableRolePlace);
/**
* Returns the respective {@link AssignableRole assignableRole} for the {@link CachedEmote emote} which is part of the
* {@link AssignableRolePlace place}. It will throw an exception, if the {@link CachedEmote emote} is not used.
* @param cachedEmote
* @param assignableRolePlace
* @return
*/
AssignableRole getRoleForReactionEmote(CachedEmote cachedEmote, AssignableRolePlace assignableRolePlace);
}

View File

@@ -7,15 +7,82 @@ import dev.sheldan.abstracto.core.models.database.AServer;
import java.util.List;
import java.util.Optional;
/**
* Management service for {@link AssignableRolePlace place} table
*/
public interface AssignableRolePlaceManagementService {
AssignableRolePlace createPlace(AServer server, String name, AChannel channel, String text);
/**
* Creates an {@link AssignableRolePlace place} with the given attributes
* @param name The key of the {@link AssignableRolePlace place} to identify it with
* @param channel The {@link AChannel channel} in which the {@link dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost posts}
* should be created in
* @param text The text which should be shown in the description
* @return The {@link AssignableRolePlace place} which was created
*/
AssignableRolePlace createPlace(String name, AChannel channel, String text);
/**
* Whether or not a place with the key exists in the given {@link AServer server}
* @param server The {@link AServer server} in which it should be searched
* @param name The key of an {@link AssignableRolePlace place} which should be searched
* @return Whether or not an {@link AssignableRolePlace place} with the key exists in the {@link AServer server}
*/
boolean doesPlaceExist(AServer server, String name);
/**
* Retrieves an {@link AssignableRolePlace place} identified by the given key in the {@link AServer server}
* @param server The {@link AServer server} to search in
* @param name The key of the {@link AssignableRolePlace place} to search for
* @throws dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceNotFoundException if not found
* @return Returns an instance of {@link AssignableRolePlace place}, if it was found
*/
AssignableRolePlace findByServerAndKey(AServer server, String name);
/**
* Retrieves an {@link AssignableRolePlace place} via its ID in an {@link Optional optional}
* @param id The ID to search for
* @return Returns an {@link Optional optional} with an instance of {@link AssignableRolePlace place}, if it was found
* and empty otherwise
*/
Optional<AssignableRolePlace> findByPlaceIdOptional(Long id);
/**
* Retrieves an {@link AssignableRolePlace place} via its ID
* @param id The ID to search for
* @throws dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlaceNotFoundException if not found
* @return Returns an {@link AssignableRolePlace place} if one was found with this ID
*/
AssignableRolePlace findByPlaceId(Long id);
void moveAssignableRolePlace(AServer server, String name, AChannel newChannel);
/**
* Changes the {@link AChannel channel} in which a {@link AssignableRolePlace place} should be posted towards
* when being setup.
* @param name The key of the {@link AssignableRolePlace place} to move
* @param newChannel The new {@link AChannel channel} where the place should be moved to, used to determine in which {@link AServer server}
* the {@link AssignableRolePlace place} is
*/
void moveAssignableRolePlace(String name, AChannel newChannel);
/**
* Changes the {@link AssignableRolePlace#text description} of an assignable role place
* @param server The {@link AServer server} in which to look for the place
* @param name The key of the {@link AssignableRolePlace place} to change the descripiont for
* @param newDescription The description value which should be used from now on
*/
void changeAssignableRolePlaceDescription(AServer server, String name, String newDescription);
/**
* Deletes the {@link AssignableRolePlace place}
* @param place The {@link AssignableRolePlace place} to remove
*/
void deleteAssignablePlace(AssignableRolePlace place);
/**
* Retrieves all {@link AssignableRolePlace assignableRolePlaces} for the given {@link AServer server}
* @param server The {@link AServer server} for whom the places should be retrieved for
* @return All {@link AssignableRolePlace assignableRolePlaces} in the given {@link AServer server}
*/
List<AssignableRolePlace> findAllByServer(AServer server);
}

View File

@@ -1,12 +1,37 @@
package dev.sheldan.abstracto.assignableroles.service.management;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignableRolePlacePostNotFoundException;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlace;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRolePlacePost;
import java.util.Optional;
/**
* Management service for {@link AssignableRolePlacePost post} table
*/
public interface AssignableRolePlacePostManagementService {
/**
* Finds a {@link AssignableRolePlacePost post} via the ID of the {@link net.dv8tion.jda.api.entities.Message} it was
* posted
* @param messageId The ID of the {@link net.dv8tion.jda.api.entities.Message} in which the
* @return An {@link Optional optional} containing the {@link AssignableRolePlacePost post}, if one was found, empty otherwise
*/
Optional<AssignableRolePlacePost> findByMessageIdOptional(Long messageId);
/**
* Finds a {@link AssignableRolePlacePost post} via the ID of the {@link net.dv8tion.jda.api.entities.Message} it was
* posted
* @param messageId The ID of the {@link net.dv8tion.jda.api.entities.Message} in which the
* @throws AssignableRolePlacePostNotFoundException if it was not found
* @return The {@link AssignableRolePlacePost post} if one existed with this ID
*/
AssignableRolePlacePost findByMessageId(Long messageId);
/**
* Creates an {@link AssignableRolePlacePost post} for the given {@link AssignableRolePlace place} in the given message ID
* @param updatedPlace The {@link AssignableRolePlace place} this post should be part of
* @param messageId The ID of the message in which the post exists
* @return
*/
AssignableRolePlacePost createAssignableRolePlacePost(AssignableRolePlace updatedPlace, Long messageId);
}

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.assignableroles.service.management;
import dev.sheldan.abstracto.assignableroles.exceptions.AssignedUserNotFoundException;
import dev.sheldan.abstracto.assignableroles.models.database.AssignableRole;
import dev.sheldan.abstracto.assignableroles.models.database.AssignedRoleUser;
import dev.sheldan.abstracto.core.models.ServerUser;
@@ -7,14 +8,60 @@ import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import java.util.Optional;
/**
* Management service for {@link AssignedRoleUser assignedRoleUser} table
*/
public interface AssignedRoleUserManagementService {
/**
* Adds the given {@link AssignableRole assignableRole} to the given {@link AUserInAServer user}.
* @param assignableRole The {@link AssignableRole assignableRole} to add
* @param aUserInAServer The {@link AUserInAServer user} who should get the {@link AssignableRole role}
*/
void addAssignedRoleToUser(AssignableRole assignableRole, AUserInAServer aUserInAServer);
/**
* Removes the given {@link AssignableRole assignableFrom} from the given {@link AUserInAServer user}.
* @param assignableRole The {@link AssignableRole assignableRole} to remove
* @param aUserInAServer The {@link AUserInAServer user} from whom the {@link AssignableRole role} should be removed
*/
void removeAssignedRoleFromUser(AssignableRole assignableRole, AUserInAServer aUserInAServer);
/**
* Removes the given {@link AssignableRole assignableFrom} from the given {@link AssignedRoleUser user}.
* @param assignableRole The {@link AssignableRole assignableRole} to remove
* @param user The {@link AssignedRoleUser user} from whom the {@link AssignableRole role} should be removed
*/
void removeAssignedRoleFromUser(AssignableRole assignableRole, AssignedRoleUser user);
/**
* Creating an {@link AssignedRoleUser assignedRoleUser} from the given {@link AUserInAServer user}
* @param aUserInAServer The {@link AUserInAServer user} to create it for
* @return The created instance of {@link AssignedRoleUser assignedRoleUser}
*/
AssignedRoleUser createAssignedRoleUser(AUserInAServer aUserInAServer);
void clearAllAssignedRolesOfUser(AUserInAServer userInAServer);
boolean doesAssignedRoleUserExist(AUserInAServer aUserInAServer);
/**
* Tries to retrieve a {@link AssignedRoleUser assignedRoleUser} from an {@link AUserInAServer userInAServer},
* and returns it as an {@link Optional optional}, if it exists. Empty otherwise.
* @param aUserInAServer The {@link AUserInAServer user} to search for
* @return An {@link Optional optional} containing the {@link AssignedRoleUser user}
*/
Optional<AssignedRoleUser> findByUserInServerOptional(AUserInAServer aUserInAServer);
/**
* Tries to retrieve a {@link AssignedRoleUser assignedRoleUser} from an {@link AUserInAServer userInAServer},
* and returns it as an {@link Optional optional}, if it exists. Empty otherwise.
* @param serverUser The {@link ServerUser user} to search for
* @return An {@link Optional optional} containing the {@link AssignedRoleUser user}
*/
Optional<AssignedRoleUser> findByUserInServerOptional(ServerUser serverUser);
/**
* Tries to retrieve a {@link AssignedRoleUser assignedRoleUser} from an {@link AUserInAServer userInAServer},
* and returns it if it exists. Throws an exception otherwise.
* @param aUserInAServer The {@link AUserInAServer user} to search for
* @throws AssignedUserNotFoundException if it doesnt find any
* @return The {@link AssignedRoleUser user} if it exists
*/
AssignedRoleUser findByUserInServer(AUserInAServer aUserInAServer);
}

View File

@@ -34,7 +34,7 @@ public class ConditionServiceBean implements ConditionService {
}
private void verifyConditionContext(ConditionContextInstance contextInstance, SystemCondition condition) {
for (ConditionContextVariable conditionContextVariable : condition.getExpectedContext().getExpectedVariables()) {
for (ConditionContextVariable conditionContextVariable : condition.getExpectedContext().getRequiredVariables()) {
HashMap<String, Object> providedParameters = contextInstance.getParameters();
if(!providedParameters.containsKey(conditionContextVariable.getName())) {
throw new InvalidConditionParametersException(String.format("Variable %s was not present", conditionContextVariable.getName()));

View File

@@ -10,5 +10,5 @@ import java.util.List;
@Setter
@Builder
public class ConditionContext {
private List<ConditionContextVariable> expectedVariables;
private List<ConditionContextVariable> requiredVariables;
}

View File

@@ -8,11 +8,21 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* A wrapper object which contains a list of {@link CompletableFuture futures} of the same type, and a primary {@link CompletableFuture future} which completes, when *all* of the futures complete
* @param <T> The return value of the individual {@link CompletableFuture futures}
*/
@Getter
@Setter
@Slf4j
public class CompletableFutureList<T> {
/**
* The primary {@link CompletableFuture future} which completes once all futures in the list complete, this will complete erroneously, if any of them do so
*/
private CompletableFuture<Void> mainFuture;
/**
* The list of {@link CompletableFuture futures} which are wrapped and should complete.
*/
private List<CompletableFuture<T>> futures;
public CompletableFutureList(List<CompletableFuture<T>> futures) {
@@ -20,6 +30,10 @@ public class CompletableFutureList<T> {
this.futures = futures;
}
/**
* Returns all results of the {@link CompletableFuture futures}, for those who completed, the ones which were not completed successfully are not returned
* @return A {@link List list} of objects, which were returned by the {@link CompletableFuture futures}.
*/
public List<T> getObjects() {
List<T> result = new ArrayList<>();
futures.forEach(future -> {