mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-15 12:10:21 +00:00
[AB-365] introducing slash commands for a selection of commands
adding method for pinning a message moving suggestion to correct deployment
This commit is contained in:
@@ -5,20 +5,24 @@ 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.config.SlashCommandConfig;
|
||||
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.slash.parameter.SlashCommandParameterService;
|
||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||
import dev.sheldan.abstracto.core.interaction.InteractionService;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.core.service.ProfanityService;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.webservices.config.WebServicesSlashCommandNames;
|
||||
import dev.sheldan.abstracto.webservices.config.WebserviceFeatureDefinition;
|
||||
import dev.sheldan.abstracto.webservices.urban.model.UrbanDefinition;
|
||||
import dev.sheldan.abstracto.webservices.urban.model.UrbanResponseModel;
|
||||
import dev.sheldan.abstracto.webservices.urban.service.UrbanService;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -31,6 +35,10 @@ import java.util.concurrent.CompletableFuture;
|
||||
@Component
|
||||
public class UrbanDefine extends AbstractConditionableCommand {
|
||||
|
||||
private static final String URBAN_DEFINE_RESPONSE_MODEL_TEMPLATE_KEY = "urban_define_response_model";
|
||||
private static final String URBAN_DEFINE_COMMAND = "urbanDefine";
|
||||
private static final String SEARCH_QUERY_PARAMETER = "searchQuery";
|
||||
|
||||
@Autowired
|
||||
private UrbanService urbanService;
|
||||
|
||||
@@ -43,20 +51,17 @@ public class UrbanDefine extends AbstractConditionableCommand {
|
||||
@Autowired
|
||||
private ProfanityService profanityService;
|
||||
|
||||
private static final String URBAN_DEFINE_RESPONSE_MODEL_TEMPLATE_KEY = "urban_define_response_model";
|
||||
@Autowired
|
||||
private SlashCommandParameterService slashCommandParameterService;
|
||||
|
||||
@Autowired
|
||||
private InteractionService interactionService;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
String parameter = (String) commandContext.getParameters().getParameters().get(0);
|
||||
try {
|
||||
UrbanDefinition definition = urbanService.getUrbanDefinition(parameter);
|
||||
UrbanResponseModel model = (UrbanResponseModel) ContextConverter.slimFromCommandContext(commandContext, UrbanResponseModel.class);
|
||||
String sanitizedDefinition = profanityService.replaceProfanities(definition.getDefinition(), commandContext.getGuild().getIdLong());
|
||||
String sanitizedExample = profanityService.replaceProfanities(definition.getExample(), commandContext.getGuild().getIdLong());
|
||||
definition.setDefinition(sanitizedDefinition);
|
||||
definition.setExample(sanitizedExample);
|
||||
model.setDefinition(definition);
|
||||
MessageToSend message = templateService.renderEmbedTemplate(URBAN_DEFINE_RESPONSE_MODEL_TEMPLATE_KEY, model, commandContext.getGuild().getIdLong());
|
||||
MessageToSend message = getMessageToSend(commandContext.getGuild().getIdLong(), parameter);
|
||||
return FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(message, commandContext.getChannel()))
|
||||
.thenApply(unused -> CommandResult.fromSuccess());
|
||||
} catch (IOException e) {
|
||||
@@ -64,16 +69,57 @@ public class UrbanDefine extends AbstractConditionableCommand {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
|
||||
String query = slashCommandParameterService.getCommandOption(SEARCH_QUERY_PARAMETER, event, String.class);
|
||||
try {
|
||||
MessageToSend messageToSend = getMessageToSend(event.getGuild().getIdLong(), query);
|
||||
return interactionService.replyMessageToSend(messageToSend, event)
|
||||
.thenApply(interactionHook -> CommandResult.fromSuccess());
|
||||
} catch (IOException e) {
|
||||
throw new AbstractoRunTimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private MessageToSend getMessageToSend(Long serverId, String parameter) throws IOException {
|
||||
UrbanDefinition definition = urbanService.getUrbanDefinition(parameter);
|
||||
String sanitizedDefinition = profanityService.replaceProfanities(definition.getDefinition(),serverId);
|
||||
String sanitizedExample = profanityService.replaceProfanities(definition.getExample(), serverId);
|
||||
definition.setDefinition(sanitizedDefinition);
|
||||
definition.setExample(sanitizedExample);
|
||||
UrbanResponseModel model = UrbanResponseModel
|
||||
.builder()
|
||||
.definition(definition)
|
||||
.build();
|
||||
return templateService.renderEmbedTemplate(URBAN_DEFINE_RESPONSE_MODEL_TEMPLATE_KEY, model, serverId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandConfiguration getConfiguration() {
|
||||
List<Parameter> parameters = new ArrayList<>();
|
||||
parameters.add(Parameter.builder().name("searchQuery").type(String.class).remainder(true).templated(true).build());
|
||||
Parameter searchQueryParameter = Parameter
|
||||
.builder()
|
||||
.name(SEARCH_QUERY_PARAMETER)
|
||||
.type(String.class)
|
||||
.remainder(true)
|
||||
.templated(true)
|
||||
.build();
|
||||
parameters.add(searchQueryParameter);
|
||||
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
|
||||
List<String> aliases = Arrays.asList("ud");
|
||||
|
||||
SlashCommandConfig slashCommandConfig = SlashCommandConfig
|
||||
.builder()
|
||||
.enabled(true)
|
||||
.rootCommandName(WebServicesSlashCommandNames.URBAN)
|
||||
.commandName("search")
|
||||
.build();
|
||||
|
||||
return CommandConfiguration.builder()
|
||||
.name("urbanDefine")
|
||||
.name(URBAN_DEFINE_COMMAND)
|
||||
.module(UtilityModuleDefinition.UTILITY)
|
||||
.templated(true)
|
||||
.slashCommandConfig(slashCommandConfig)
|
||||
.async(true)
|
||||
.aliases(aliases)
|
||||
.supportsEmbedException(true)
|
||||
|
||||
@@ -5,20 +5,24 @@ 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.config.SlashCommandConfig;
|
||||
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.slash.parameter.SlashCommandParameterService;
|
||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||
import dev.sheldan.abstracto.core.interaction.InteractionService;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.core.service.FeatureModeService;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.webservices.config.WebServicesSlashCommandNames;
|
||||
import dev.sheldan.abstracto.webservices.config.WebserviceFeatureDefinition;
|
||||
import dev.sheldan.abstracto.webservices.youtube.config.YoutubeWebServiceFeatureMode;
|
||||
import dev.sheldan.abstracto.webservices.youtube.model.YoutubeVideo;
|
||||
import dev.sheldan.abstracto.webservices.youtube.model.command.YoutubeVideoSearchCommandModel;
|
||||
import dev.sheldan.abstracto.webservices.youtube.service.YoutubeSearchService;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -32,6 +36,9 @@ public class YoutubeVideoSearch extends AbstractConditionableCommand {
|
||||
|
||||
public static final String YOUTUBE_SEARCH_COMMAND_RESPONSE_TEMPLATE_KEY = "youtube_search_command_response";
|
||||
public static final String YOUTUBE_SEARCH_COMMAND_RESPONSE_LINK_TEMPLATE_KEY = "youtube_search_command_response_link";
|
||||
private static final String SEARCH_QUERY_PARAMETER = "searchQuery";
|
||||
private static final String YOUTUBE_SEARCH_COMMAND = "youtubeSearch";
|
||||
|
||||
@Autowired
|
||||
private YoutubeSearchService youtubeSearchService;
|
||||
|
||||
@@ -44,12 +51,20 @@ public class YoutubeVideoSearch extends AbstractConditionableCommand {
|
||||
@Autowired
|
||||
private FeatureModeService featureModeService;
|
||||
|
||||
@Autowired
|
||||
private SlashCommandParameterService slashCommandParameterService;
|
||||
|
||||
@Autowired
|
||||
private InteractionService interactionService;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
String query = (String) commandContext.getParameters().getParameters().get(0);
|
||||
YoutubeVideo foundVideo = youtubeSearchService.searchOneVideoForQuery(query);
|
||||
YoutubeVideoSearchCommandModel model = (YoutubeVideoSearchCommandModel) ContextConverter.slimFromCommandContext(commandContext, YoutubeVideoSearchCommandModel.class);
|
||||
model.setVideo(foundVideo);
|
||||
YoutubeVideoSearchCommandModel model = YoutubeVideoSearchCommandModel
|
||||
.builder()
|
||||
.video(foundVideo)
|
||||
.build();
|
||||
CompletableFuture<Void> infoEmbedFuture;
|
||||
if(featureModeService.featureModeActive(WebserviceFeatureDefinition.YOUTUBE, commandContext.getGuild().getIdLong(), YoutubeWebServiceFeatureMode.VIDEO_DETAILS)) {
|
||||
MessageToSend message = templateService.renderEmbedTemplate(YOUTUBE_SEARCH_COMMAND_RESPONSE_TEMPLATE_KEY, model, commandContext.getGuild().getIdLong());
|
||||
@@ -63,15 +78,56 @@ public class YoutubeVideoSearch extends AbstractConditionableCommand {
|
||||
.thenApply(unused -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
|
||||
String query = slashCommandParameterService.getCommandOption(SEARCH_QUERY_PARAMETER, event, String.class);
|
||||
YoutubeVideo foundVideo = youtubeSearchService.searchOneVideoForQuery(query);
|
||||
YoutubeVideoSearchCommandModel model = YoutubeVideoSearchCommandModel
|
||||
.builder()
|
||||
.video(foundVideo)
|
||||
.build();
|
||||
boolean sendInfo = featureModeService.featureModeActive(WebserviceFeatureDefinition.YOUTUBE, event.getGuild().getIdLong(), YoutubeWebServiceFeatureMode.VIDEO_DETAILS);
|
||||
MessageToSend linkEmbed = templateService.renderEmbedTemplate(YOUTUBE_SEARCH_COMMAND_RESPONSE_LINK_TEMPLATE_KEY, model, event.getGuild().getIdLong());
|
||||
MessageToSend infoEmbed = templateService.renderEmbedTemplate(YOUTUBE_SEARCH_COMMAND_RESPONSE_TEMPLATE_KEY, model, event.getGuild().getIdLong());
|
||||
return interactionService.replyMessageToSend(linkEmbed, event)
|
||||
.thenCompose(interactionHook -> {
|
||||
if(sendInfo) {
|
||||
return FutureUtils.toSingleFutureGeneric(interactionService.sendMessageToInteraction(infoEmbed, interactionHook));
|
||||
} else {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
})
|
||||
.thenApply(o -> CommandResult.fromSuccess());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandConfiguration getConfiguration() {
|
||||
List<Parameter> parameters = new ArrayList<>();
|
||||
parameters.add(Parameter.builder().name("searchQuery").type(String.class).remainder(true).templated(true).build());
|
||||
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
|
||||
Parameter queryParameter = Parameter
|
||||
.builder()
|
||||
.name(SEARCH_QUERY_PARAMETER)
|
||||
.type(String.class)
|
||||
.remainder(true)
|
||||
.templated(true)
|
||||
.build();
|
||||
parameters.add(queryParameter);
|
||||
HelpInfo helpInfo = HelpInfo
|
||||
.builder()
|
||||
.templated(true)
|
||||
.build();
|
||||
List<String> aliases = Arrays.asList("yt");
|
||||
|
||||
SlashCommandConfig slashCommandConfig = SlashCommandConfig
|
||||
.builder()
|
||||
.enabled(true)
|
||||
.rootCommandName(WebServicesSlashCommandNames.YOUTUBE)
|
||||
.commandName("search")
|
||||
.build();
|
||||
|
||||
return CommandConfiguration.builder()
|
||||
.name("youtubeSearch")
|
||||
.name(YOUTUBE_SEARCH_COMMAND)
|
||||
.module(UtilityModuleDefinition.UTILITY)
|
||||
.slashCommandConfig(slashCommandConfig)
|
||||
.templated(true)
|
||||
.async(true)
|
||||
.aliases(aliases)
|
||||
|
||||
Reference in New Issue
Block a user