[AB-225] adding parameter handler for messages

reworking parameter handling in command received handler
adding string parameter handler to explicitly parse strings
This commit is contained in:
Sheldan
2021-04-07 23:16:25 +02:00
parent c56a037d28
commit 400181a280
62 changed files with 815 additions and 153 deletions

View File

@@ -43,7 +43,7 @@ public class LoveCalc extends AbstractConditionableCommand {
model.setFirstPart(firstPart);
model.setSecondPart(secondPart);
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInTextChannelList(LOVE_CALC_RESPONSE_TEMPLATE_KEY, model, commandContext.getChannel()))
.thenApply(unused -> CommandResult.fromIgnored());
.thenApply(unused -> CommandResult.fromSuccess());
}
@Override
@@ -58,7 +58,6 @@ public class LoveCalc extends AbstractConditionableCommand {
.module(EntertainmentModuleDefinition.ENTERTAINMENT)
.templated(true)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)
.build();

View File

@@ -0,0 +1,81 @@
package dev.sheldan.abstracto.entertainment.command;
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
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.command.execution.ContextConverter;
import dev.sheldan.abstracto.core.command.handler.parameter.CombinedParameter;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.entertainment.config.EntertainmentFeatureDefinition;
import dev.sheldan.abstracto.entertainment.config.EntertainmentModuleDefinition;
import dev.sheldan.abstracto.entertainment.model.MockResponseModel;
import dev.sheldan.abstracto.entertainment.service.EntertainmentService;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import static dev.sheldan.abstracto.core.command.config.Parameter.ADDITIONAL_TYPES_KEY;
@Component
public class Mock extends AbstractConditionableCommand {
public static final String MOCK_RESPONSE_TEMPLATE_KEY = "mock_response";
@Autowired
private EntertainmentService entertainmentService;
@Autowired
private ChannelService channelService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
Object givenParameter = commandContext.getParameters().getParameters().get(0);
String messageText;
Member mockedMember = null;
if(givenParameter instanceof Message) {
Message originalMessage = (Message) givenParameter;
messageText = originalMessage.getContentRaw();
mockedMember = originalMessage.getMember();
} else {
messageText = givenParameter.toString();
}
String mockingText = entertainmentService.createMockText(messageText, commandContext.getAuthor(), mockedMember);
MockResponseModel model = (MockResponseModel) ContextConverter.slimFromCommandContext(commandContext, MockResponseModel.class);
model.setOriginalText(messageText);
model.setMockingText(mockingText);
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInTextChannelList(MOCK_RESPONSE_TEMPLATE_KEY, model, commandContext.getChannel()))
.thenApply(unused -> CommandResult.fromSuccess());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
Map<String, Object> parameterAlternatives = new HashMap<>();
parameterAlternatives.put(ADDITIONAL_TYPES_KEY, Arrays.asList(Message.class, String.class));
parameters.add(Parameter.builder().name("message").type(CombinedParameter.class).remainder(true)
.additionalInfo(parameterAlternatives).templated(true).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("mock")
.module(EntertainmentModuleDefinition.ENTERTAINMENT)
.templated(true)
.async(true)
.supportsEmbedException(true)
.parameters(parameters)
.help(helpInfo)
.build();
}
@Override
public FeatureDefinition getFeature() {
return EntertainmentFeatureDefinition.ENTERTAINMENT;
}
}

View File

