mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-24 13:44:33 +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:
@@ -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