mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-28 19:35:23 +00:00
[AB-136] replacing some rare occurrences where fake objects where used for actual database operations
replacing a few sync commands with async commands refactoring parameters in assignable role place service bean fixing emote parameter handler to also consider default emojis removing unused description in command replacing some ARole parameters with Role parameters, so we can be sure they exists added a few TODOs marking exception changes required
This commit is contained in:
@@ -14,6 +14,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class DeleteAssignableRolePlace extends AbstractConditionableCommand {
|
||||
@@ -22,12 +23,12 @@ public class DeleteAssignableRolePlace extends AbstractConditionableCommand {
|
||||
private AssignableRolePlaceService service;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
String name = (String) parameters.get(0);
|
||||
service.deleteAssignableRolePlace(commandContext.getUserInitiatedContext().getServer(), name);
|
||||
return CommandResult.fromSuccess();
|
||||
return service.deleteAssignableRolePlace(commandContext.getUserInitiatedContext().getServer(), name)
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -40,6 +41,7 @@ public class DeleteAssignableRolePlace extends AbstractConditionableCommand {
|
||||
.module(AssignableRoleModule.ASSIGNABLE_ROLES)
|
||||
.templated(true)
|
||||
.causesReaction(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.parameters(parameters)
|
||||
.help(helpInfo)
|
||||
|
||||
@@ -34,7 +34,8 @@ public class EditAssignableRolePlaceText extends AbstractConditionableCommand {
|
||||
String name = (String) parameters.get(0);
|
||||
String newText = (String) parameters.get(1);
|
||||
|
||||
return service.changeText(commandContext.getUserInitiatedContext().getServer(), name, newText).thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
return service.changeText(commandContext.getUserInitiatedContext().getServer(), name, newText)
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class RemoveRoleFromAssignableRolePlace extends AbstractConditionableCommand {
|
||||
@@ -23,13 +24,13 @@ public class RemoveRoleFromAssignableRolePlace extends AbstractConditionableComm
|
||||
private AssignableRolePlaceService service;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
String name = (String) parameters.get(0);
|
||||
FullEmote emote = (FullEmote) parameters.get(1);
|
||||
service.removeRoleFromAssignableRolePlace(commandContext.getUserInitiatedContext().getServer(), name, emote);
|
||||
return CommandResult.fromSuccess();
|
||||
return service.removeRoleFromAssignableRolePlace(commandContext.getUserInitiatedContext().getServer(), name, emote)
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,6 +44,7 @@ public class RemoveRoleFromAssignableRolePlace extends AbstractConditionableComm
|
||||
.module(AssignableRoleModule.ASSIGNABLE_ROLES)
|
||||
.templated(true)
|
||||
.causesReaction(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.parameters(parameters)
|
||||
.help(helpInfo)
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class SetAssignableRolePosition extends AbstractConditionableCommand {
|
||||
@@ -23,14 +24,14 @@ public class SetAssignableRolePosition extends AbstractConditionableCommand {
|
||||
private AssignableRolePlaceService service;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
String name = (String) parameters.get(0);
|
||||
FullEmote emote = (FullEmote) parameters.get(1);
|
||||
Integer newPosition = (Integer) parameters.get(2);
|
||||
service.setEmoteToPosition(commandContext.getUserInitiatedContext().getServer(), name, emote, newPosition);
|
||||
return CommandResult.fromSuccess();
|
||||
return service.setEmoteToPosition(commandContext.getUserInitiatedContext().getServer(), name, emote, newPosition)
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -45,6 +46,7 @@ public class SetAssignableRolePosition extends AbstractConditionableCommand {
|
||||
.module(AssignableRoleModule.ASSIGNABLE_ROLES)
|
||||
.templated(true)
|
||||
.causesReaction(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.parameters(parameters)
|
||||
.help(helpInfo)
|
||||
|
||||
@@ -27,6 +27,7 @@ public class ShowAssignableRolePlaceConfig extends AbstractConditionableCommand
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
String name = (String) parameters.get(0);
|
||||
// TODO refactor to return something to be posted in this command here instead of relying it to be posted somewhere else
|
||||
service.showAssignablePlaceConfig(commandContext.getUserInitiatedContext().getServer(), name, commandContext.getChannel());
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
public class TestAssignableRolePlace extends AbstractConditionableCommand {
|
||||
@@ -22,12 +23,12 @@ public class TestAssignableRolePlace extends AbstractConditionableCommand {
|
||||
private AssignableRolePlaceService service;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
String name = (String) parameters.get(0);
|
||||
service.testAssignableRolePlace(commandContext.getUserInitiatedContext().getServer(), name, commandContext.getChannel());
|
||||
return CommandResult.fromSuccess();
|
||||
return service.testAssignableRolePlace(commandContext.getUserInitiatedContext().getServer(), name, commandContext.getChannel())
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -40,6 +41,7 @@ public class TestAssignableRolePlace extends AbstractConditionableCommand {
|
||||
.module(AssignableRoleModule.ASSIGNABLE_ROLES)
|
||||
.templated(true)
|
||||
.causesReaction(true)
|
||||
.async(true)
|
||||
.supportsEmbedException(true)
|
||||
.parameters(parameters)
|
||||
.help(helpInfo)
|
||||
|
||||
@@ -14,7 +14,6 @@ import dev.sheldan.abstracto.assignableroles.service.management.AssignableRolePl
|
||||
import dev.sheldan.abstracto.assignableroles.service.management.AssignableRolePlacePostManagementService;
|
||||
import dev.sheldan.abstracto.core.command.exception.AbstractoTemplatedException;
|
||||
import dev.sheldan.abstracto.core.command.exception.CommandParameterKeyValueWrongTypeException;
|
||||
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
|
||||
import dev.sheldan.abstracto.core.exception.EmoteNotUsableException;
|
||||
import dev.sheldan.abstracto.core.models.FullEmote;
|
||||
@@ -35,7 +34,6 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@@ -169,16 +167,16 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
|
||||
if(channelOptional.isPresent()) {
|
||||
TextChannel textChannel = channelOptional.get();
|
||||
if(latestPost.getAssignableRoles().size() < 20) {
|
||||
return addReactionToExistingAssignableRolePlacePost(fakeEmote, description, assignableRolePlace, placeId, roleId, serverId, latestPost, messageToSend, textChannel, fakeEmote);
|
||||
return addReactionToExistingAssignableRolePlacePost(fakeEmote, description, assignableRolePlace, placeId, roleId, serverId, latestPost, messageToSend, textChannel);
|
||||
} else {
|
||||
return addNewMessageToAssignableRolePlace(placeName, fakeEmote, description, roleId, serverId, messageToSend, textChannel, fakeEmote);
|
||||
return addNewMessageToAssignableRolePlace(placeName, fakeEmote, description, roleId, serverId, messageToSend, textChannel);
|
||||
}
|
||||
} else {
|
||||
throw new ChannelNotFoundException(latestPost.getUsedChannel().getId());
|
||||
}
|
||||
} else {
|
||||
log.info("Added emote to assignable place {} in server {}, but no message post yet.", placeName, serverId);
|
||||
self.addAssignableRoleInstanceWithoutPost(placeId, roleId, fakeEmote, description);
|
||||
self.addAssignableRoleInstanceWithoutPost(placeId, roleId, fakeEmote, description, serverId);
|
||||
}
|
||||
} else {
|
||||
throw new EmoteNotUsableException(fakeEmote.getEmote());
|
||||
@@ -186,7 +184,7 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
||||
private CompletableFuture<Void> addReactionToExistingAssignableRolePlacePost(FullEmote fakeEmote, String description, AssignableRolePlace assignableRolePlace, Long placeId, Long roleId, Long serverId, AssignableRolePlacePost latestPost, MessageToSend messageToSend, TextChannel textChannel, FullEmote emote) {
|
||||
private CompletableFuture<Void> addReactionToExistingAssignableRolePlacePost(FullEmote fakeEmote, String description, AssignableRolePlace assignableRolePlace, Long placeId, Long roleId, Long serverId, 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
|
||||
return textChannel.retrieveMessageById(latestPost.getId()).submit()
|
||||
.thenCompose(message -> messageService.addReactionToMessageWithFuture(fakeEmote.getFakeEmote(), serverId, message))
|
||||
@@ -195,16 +193,16 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
|
||||
return channelService.editEmbedMessageInAChannel(embedToUse, textChannel, latestPost.getId());
|
||||
})
|
||||
.thenCompose(message -> {
|
||||
self.addAssignableRoleInstanceWithPost(message.getIdLong(), placeId, roleId, description, emote);
|
||||
self.addAssignableRoleInstanceWithPost(message.getIdLong(), placeId, roleId, description, fakeEmote, serverId);
|
||||
return CompletableFuture.completedFuture(null);
|
||||
});
|
||||
}
|
||||
|
||||
private CompletableFuture<Void> addNewMessageToAssignableRolePlace(String placeName, FullEmote fakeEmote, String description, Long roleId, Long serverId, MessageToSend messageToSend, TextChannel textChannel, FullEmote emote) {
|
||||
private CompletableFuture<Void> addNewMessageToAssignableRolePlace(String placeName, FullEmote fakeEmote, String description, Long roleId, Long serverId, MessageToSend messageToSend, TextChannel textChannel) {
|
||||
MessageEmbed embedToUse = messageToSend.getEmbeds().get(messageToSend.getEmbeds().size() - 1);
|
||||
return channelService.sendEmbedToChannel(embedToUse, textChannel)
|
||||
.thenCompose(message -> messageService.addReactionToMessageWithFuture(fakeEmote.getFakeEmote(), serverId, message).thenAccept(aVoid ->
|
||||
self.addNewlyCreatedAssignablePlacePost(placeName, description, roleId, serverId, textChannel, message, emote)
|
||||
self.addNewlyCreatedAssignablePlacePost(placeName, description, roleId, serverId, textChannel, message, fakeEmote)
|
||||
));
|
||||
}
|
||||
|
||||
@@ -229,15 +227,15 @@ public class AssignableRolePlaceServiceBean implements AssignableRolePlaceServic
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addAssignableRoleInstanceWithPost(Long messageId, Long placeId, Long roleId, String description, FullEmote fakeEmote) {
|
||||
AEmote emote = emoteManagementService.createEmote(null, fakeEmote.getFakeEmote(), fakeEmote.getEmote().getGuild().getIdLong(), false);
|
||||
public void addAssignableRoleInstanceWithPost(Long messageId, Long placeId, Long roleId, String description, FullEmote fakeEmote, Long serverId) {
|
||||
AEmote emote = emoteManagementService.createEmote(null, fakeEmote.getFakeEmote(), serverId, false);
|
||||
emote.setChangeable(false);
|
||||
assignableRoleManagementServiceBean.addRoleToPlace(placeId, emote.getId(), roleId, description, messageId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addAssignableRoleInstanceWithoutPost(Long placeId, Long roleId, FullEmote fakeEmote, String description) {
|
||||
AEmote emote = emoteManagementService.createEmote(null, fakeEmote.getFakeEmote(), fakeEmote.getEmote().getGuild().getIdLong(), false);
|
||||
public void addAssignableRoleInstanceWithoutPost(Long placeId, Long roleId, FullEmote fakeEmote, String description, Long serverId) {
|
||||
AEmote emote = emoteManagementService.createEmote(null, fakeEmote.getFakeEmote(), serverId, false);
|
||||
emote.setChangeable(false);
|
||||
assignableRoleManagementServiceBean.addRoleToPlace(placeId, emote.getId(), roleId, description);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ 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.ARole;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
import dev.sheldan.abstracto.experience.config.features.ExperienceFeature;
|
||||
import dev.sheldan.abstracto.experience.service.management.DisabledExpRoleManagementService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -25,13 +26,17 @@ public class DisableExpForRole extends AbstractConditionableCommand {
|
||||
@Autowired
|
||||
private DisabledExpRoleManagementService disabledExpRoleManagementService;
|
||||
|
||||
@Autowired
|
||||
private RoleManagementService roleManagementService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
ARole role = (ARole) parameters.get(0);
|
||||
if(!disabledExpRoleManagementService.isExperienceDisabledForRole(role)) {
|
||||
disabledExpRoleManagementService.setRoleToBeDisabledForExp(role);
|
||||
ARole actualRole = roleManagementService.findRole(role.getId());
|
||||
if(!disabledExpRoleManagementService.isExperienceDisabledForRole(actualRole)) {
|
||||
disabledExpRoleManagementService.setRoleToBeDisabledForExp(actualRole);
|
||||
}
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ 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.ARole;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
import dev.sheldan.abstracto.experience.config.features.ExperienceFeature;
|
||||
import dev.sheldan.abstracto.experience.service.management.DisabledExpRoleManagementService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -25,12 +26,16 @@ public class EnableExpForRole extends AbstractConditionableCommand {
|
||||
@Autowired
|
||||
private DisabledExpRoleManagementService disabledExpRoleManagementService;
|
||||
|
||||
@Autowired
|
||||
private RoleManagementService roleManagementService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
ARole role = (ARole) commandContext.getParameters().getParameters().get(0);
|
||||
if(disabledExpRoleManagementService.isExperienceDisabledForRole(role)) {
|
||||
disabledExpRoleManagementService.removeRoleToBeDisabledForExp(role);
|
||||
ARole actualRole = roleManagementService.findRole(role.getId());
|
||||
if(disabledExpRoleManagementService.isExperienceDisabledForRole(actualRole)) {
|
||||
disabledExpRoleManagementService.removeRoleToBeDisabledForExp(actualRole);
|
||||
}
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
@@ -68,6 +68,7 @@ public class ListDisabledExperienceRoles extends AbstractConditionableCommand {
|
||||
.templated(true)
|
||||
.supportsEmbedException(true)
|
||||
.causesReaction(true)
|
||||
.async(true)
|
||||
.aliases(aliases)
|
||||
.parameters(parameters)
|
||||
.help(helpInfo)
|
||||
|
||||
@@ -9,13 +9,12 @@ import dev.sheldan.abstracto.core.command.config.validator.MinIntegerValueValida
|
||||
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.exception.RoleNotFoundInGuildException;
|
||||
import dev.sheldan.abstracto.core.models.database.ARole;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.service.RoleService;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
import dev.sheldan.abstracto.experience.config.features.ExperienceFeature;
|
||||
import dev.sheldan.abstracto.experience.service.ExperienceRoleService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -37,16 +36,15 @@ public class SetExpRole extends AbstractConditionableCommand {
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
@Autowired
|
||||
private RoleManagementService roleManagementService;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
Integer level = (Integer) commandContext.getParameters().getParameters().get(0);
|
||||
ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
|
||||
AServer server = commandContext.getUserInitiatedContext().getServer();
|
||||
if(!roleService.isRoleInServer(role)) {
|
||||
throw new RoleNotFoundInGuildException(role.getId(), server.getId());
|
||||
}
|
||||
log.info("Setting role {} to be used for level {} on server {}", role.getId(), level, server.getId());
|
||||
Role role = (Role) commandContext.getParameters().getParameters().get(1);
|
||||
log.info("Setting role {} to be used for level {} on server {}", role.getId(), level, role.getGuild().getId());
|
||||
return experienceRoleService.setRoleToLevel(role, level, commandContext.getUserInitiatedContext().getChannel())
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
@@ -56,7 +54,7 @@ public class SetExpRole extends AbstractConditionableCommand {
|
||||
List<Parameter> parameters = new ArrayList<>();
|
||||
List<ParameterValidator> levelValidators = Arrays.asList(MinIntegerValueValidator.min(0L));
|
||||
parameters.add(Parameter.builder().name("level").validators(levelValidators).templated(true).type(Integer.class).build());
|
||||
parameters.add(Parameter.builder().name("role").templated(true).type(ARole.class).build());
|
||||
parameters.add(Parameter.builder().name("role").templated(true).type(Role.class).build());
|
||||
HelpInfo helpInfo = HelpInfo.builder().templated(true).hasExample(true).build();
|
||||
return CommandConfiguration.builder()
|
||||
.name("setExpRole")
|
||||
|
||||
@@ -8,6 +8,7 @@ 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.ARole;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
import dev.sheldan.abstracto.experience.config.features.ExperienceFeature;
|
||||
import dev.sheldan.abstracto.experience.service.ExperienceRoleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -27,13 +28,17 @@ public class UnSetExpRole extends AbstractConditionableCommand {
|
||||
@Autowired
|
||||
private ExperienceRoleService experienceRoleService;
|
||||
|
||||
@Autowired
|
||||
private RoleManagementService roleManagementService;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
ARole role = (ARole) commandContext.getParameters().getParameters().get(0);
|
||||
ARole actualRole = roleManagementService.findRole(role.getId());
|
||||
// do not check for the existence of the role, because if the role was deleted, users should be able
|
||||
// to get rid of it in the configuration
|
||||
return experienceRoleService.unsetRole(role, commandContext.getUserInitiatedContext().getChannel())
|
||||
return experienceRoleService.unsetRole(actualRole, commandContext.getUserInitiatedContext().getChannel())
|
||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import dev.sheldan.abstracto.experience.models.database.AUserExperience;
|
||||
import dev.sheldan.abstracto.experience.service.management.ExperienceLevelManagementService;
|
||||
import dev.sheldan.abstracto.experience.service.management.ExperienceRoleManagementService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -48,9 +49,10 @@ public class ExperienceRoleServiceBean implements ExperienceRoleService {
|
||||
* @param level The level the {@link ARole} should be awarded at
|
||||
*/
|
||||
@Override
|
||||
public CompletableFuture<Void> setRoleToLevel(ARole role, Integer level, AChannel feedbackChannel) {
|
||||
Long roleId = role.getId();
|
||||
return unsetRole(role, feedbackChannel).thenAccept(aVoid ->
|
||||
public CompletableFuture<Void> setRoleToLevel(Role role, Integer level, AChannel feedbackChannel) {
|
||||
Long roleId = role.getIdLong();
|
||||
ARole aRole = roleManagementService.findRole(roleId);
|
||||
return unsetRole(aRole, feedbackChannel).thenAccept(aVoid ->
|
||||
self.unsetRoleInDb(level, roleId)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import dev.sheldan.abstracto.core.command.exception.InsufficientParametersExcept
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.models.database.ARole;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
import dev.sheldan.abstracto.experience.service.management.DisabledExpRoleManagementService;
|
||||
import dev.sheldan.abstracto.test.MockUtils;
|
||||
import dev.sheldan.abstracto.test.command.CommandConfigValidator;
|
||||
import dev.sheldan.abstracto.test.command.CommandTestUtilities;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -14,6 +14,7 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -27,6 +28,9 @@ public class DisableExpForRoleTest {
|
||||
@InjectMocks
|
||||
private DisableExpForRole testUnit;
|
||||
|
||||
@Mock
|
||||
private RoleManagementService roleManagementService;
|
||||
|
||||
@Mock
|
||||
private DisabledExpRoleManagementService disabledExpRoleManagementService;
|
||||
|
||||
@@ -51,11 +55,14 @@ public class DisableExpForRoleTest {
|
||||
}
|
||||
|
||||
private void executeDisableExpForRoleTest(boolean value, int wantedNumberOfInvocations) {
|
||||
ARole disabledRole = MockUtils.getRole(1L, MockUtils.getServer());
|
||||
CommandContext context = CommandTestUtilities.getWithParameters(Arrays.asList(disabledRole));
|
||||
when(disabledExpRoleManagementService.isExperienceDisabledForRole(disabledRole)).thenReturn(value);
|
||||
ARole parameterRole = Mockito.mock(ARole.class);
|
||||
ARole actualRole = Mockito.mock(ARole.class);
|
||||
when(parameterRole.getId()).thenReturn(5L);
|
||||
CommandContext context = CommandTestUtilities.getWithParameters(Arrays.asList(parameterRole));
|
||||
when(roleManagementService.findRole(parameterRole.getId())).thenReturn(actualRole);
|
||||
when(disabledExpRoleManagementService.isExperienceDisabledForRole(actualRole)).thenReturn(value);
|
||||
CommandResult result = testUnit.execute(context);
|
||||
verify(disabledExpRoleManagementService, times(wantedNumberOfInvocations)).setRoleToBeDisabledForExp(disabledRole);
|
||||
verify(disabledExpRoleManagementService, times(wantedNumberOfInvocations)).setRoleToBeDisabledForExp(actualRole);
|
||||
CommandTestUtilities.checkSuccessfulCompletion(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,14 +5,15 @@ import dev.sheldan.abstracto.core.command.exception.InsufficientParametersExcept
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.models.database.ARole;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
import dev.sheldan.abstracto.experience.service.management.DisabledExpRoleManagementService;
|
||||
import dev.sheldan.abstracto.test.MockUtils;
|
||||
import dev.sheldan.abstracto.test.command.CommandConfigValidator;
|
||||
import dev.sheldan.abstracto.test.command.CommandTestUtilities;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -28,6 +29,9 @@ public class EnableExpForRoleTest {
|
||||
@Mock
|
||||
private DisabledExpRoleManagementService disabledExpRoleManagementService;
|
||||
|
||||
@Mock
|
||||
private RoleManagementService roleManagementService;
|
||||
|
||||
@Test(expected = InsufficientParametersException.class)
|
||||
public void testTooLittleParameters() {
|
||||
CommandTestUtilities.executeNoParametersTest(testUnit);
|
||||
@@ -49,11 +53,15 @@ public class EnableExpForRoleTest {
|
||||
}
|
||||
|
||||
private void executeEnableExpForRoleTest(boolean value, int wantedNumberOfInvocations) {
|
||||
ARole disabledRole = MockUtils.getRole(1L, MockUtils.getServer());
|
||||
CommandContext context = CommandTestUtilities.getWithParameters(Arrays.asList(disabledRole));
|
||||
when(disabledExpRoleManagementService.isExperienceDisabledForRole(disabledRole)).thenReturn(value);
|
||||
ARole roleParameter = Mockito.mock(ARole.class);
|
||||
CommandContext context = CommandTestUtilities.getWithParameters(Arrays.asList(roleParameter));
|
||||
Long roleId = 8L;
|
||||
when(roleParameter.getId()).thenReturn(roleId);
|
||||
ARole actualRole = Mockito.mock(ARole.class);
|
||||
when(roleManagementService.findRole(roleId)).thenReturn(actualRole);
|
||||
when(disabledExpRoleManagementService.isExperienceDisabledForRole(actualRole)).thenReturn(value);
|
||||
CommandResult result = testUnit.execute(context);
|
||||
verify(disabledExpRoleManagementService, times(wantedNumberOfInvocations)).removeRoleToBeDisabledForExp(disabledRole);
|
||||
verify(disabledExpRoleManagementService, times(wantedNumberOfInvocations)).removeRoleToBeDisabledForExp(actualRole);
|
||||
CommandTestUtilities.checkSuccessfulCompletion(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,17 +4,17 @@ import dev.sheldan.abstracto.core.command.exception.IncorrectParameterException;
|
||||
import dev.sheldan.abstracto.core.command.exception.InsufficientParametersException;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.exception.RoleNotFoundInGuildException;
|
||||
import dev.sheldan.abstracto.core.models.database.ARole;
|
||||
import dev.sheldan.abstracto.core.service.RoleService;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
import dev.sheldan.abstracto.experience.service.ExperienceRoleService;
|
||||
import dev.sheldan.abstracto.test.MockUtils;
|
||||
import dev.sheldan.abstracto.test.command.CommandConfigValidator;
|
||||
import dev.sheldan.abstracto.test.command.CommandTestUtilities;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -34,6 +34,9 @@ public class SetExpRoleTest {
|
||||
@Mock
|
||||
private RoleService roleService;
|
||||
|
||||
@Mock
|
||||
private RoleManagementService roleManagementService;
|
||||
|
||||
@Test(expected = InsufficientParametersException.class)
|
||||
public void testTooLittleParameters() {
|
||||
CommandTestUtilities.executeNoParametersTestAsync(testUnit);
|
||||
@@ -59,25 +62,15 @@ public class SetExpRoleTest {
|
||||
@Test
|
||||
public void setExpRole() {
|
||||
CommandContext noParameters = CommandTestUtilities.getNoParameters();
|
||||
ARole changedRole = MockUtils.getRole(4L, noParameters.getUserInitiatedContext().getServer());
|
||||
Role roleToChange = Mockito.mock(Role.class);
|
||||
when(roleToChange.getGuild()).thenReturn(noParameters.getGuild());
|
||||
Integer levelToSetTo = 4;
|
||||
CommandContext context = CommandTestUtilities.enhanceWithParameters(noParameters, Arrays.asList(levelToSetTo, changedRole));
|
||||
when(roleService.isRoleInServer(changedRole)).thenReturn(true);
|
||||
when(experienceRoleService.setRoleToLevel(changedRole, levelToSetTo, context.getUserInitiatedContext().getChannel())).thenReturn(CompletableFuture.completedFuture(null));
|
||||
CommandContext context = CommandTestUtilities.enhanceWithParameters(noParameters, Arrays.asList(levelToSetTo, roleToChange));
|
||||
when(experienceRoleService.setRoleToLevel(roleToChange, levelToSetTo, context.getUserInitiatedContext().getChannel())).thenReturn(CompletableFuture.completedFuture(null));
|
||||
CompletableFuture<CommandResult> result = testUnit.executeAsync(context);
|
||||
CommandTestUtilities.checkSuccessfulCompletionAsync(result);
|
||||
}
|
||||
|
||||
@Test(expected = RoleNotFoundInGuildException.class)
|
||||
public void setExpRoleNotExistingOnServer() {
|
||||
CommandContext noParameters = CommandTestUtilities.getNoParameters();
|
||||
ARole changedRole = MockUtils.getRole(4L, noParameters.getUserInitiatedContext().getServer());
|
||||
Integer levelToSetTo = 4;
|
||||
CommandContext context = CommandTestUtilities.enhanceWithParameters(noParameters, Arrays.asList(levelToSetTo, changedRole));
|
||||
when(roleService.isRoleInServer(changedRole)).thenReturn(false);
|
||||
testUnit.executeAsync(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validateCommand() {
|
||||
CommandConfigValidator.validateCommandConfiguration(testUnit.getConfiguration());
|
||||
|
||||
@@ -5,6 +5,7 @@ import dev.sheldan.abstracto.core.command.exception.InsufficientParametersExcept
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.models.database.ARole;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
import dev.sheldan.abstracto.experience.service.ExperienceRoleService;
|
||||
import dev.sheldan.abstracto.test.MockUtils;
|
||||
import dev.sheldan.abstracto.test.command.CommandConfigValidator;
|
||||
@@ -13,6 +14,7 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -29,6 +31,9 @@ public class UnSetExpRoleTest {
|
||||
@Mock
|
||||
private ExperienceRoleService experienceRoleService;
|
||||
|
||||
@Mock
|
||||
private RoleManagementService roleManagementService;
|
||||
|
||||
@Test(expected = InsufficientParametersException.class)
|
||||
public void testTooLittleParameters() {
|
||||
CommandTestUtilities.executeNoParametersTestAsync(testUnit);
|
||||
@@ -44,7 +49,9 @@ public class UnSetExpRoleTest {
|
||||
CommandContext noParameters = CommandTestUtilities.getNoParameters();
|
||||
ARole changedRole = MockUtils.getRole(4L, noParameters.getUserInitiatedContext().getServer());
|
||||
CommandContext context = CommandTestUtilities.enhanceWithParameters(noParameters, Arrays.asList(changedRole));
|
||||
when(experienceRoleService.unsetRole(changedRole, context.getUserInitiatedContext().getChannel())).thenReturn(CompletableFuture.completedFuture(null));
|
||||
ARole actualRole = Mockito.mock(ARole.class);
|
||||
when(roleManagementService.findRole(changedRole.getId())).thenReturn(actualRole);
|
||||
when(experienceRoleService.unsetRole(actualRole, context.getUserInitiatedContext().getChannel())).thenReturn(CompletableFuture.completedFuture(null));
|
||||
CompletableFuture<CommandResult> result = testUnit.executeAsync(context);
|
||||
CommandTestUtilities.checkSuccessfulCompletionAsync(result);
|
||||
}
|
||||
|
||||
@@ -14,11 +14,13 @@ import dev.sheldan.abstracto.experience.models.database.AUserExperience;
|
||||
import dev.sheldan.abstracto.experience.service.management.ExperienceLevelManagementService;
|
||||
import dev.sheldan.abstracto.experience.service.management.ExperienceRoleManagementService;
|
||||
import dev.sheldan.abstracto.test.MockUtils;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -56,9 +58,13 @@ public class ExperienceRoleServiceBeanTest extends ExperienceRelatedTest {
|
||||
AServer server = MockUtils.getServer();
|
||||
Integer levelCount = 10;
|
||||
AExperienceLevel level = AExperienceLevel.builder().experienceNeeded(10L).level(levelCount).build();
|
||||
ARole roleToChange = getRole(1L, server);
|
||||
AExperienceRole previousExperienceRole = AExperienceRole.builder().role(roleToChange).roleServer(server).level(level).build();
|
||||
when(experienceRoleManagementService.getRoleInServerOptional(roleToChange)).thenReturn(Optional.of(previousExperienceRole));
|
||||
Role roleToChange = Mockito.mock(Role.class);
|
||||
ARole role = Mockito.mock(ARole.class);
|
||||
Long roleId = 5L;
|
||||
when(roleToChange.getIdLong()).thenReturn(roleId);
|
||||
when(roleManagementService.findRole(roleId)).thenReturn(role);
|
||||
AExperienceRole previousExperienceRole = AExperienceRole.builder().role(role).roleServer(server).level(level).build();
|
||||
when(experienceRoleManagementService.getRoleInServerOptional(role)).thenReturn(Optional.of(previousExperienceRole));
|
||||
CompletableFuture<Void> future = testingUnit.setRoleToLevel(roleToChange, levelCount, getFeedbackChannel(server));
|
||||
|
||||
future.join();
|
||||
@@ -86,14 +92,19 @@ public class ExperienceRoleServiceBeanTest extends ExperienceRelatedTest {
|
||||
AServer server = MockUtils.getServer();
|
||||
Integer levelCount = 10;
|
||||
AExperienceLevel level = AExperienceLevel.builder().experienceNeeded(10L).level(levelCount).build();
|
||||
ARole roleToChange = getRole(1L, server);
|
||||
Role roleToChange = Mockito.mock(Role.class);
|
||||
ARole role = Mockito.mock(ARole.class);
|
||||
Long roleId = 5L;
|
||||
when(roleToChange.getIdLong()).thenReturn(roleId);
|
||||
when(roleManagementService.findRole(roleId)).thenReturn(role);
|
||||
when(role.getServer()).thenReturn(server);
|
||||
ARole newRoleToAward = getRole(2L, server);
|
||||
AUserExperience firstUser = AUserExperience.builder().build();
|
||||
AUserExperience secondUser = AUserExperience.builder().build();
|
||||
List<AUserExperience> users = Arrays.asList(firstUser, secondUser);
|
||||
AExperienceRole previousExperienceRole = AExperienceRole.builder().role(roleToChange).id(roleToChange.getId()).roleServer(server).level(level).users(users).build();
|
||||
AExperienceRole previousExperienceRole = AExperienceRole.builder().role(role).id(roleToChange.getIdLong()).roleServer(server).level(level).users(users).build();
|
||||
AExperienceRole newExperienceRole = AExperienceRole.builder().role(newRoleToAward).id(newRoleToAward.getId()).roleServer(server).level(level).build();
|
||||
when(experienceRoleManagementService.getRoleInServerOptional(roleToChange)).thenReturn(Optional.of(previousExperienceRole));
|
||||
when(experienceRoleManagementService.getRoleInServerOptional(role)).thenReturn(Optional.of(previousExperienceRole));
|
||||
when(experienceRoleManagementService.getExperienceRolesForServer(server)).thenReturn(new ArrayList<>(Arrays.asList(newExperienceRole, previousExperienceRole)));
|
||||
AChannel feedBackChannel = getFeedbackChannel(server);
|
||||
List<CompletableFuture<RoleCalculationResult>> futures = new ArrayList<>();
|
||||
|
||||
@@ -6,6 +6,7 @@ import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.experience.models.database.AExperienceLevel;
|
||||
import dev.sheldan.abstracto.experience.models.database.AExperienceRole;
|
||||
import dev.sheldan.abstracto.experience.models.database.AUserExperience;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -20,7 +21,7 @@ public interface ExperienceRoleService {
|
||||
* @param role The {@link ARole} to set the level to
|
||||
* @param level The level the {@link ARole} should be awarded at
|
||||
*/
|
||||
CompletableFuture<Void> setRoleToLevel(ARole role, Integer level, AChannel channel);
|
||||
CompletableFuture<Void> setRoleToLevel(Role role, Integer level, AChannel channel);
|
||||
|
||||
/**
|
||||
* Removes the role from the {@link dev.sheldan.abstracto.experience.models.database.AExperienceRole} configuration
|
||||
|
||||
@@ -37,6 +37,7 @@ public class DeleteNote extends AbstractConditionableCommand {
|
||||
if(userNoteManagementService.noteExists(id)) {
|
||||
userNoteManagementService.deleteNote(id);
|
||||
} else {
|
||||
// TODO replace with exception
|
||||
return CommandResult.fromError(templateService.renderSimpleTemplate(NOTE_NOT_FOUND_EXCEPTION_TEMPLATE));
|
||||
}
|
||||
return CommandResult.fromSuccess();
|
||||
|
||||
@@ -8,9 +8,11 @@ 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.ARole;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
import dev.sheldan.abstracto.moderation.config.ModerationModule;
|
||||
import dev.sheldan.abstracto.moderation.config.features.ModerationFeatures;
|
||||
import dev.sheldan.abstracto.moderation.service.management.MuteRoleManagementService;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -23,10 +25,14 @@ public class SetMuteRole extends AbstractConditionableCommand {
|
||||
@Autowired
|
||||
private MuteRoleManagementService muteRoleManagementService;
|
||||
|
||||
@Autowired
|
||||
private RoleManagementService roleManagementService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
checkParameters(commandContext);
|
||||
ARole role = (ARole) commandContext.getParameters().getParameters().get(0);
|
||||
Role jdaRole = (Role) commandContext.getParameters().getParameters().get(0);
|
||||
ARole role = roleManagementService.findRole(jdaRole.getIdLong());
|
||||
muteRoleManagementService.setMuteRoleForServer(commandContext.getUserInitiatedContext().getServer(), role);
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
@@ -34,7 +40,7 @@ public class SetMuteRole extends AbstractConditionableCommand {
|
||||
@Override
|
||||
public CommandConfiguration getConfiguration() {
|
||||
List<Parameter> parameters = new ArrayList<>();
|
||||
parameters.add(Parameter.builder().name("role").templated(true).type(ARole.class).build());
|
||||
parameters.add(Parameter.builder().name("role").templated(true).type(Role.class).build());
|
||||
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
|
||||
return CommandConfiguration.builder()
|
||||
.name("setMuteRole")
|
||||
|
||||
@@ -5,13 +5,16 @@ import dev.sheldan.abstracto.core.command.exception.InsufficientParametersExcept
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.models.database.ARole;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
import dev.sheldan.abstracto.moderation.service.management.MuteRoleManagementService;
|
||||
import dev.sheldan.abstracto.test.command.CommandConfigValidator;
|
||||
import dev.sheldan.abstracto.test.command.CommandTestUtilities;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -27,12 +30,19 @@ public class SetMuteRoleTest {
|
||||
@Mock
|
||||
private MuteRoleManagementService muteRoleManagementService;
|
||||
|
||||
@Mock
|
||||
private RoleManagementService roleManagementService;
|
||||
|
||||
@Test
|
||||
public void testExecuteCommand() {
|
||||
ARole muteRoleToSet = ARole.builder().build();
|
||||
CommandContext parameters = CommandTestUtilities.getWithParameters(Arrays.asList(muteRoleToSet));
|
||||
Role role = Mockito.mock(Role.class);
|
||||
Long roleId = 5L;
|
||||
when(role.getIdLong()).thenReturn(roleId);
|
||||
ARole aRole = Mockito.mock(ARole.class);
|
||||
when(roleManagementService.findRole(roleId)).thenReturn(aRole);
|
||||
CommandContext parameters = CommandTestUtilities.getWithParameters(Arrays.asList(role));
|
||||
CommandResult result = testUnit.execute(parameters);
|
||||
verify(muteRoleManagementService, times(1)).setMuteRoleForServer(parameters.getUserInitiatedContext().getServer(), muteRoleToSet);
|
||||
verify(muteRoleManagementService, times(1)).setMuteRoleForServer(parameters.getUserInitiatedContext().getServer(), aRole);
|
||||
CommandTestUtilities.checkSuccessfulCompletion(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,10 @@ 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.ARole;
|
||||
import dev.sheldan.abstracto.core.service.management.RoleManagementService;
|
||||
import dev.sheldan.abstracto.modmail.config.ModMailFeatures;
|
||||
import dev.sheldan.abstracto.modmail.service.ModMailRoleService;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -27,16 +29,20 @@ public class SetModMailRole extends AbstractConditionableCommand {
|
||||
@Autowired
|
||||
private ModMailRoleService modMailRoleService;
|
||||
|
||||
@Autowired
|
||||
private RoleManagementService roleManagementService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
ARole role = (ARole) commandContext.getParameters().getParameters().get(0);
|
||||
Role jdaRole = (Role) commandContext.getParameters().getParameters().get(0);
|
||||
ARole role = roleManagementService.findRole(jdaRole.getIdLong());
|
||||
modMailRoleService.addRoleToModMailRoles(role);
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandConfiguration getConfiguration() {
|
||||
Parameter categoryId = Parameter.builder().name("role").type(ARole.class).templated(true).build();
|
||||
Parameter categoryId = Parameter.builder().name("role").type(Role.class).templated(true).build();
|
||||
List<Parameter> parameters = Arrays.asList(categoryId);
|
||||
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
|
||||
List<String> aliases = Arrays.asList("modMailRole");
|
||||
|
||||
@@ -25,7 +25,11 @@ public class AEmoteParameterHandler implements CommandParameterHandler {
|
||||
@Override
|
||||
public Object handle(String input, CommandParameterIterators iterators, Class clazz, Message context) {
|
||||
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, Emote.class, context);
|
||||
return emoteService.getFakeEmoteFromEmote(emote);
|
||||
if(emote != null) {
|
||||
return emoteService.getFakeEmoteFromEmote(emote);
|
||||
} else {
|
||||
return emoteService.getFakeEmote(input);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,6 +3,7 @@ package dev.sheldan.abstracto.core.command.handler;
|
||||
import dev.sheldan.abstracto.core.command.CommandConstants;
|
||||
import net.dv8tion.jda.api.entities.Emote;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
@@ -21,8 +22,12 @@ public class EmoteParameterHandler implements CommandParameterHandler {
|
||||
if(matcher.matches()) {
|
||||
return iterators.getEmoteIterator().next();
|
||||
} else {
|
||||
long emoteId = Long.parseLong(input);
|
||||
return context.getGuild().getEmoteById(emoteId);
|
||||
if(StringUtils.isNumeric(input)) {
|
||||
long emoteId = Long.parseLong(input);
|
||||
return context.getGuild().getEmoteById(emoteId);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,12 @@ public class FullEmoteParameterHandler implements CommandParameterHandler {
|
||||
@Override
|
||||
public Object handle(String input, CommandParameterIterators iterators, Class clazz, Message context) {
|
||||
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, Emote.class, context);
|
||||
AEmote aEmote = emoteService.getFakeEmoteFromEmote(emote);
|
||||
AEmote aEmote;
|
||||
if(emote != null) {
|
||||
aEmote = emoteService.getFakeEmoteFromEmote(emote);
|
||||
} else {
|
||||
aEmote = emoteService.getFakeEmote(input);
|
||||
}
|
||||
return FullEmote.builder().emote(emote).fakeEmote(aEmote).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -33,8 +33,8 @@ public class RemoveFromChannelGroup extends AbstractConditionableCommand {
|
||||
|
||||
@Override
|
||||
public CommandConfiguration getConfiguration() {
|
||||
Parameter channelGroupName = Parameter.builder().name("name").type(String.class).description("The name of the channel group to remove the channel from.").build();
|
||||
Parameter channelToAdd = Parameter.builder().name("channel").type(TextChannel.class).description("The mention of the channel to remove from the group.").build();
|
||||
Parameter channelGroupName = Parameter.builder().name("name").type(String.class).build();
|
||||
Parameter channelToAdd = Parameter.builder().name("channel").type(TextChannel.class).build();
|
||||
List<Parameter> parameters = Arrays.asList(channelGroupName, channelToAdd);
|
||||
List<String> aliases = Arrays.asList("rmChChgrp", "chGrpCh-");
|
||||
HelpInfo helpInfo = HelpInfo.builder().templated(true).hasExample(true).build();
|
||||
|
||||
@@ -53,6 +53,7 @@ public class Allow extends AbstractConditionableCommand {
|
||||
ACommand command = commandManagementService.findCommandByName(name);
|
||||
commandService.unRestrictCommand(command, commandContext.getUserInitiatedContext().getServer());
|
||||
} else {
|
||||
// TODO refactor to use exception
|
||||
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
|
||||
}
|
||||
return CommandResult.fromSuccess();
|
||||
|
||||
@@ -48,13 +48,14 @@ public class AllowRole extends AbstractConditionableCommand {
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
String name = (String) commandContext.getParameters().getParameters().get(0);
|
||||
ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
|
||||
ARole fakeRole = (ARole) commandContext.getParameters().getParameters().get(1);
|
||||
ARole actualRole = roleManagementService.findRole(fakeRole.getId());
|
||||
if(featureManagementService.featureExists(name)) {
|
||||
FeatureEnum featureEnum = featureFlagService.getFeatureEnum(name);
|
||||
commandService.allowFeatureForRole(featureEnum, role);
|
||||
commandService.allowFeatureForRole(featureEnum, actualRole);
|
||||
} else if(commandManagementService.doesCommandExist(name)) {
|
||||
ACommand command = commandManagementService.findCommandByName(name);
|
||||
commandService.allowCommandForRole(command, role);
|
||||
commandService.allowCommandForRole(command, actualRole);
|
||||
} else {
|
||||
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
|
||||
}
|
||||
|
||||
@@ -46,14 +46,15 @@ public class DisAllowRole extends AbstractConditionableCommand {
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
String name = (String) commandContext.getParameters().getParameters().get(0);
|
||||
ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
|
||||
ARole actualRole = roleManagementService.findRole(role.getId());
|
||||
if(featureManagementService.featureExists(name)) {
|
||||
AFeature feature = featureManagementService.getFeature(name);
|
||||
feature.getCommands().forEach(command ->
|
||||
commandService.disAllowCommandForRole(command, role)
|
||||
commandService.disAllowCommandForRole(command, actualRole)
|
||||
);
|
||||
} else if(commandManagementService.doesCommandExist(name)) {
|
||||
ACommand command = commandManagementService.findCommandByName(name);
|
||||
commandService.disAllowCommandForRole(command, role);
|
||||
commandService.disAllowCommandForRole(command, actualRole);
|
||||
} else {
|
||||
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
|
||||
}
|
||||
|
||||
@@ -46,15 +46,17 @@ public class MakeAffected extends AbstractConditionableCommand {
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
String name = (String) commandContext.getParameters().getParameters().get(0);
|
||||
ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
|
||||
ARole actualRole = roleManagementService.findRole(role.getId());
|
||||
if(featureManagementService.featureExists(name)) {
|
||||
AFeature feature = featureManagementService.getFeature(name);
|
||||
feature.getCommands().forEach(command ->
|
||||
commandService.makeRoleAffectedByCommand(command, role)
|
||||
commandService.makeRoleAffectedByCommand(command, actualRole)
|
||||
);
|
||||
} else if(commandManagementService.doesCommandExist(name)) {
|
||||
ACommand command = commandManagementService.findCommandByName(name);
|
||||
commandService.makeRoleAffectedByCommand(command, role);
|
||||
commandService.makeRoleAffectedByCommand(command, actualRole);
|
||||
} else {
|
||||
// TODO refactor to use exception
|
||||
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
|
||||
}
|
||||
return CommandResult.fromSuccess();
|
||||
|
||||
@@ -46,15 +46,17 @@ public class MakeImmune extends AbstractConditionableCommand {
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
String name = (String) commandContext.getParameters().getParameters().get(0);
|
||||
ARole role = (ARole) commandContext.getParameters().getParameters().get(1);
|
||||
ARole actualRole = roleManagementService.findRole(role.getId());
|
||||
if(featureManagementService.featureExists(name)) {
|
||||
AFeature feature = featureManagementService.getFeature(name);
|
||||
feature.getCommands().forEach(command ->
|
||||
commandService.makeRoleImmuneForCommand(command, role)
|
||||
commandService.makeRoleImmuneForCommand(command, actualRole)
|
||||
);
|
||||
} else if(commandManagementService.doesCommandExist(name)) {
|
||||
ACommand command = commandManagementService.findCommandByName(name);
|
||||
commandService.makeRoleImmuneForCommand(command, role);
|
||||
commandService.makeRoleImmuneForCommand(command, actualRole);
|
||||
} else {
|
||||
// TODO refactor to use exception
|
||||
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
|
||||
}
|
||||
return CommandResult.fromSuccess();
|
||||
|
||||
@@ -53,6 +53,7 @@ public class Restrict extends AbstractConditionableCommand {
|
||||
ACommand command = commandManagementService.findCommandByName(name);
|
||||
commandService.restrictCommand(command, commandContext.getUserInitiatedContext().getServer());
|
||||
} else {
|
||||
// TODO Refactor to use exception
|
||||
return CommandResult.fromError(templateService.renderTemplate(CommandServiceBean.NO_FEATURE_COMMAND_FOUND_EXCEPTION_TEMPLATE, new Object()));
|
||||
}
|
||||
return CommandResult.fromSuccess();
|
||||
|
||||
@@ -157,30 +157,30 @@ public class EmoteManagementServiceBean implements EmoteManagementService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AEmote setEmoteToAEmote(String name, AEmote emote, Long serverId) {
|
||||
public AEmote setEmoteToAEmote(String name, AEmote fakeEmote, Long serverId) {
|
||||
Optional<AEmote> emoteOptional = loadEmoteByName(name, serverId);
|
||||
if(!emoteOptional.isPresent()) {
|
||||
return createEmote(name, emote, serverId, true);
|
||||
return createEmote(name, fakeEmote, serverId, true);
|
||||
} else {
|
||||
AEmote emoteBeingSet = emoteOptional.get();
|
||||
if(emote.getCustom()) {
|
||||
emoteBeingSet.setCustom(emote.getCustom());
|
||||
emoteBeingSet.setEmoteId(emote.getEmoteId());
|
||||
emoteBeingSet.setEmoteKey(emote.getEmoteKey());
|
||||
if(fakeEmote.getCustom()) {
|
||||
emoteBeingSet.setCustom(fakeEmote.getCustom());
|
||||
emoteBeingSet.setEmoteId(fakeEmote.getEmoteId());
|
||||
emoteBeingSet.setEmoteKey(fakeEmote.getEmoteKey());
|
||||
} else {
|
||||
emoteBeingSet.setCustom(false);
|
||||
emoteBeingSet.setEmoteKey(emote.getEmoteKey());
|
||||
emoteBeingSet.setEmoteKey(fakeEmote.getEmoteKey());
|
||||
}
|
||||
return emoteBeingSet;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AEmote createEmote(String name, AEmote emote, Long serverId, boolean validateName) {
|
||||
if(emote.getCustom()) {
|
||||
return this.createCustomEmote(name, emote, serverId, validateName);
|
||||
public AEmote createEmote(String name, AEmote fakeEmote, Long serverId, boolean validateName) {
|
||||
if(fakeEmote.getCustom()) {
|
||||
return this.createCustomEmote(name, fakeEmote, serverId, validateName);
|
||||
} else {
|
||||
return this.createDefaultEmote(name, emote.getEmoteKey(), serverId, validateName);
|
||||
return this.createDefaultEmote(name, fakeEmote.getEmoteKey(), serverId, validateName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,5 +56,14 @@ public class AEmoteParameterHandlerTest {
|
||||
Assert.assertEquals(aEmote, parsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultEmoteHandling() {
|
||||
String input = "test";
|
||||
when(emoteParameterHandler.handle(input, iterators, Emote.class, message)).thenReturn(null);
|
||||
when(emoteService.getFakeEmote(input)).thenReturn(aEmote);
|
||||
AEmote parsed = (AEmote) testUnit.handle(input, iterators, AEmote.class, message);
|
||||
Assert.assertEquals(aEmote, parsed);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -62,10 +62,9 @@ public class EmoteParameterHandlerTest {
|
||||
Assert.assertEquals(parsed, emote);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
@Test
|
||||
public void testInvalidEmoteMention() {
|
||||
String input = "test";
|
||||
testUnit.handle(input, null, Emote.class, null);
|
||||
Assert.assertNull(testUnit.handle("test", null, Emote.class, null));
|
||||
}
|
||||
|
||||
private String getEmoteMention() {
|
||||
|
||||
@@ -59,4 +59,15 @@ public class FullEmoteParameterHandlerTest {
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDefaultEmoteHandling() {
|
||||
String input = "test";
|
||||
when(emoteParameterHandler.handle(input, iterators, Emote.class, message)).thenReturn(null);
|
||||
when(emoteService.getFakeEmote(input)).thenReturn(aEmote);
|
||||
FullEmote parsed = (FullEmote) testUnit.handle(input, iterators, AEmote.class, message);
|
||||
Assert.assertNull(parsed.getEmote());
|
||||
Assert.assertEquals(aEmote, parsed.getFakeEmote());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user