@@ -54,4 +54,19 @@ public class EntertainmentServiceBean implements EntertainmentService {
public String takeChoice(List<String> choices, Member memberExecuting) {
return choices.get(secureRandom.nextInt(choices.size()));
}
@Override
public String createMockText(String text, Member memberExecuting, Member mockedUser) {
char[] textChars = text.toLowerCase().toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0, textCharsLength = textChars.length; i < textCharsLength; i++) {
char character = textChars[i];
if(i % 2 == 0) {
sb.append(Character.toUpperCase(character));
} else {
sb.append(character);
}
}
return sb.toString();
}
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../dbchangelog-3.8.xsd" >
<include file="entertainment-seedData/data.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,20 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<property name="entertainmentModule" value="(SELECT id FROM module WHERE name = 'entertainment')"/>
<property name="entertainmentFeature" value="(SELECT id FROM feature WHERE key = 'entertainment')"/>
<changeSet author="Sheldan" id="mock-command">
<insert tableName="command">
<column name="name" value="mock"/>
<column name="module_id" valueComputed="${entertainmentModule}"/>
<column name="feature_id" valueComputed="${entertainmentFeature}"/>
</insert>
</changeSet>
</databaseChangeLog>

View File

@@ -0,0 +1,10 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../../dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<include file="command.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -7,4 +7,5 @@
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog-3.8.xsd" >
<include file="1.0-entertainment/collection.xml" relativeToChangelogFile="true"/>
<include file="1.2.8-entertainment/collection.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,14 @@
package dev.sheldan.abstracto.entertainment.model;
import dev.sheldan.abstracto.core.models.context.SlimUserInitiatedServerContext;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
@Getter
@Setter
@SuperBuilder
public class MockResponseModel extends SlimUserInitiatedServerContext {
private String originalText;
private String mockingText;
}

View File

@@ -10,4 +10,5 @@ public interface EntertainmentService {
Integer calculateRollResult(Integer low, Integer high);
boolean executeRoulette(Member memberExecuting);
String takeChoice(List<String> choices, Member memberExecuting);
String createMockText(String text, Member memberExecuting, Member mockedUser);
}

View File

@@ -1,9 +1,12 @@
package dev.sheldan.abstracto.statistic.emote.command.parameter.handler;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.command.handler.CommandParameterHandler;
import dev.sheldan.abstracto.core.command.handler.CommandParameterIterators;
import dev.sheldan.abstracto.core.command.handler.provided.EmoteParameterHandler;
import dev.sheldan.abstracto.core.command.service.CommandService;
import dev.sheldan.abstracto.statistic.emote.model.database.TrackedEmote;
import dev.sheldan.abstracto.statistic.emote.service.TrackedEmoteService;
import net.dv8tion.jda.api.entities.Emote;
@@ -24,6 +27,9 @@ public class TrackedEmoteParameterHandler implements CommandParameterHandler {
@Autowired
private TrackedEmoteService trackedEmoteService;
@Autowired
private CommandService commandService;
/**
* This {@link CommandParameterHandler} only handles {@link TrackedEmote}
* @param clazz The desired {@link Class} of a parameter
@@ -42,14 +48,17 @@ public class TrackedEmoteParameterHandler implements CommandParameterHandler {
* @param input The {@link String} input at the current position
* @param iterators The {@link CommandParameterIterators} containing all available iterators to directly retrieve JDA related
* entities from
* @param clazz The {@link Class} which this type should handle
* @param param The {@link Class} which this type should handle
* @param context The {@link Message} which caused the command to be executed
* @param command
* @return A faked {@link TrackedEmote} based on the given input or from {@link CommandParameterIterators} directly. This {@link TrackedEmote}
* does not need to actually exist.
*/
@Override
public Object handle(UnparsedCommandParameterPiece input, CommandParameterIterators iterators, Class clazz, Message context) {
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, Emote.class, context);
public Object handle(UnparsedCommandParameterPiece input, CommandParameterIterators iterators, Parameter param, Message context, Command command) {
Parameter cloned = commandService.cloneParameter(param);
cloned.setType(Emote.class);
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, cloned, context, command);
if(emote != null) {
return trackedEmoteService.getFakeTrackedEmote(emote, context.getGuild());
} else {

View File

@@ -1,9 +1,12 @@
package dev.sheldan.abstracto.statistic.emote.command.parameter.handler;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.command.handler.CommandParameterHandler;
import dev.sheldan.abstracto.core.command.handler.CommandParameterIterators;
import dev.sheldan.abstracto.core.command.handler.provided.EmoteParameterHandler;
import dev.sheldan.abstracto.core.command.service.CommandService;
import dev.sheldan.abstracto.statistic.emote.command.parameter.TrackEmoteParameter;
import dev.sheldan.abstracto.statistic.emote.model.database.TrackedEmote;
import dev.sheldan.abstracto.statistic.emote.service.TrackedEmoteService;
@@ -28,6 +31,9 @@ public class TrackedEmoteParameterParameterHandler implements CommandParameterHa
@Autowired
private TrackedEmoteService trackedEmoteService;
@Autowired
private CommandService commandService;
/**
* This {@link CommandParameterHandler} only handles {@link TrackEmoteParameter}
* @param clazz The desired {@link Class} of a parameter
@@ -45,15 +51,18 @@ public class TrackedEmoteParameterParameterHandler implements CommandParameterHa
* @param input The {@link String} input at the current position
* @param iterators The {@link CommandParameterIterators} containing all available iterators to directly retrieve JDA related
* entities from
* @param clazz The {@link Class} which this type should handle
* @param param The {@link Class} which this type should handle
* @param context The {@link Message} which caused the command to be executed
* @param command
* @return An instance of {@link TrackEmoteParameter} which contains the available instances. This is an {@link Emote} in case it was
* used directly. In every successful case, it will contain a faked {@link TrackedEmote}.
*/
@Override
public Object handle(UnparsedCommandParameterPiece input, CommandParameterIterators iterators, Class clazz, Message context) {
public Object handle(UnparsedCommandParameterPiece input, CommandParameterIterators iterators, Parameter param, Message context, Command command) {
TrackEmoteParameter parameter = TrackEmoteParameter.builder().build();
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, Emote.class, context);
Parameter cloned = commandService.cloneParameter(param);
cloned.setType(Emote.class);
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, cloned, context, command);
if(emote != null) {
parameter.setEmote(emote);
parameter.setTrackedEmote(trackedEmoteService.getFakeTrackedEmote(emote, context.getGuild()));

View File

@@ -1,8 +1,11 @@
package dev.sheldan.abstracto.statistic.emote.command.handler;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.command.handler.CommandParameterIterators;
import dev.sheldan.abstracto.core.command.handler.provided.EmoteParameterHandler;
import dev.sheldan.abstracto.core.command.service.CommandService;
import dev.sheldan.abstracto.statistic.emote.command.parameter.handler.TrackedEmoteParameterHandler;
import dev.sheldan.abstracto.statistic.emote.model.database.TrackedEmote;
import dev.sheldan.abstracto.statistic.emote.service.TrackedEmoteService;
@@ -31,6 +34,9 @@ public class TrackedEmoteParameterHandlerTest {
@Mock
private TrackedEmoteService trackedEmoteService;
@Mock
private CommandService commandService;
@Mock
private Message contextMessage;
@@ -40,6 +46,15 @@ public class TrackedEmoteParameterHandlerTest {
@Mock
private Guild guild;
@Mock
private Parameter parameter;
@Mock
private Parameter parameter2;
@Mock
private Command command;
@Mock
private TrackedEmote trackedEmote;
@@ -60,9 +75,10 @@ public class TrackedEmoteParameterHandlerTest {
when(contextMessage.getGuild()).thenReturn(guild);
Emote emote = Mockito.mock(Emote.class);
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(emote);
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(emote);
when(trackedEmoteService.getFakeTrackedEmote(emote, guild)).thenReturn(trackedEmote);
TrackedEmote parsedEmote = (TrackedEmote) testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
TrackedEmote parsedEmote = (TrackedEmote) testUnit.handle(input, iterators, parameter, contextMessage, command);
Assert.assertEquals(trackedEmote, parsedEmote);
}
@@ -72,9 +88,10 @@ public class TrackedEmoteParameterHandlerTest {
when(contextMessage.getGuild()).thenReturn(guild);
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(input.getValue()).thenReturn(emoteId.toString());
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(trackedEmoteService.getFakeTrackedEmote(emoteId, guild)).thenReturn(trackedEmote);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(null);
TrackedEmote parsedEmote = (TrackedEmote) testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(null);
TrackedEmote parsedEmote = (TrackedEmote) testUnit.handle(input, iterators, parameter, contextMessage, command);
verify(trackedEmoteService, times(0)).getFakeTrackedEmote(any(Emote.class), eq(guild));
Assert.assertEquals(trackedEmote, parsedEmote);
}
@@ -83,7 +100,8 @@ public class TrackedEmoteParameterHandlerTest {
public void testWithIllegalInput() {
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(input.getValue()).thenReturn(WRONG_FORMATTED_INPUT);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(null);
testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(null);
testUnit.handle(input, iterators, parameter, contextMessage, command);
}
}

View File

@@ -1,8 +1,11 @@
package dev.sheldan.abstracto.statistic.emote.command.handler;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.command.handler.CommandParameterIterators;
import dev.sheldan.abstracto.core.command.handler.provided.EmoteParameterHandler;
import dev.sheldan.abstracto.core.command.service.CommandService;
import dev.sheldan.abstracto.statistic.emote.command.parameter.TrackEmoteParameter;
import dev.sheldan.abstracto.statistic.emote.command.parameter.handler.TrackedEmoteParameterParameterHandler;
import dev.sheldan.abstracto.statistic.emote.model.database.TrackedEmote;
@@ -34,6 +37,9 @@ public class TrackedEmoteParameterParameterHandlerTest {
@Mock
private TrackedEmoteService trackedEmoteService;
@Mock
private CommandService commandService;
@Mock
private Message contextMessage;
@@ -46,6 +52,15 @@ public class TrackedEmoteParameterParameterHandlerTest {
@Mock
private TrackedEmote trackedEmote;
@Mock
private Parameter parameter;
@Mock
private Parameter parameter2;
@Mock
private Command command;
private static final String WRONG_FORMATTED_INPUT = "input";
@Test
@@ -63,9 +78,10 @@ public class TrackedEmoteParameterParameterHandlerTest {
when(contextMessage.getGuild()).thenReturn(guild);
Emote emote = Mockito.mock(Emote.class);
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(emote);
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(emote);
when(trackedEmoteService.getFakeTrackedEmote(emote, guild)).thenReturn(trackedEmote);
TrackEmoteParameter parsedEmote = (TrackEmoteParameter) testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
TrackEmoteParameter parsedEmote = (TrackEmoteParameter) testUnit.handle(input, iterators, parameter, contextMessage, command);
Assert.assertEquals(trackedEmote, parsedEmote.getTrackedEmote());
Assert.assertEquals(emote, parsedEmote.getEmote());
}
@@ -77,8 +93,9 @@ public class TrackedEmoteParameterParameterHandlerTest {
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(input.getValue()).thenReturn(emoteId.toString());
when(trackedEmoteService.getFakeTrackedEmote(emoteId, guild)).thenReturn(trackedEmote);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(null);
TrackEmoteParameter parsedEmote = (TrackEmoteParameter) testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(null);
TrackEmoteParameter parsedEmote = (TrackEmoteParameter) testUnit.handle(input, iterators, parameter, contextMessage, command);
verify(trackedEmoteService, times(0)).getFakeTrackedEmote(any(Emote.class), eq(guild));
Assert.assertEquals(trackedEmote, parsedEmote.getTrackedEmote());
}
@@ -87,8 +104,9 @@ public class TrackedEmoteParameterParameterHandlerTest {
public void testWithIllegalInput() {
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(input.getValue()).thenReturn(WRONG_FORMATTED_INPUT);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(null);
testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(null);
testUnit.handle(input, iterators, parameter, contextMessage, command);
}
}