mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-05-19 05:50:43 +00:00
[AB-xxx] adding grrr image generation
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
package dev.sheldan.abstracto.imagegeneration.command;
|
||||
|
||||
|
||||
import dev.sheldan.abstracto.core.command.UtilityModuleDefinition;
|
||||
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.UserCommandConfig;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||
import dev.sheldan.abstracto.core.interaction.InteractionService;
|
||||
import dev.sheldan.abstracto.core.interaction.slash.SlashCommandConfig;
|
||||
import dev.sheldan.abstracto.core.interaction.slash.parameter.SlashCommandParameterService;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import dev.sheldan.abstracto.core.templating.model.AttachedFile;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import dev.sheldan.abstracto.core.utils.FileService;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import dev.sheldan.abstracto.imagegeneration.config.ImageGenerationFeatureDefinition;
|
||||
import dev.sheldan.abstracto.imagegeneration.config.ImageGenerationSlashCommandNames;
|
||||
import dev.sheldan.abstracto.imagegeneration.service.ImageGenerationService;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Grrr extends AbstractConditionableCommand {
|
||||
|
||||
@Autowired
|
||||
private ImageGenerationService imageGenerationService;
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Autowired
|
||||
private ChannelService channelService;
|
||||
|
||||
@Autowired
|
||||
private FileService fileService;
|
||||
|
||||
@Autowired
|
||||
private InteractionService interactionService;
|
||||
|
||||
@Autowired
|
||||
private SlashCommandParameterService slashCommandParameterService;
|
||||
|
||||
private static final String GRRR_EMBED_TEMPLATE_KEY = "grr_response";
|
||||
public static final String TEXT_PARAMETER_KEY = "text";
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||
String text = (String) parameters.get(0);
|
||||
File grrrFile = imageGenerationService.getGrrrImage(text);
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(GRRR_EMBED_TEMPLATE_KEY, new Object(), commandContext.getGuild().getIdLong());
|
||||
// template support does not support binary files
|
||||
AttachedFile file = AttachedFile
|
||||
.builder()
|
||||
.file(grrrFile)
|
||||
.fileName("grrr.png")
|
||||
.build();
|
||||
messageToSend.getAttachedFiles().add(file);
|
||||
return FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel()))
|
||||
.thenAccept(unused -> fileService.safeDeleteIgnoreException(messageToSend.getAttachedFiles().get(0).getFile()))
|
||||
.thenApply(unused -> CommandResult.fromIgnored());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
|
||||
event.deferReply().queue();
|
||||
String text = slashCommandParameterService.getCommandOption(TEXT_PARAMETER_KEY, event, String.class);
|
||||
File grrrFile = imageGenerationService.getGrrrImage(text);
|
||||
MessageToSend messageToSend = templateService.renderEmbedTemplate(GRRR_EMBED_TEMPLATE_KEY, new Object(), event.getGuild().getIdLong());
|
||||
// template support does not support binary files
|
||||
AttachedFile file = AttachedFile
|
||||
.builder()
|
||||
.file(grrrFile)
|
||||
.fileName("grrr.png")
|
||||
.build();
|
||||
messageToSend.getAttachedFiles().add(file);
|
||||
return FutureUtils.toSingleFutureGeneric(interactionService.sendMessageToInteraction(messageToSend, event.getHook()))
|
||||
.thenAccept(unused -> fileService.safeDeleteIgnoreException(messageToSend.getAttachedFiles().get(0).getFile()))
|
||||
.thenApply(unused -> CommandResult.fromIgnored());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandConfiguration getConfiguration() {
|
||||
List<Parameter> parameters = new ArrayList<>();
|
||||
Parameter textParameter = Parameter
|
||||
.builder()
|
||||
.name(TEXT_PARAMETER_KEY)
|
||||
.type(String.class)
|
||||
.templated(true)
|
||||
.build();
|
||||
parameters.add(textParameter);
|
||||
HelpInfo helpInfo = HelpInfo
|
||||
.builder()
|
||||
.templated(true)
|
||||
.build();
|
||||
|
||||
SlashCommandConfig slashCommandConfig = SlashCommandConfig
|
||||
.builder()
|
||||
.enabled(true)
|
||||
.userInstallable(true)
|
||||
.userCommandConfig(UserCommandConfig.all())
|
||||
.rootCommandName(ImageGenerationSlashCommandNames.IMAGE_GENERATION)
|
||||
.groupName("memes")
|
||||
.commandName("grrr")
|
||||
.build();
|
||||
|
||||
return CommandConfiguration.builder()
|
||||
.name("grrr")
|
||||
.module(UtilityModuleDefinition.UTILITY)
|
||||
.templated(true)
|
||||
.supportsEmbedException(true)
|
||||
.async(true)
|
||||
.slashCommandConfig(slashCommandConfig)
|
||||
.parameters(parameters)
|
||||
.help(helpInfo)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FeatureDefinition getFeature() {
|
||||
return ImageGenerationFeatureDefinition.IMAGE_GENERATION;
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,10 @@ public class ImageGenerationServiceBean implements ImageGenerationService {
|
||||
@Value("${abstracto.feature.imagegeneration.amongusText.url}")
|
||||
private String amongusTextUrl;
|
||||
|
||||
|
||||
@Value("${abstracto.feature.imagegeneration.grrr.url}")
|
||||
private String grrrUrl;
|
||||
|
||||
@Autowired
|
||||
private HttpService httpService;
|
||||
|
||||
@@ -68,4 +72,13 @@ public class ImageGenerationServiceBean implements ImageGenerationService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getGrrrImage(String text) {
|
||||
try {
|
||||
return httpService.downloadFileToTempFile(grrrUrl.replace("{1}", text));
|
||||
} catch (IOException | RequestException e) {
|
||||
throw new AbstractoRunTimeException(String.format("Failed to download grrr image for text %s with error %s", text, e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,3 +11,5 @@ abstracto.feature.imagegeneration.bonk.url=http://${PRIVATE_REST_API_HOST}:${PRI
|
||||
abstracto.feature.imagegeneration.bonk.imagesize=128
|
||||
|
||||
abstracto.feature.imagegeneration.amongusText.url=http://${PRIVATE_REST_API_HOST}:${PRIVATE_REST_API_PORT}/memes/amongus/text?text={1}
|
||||
|
||||
abstracto.feature.imagegeneration.grrr.url=http://${PRIVATE_REST_API_HOST}:${PRIVATE_REST_API_PORT}/memes/grrr/text?text={1}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.26.xsd" >
|
||||
<include file="seedData/data.xml" relativeToChangelogFile="true"/>
|
||||
</databaseChangeLog>
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.26.xsd" >
|
||||
<property name="utilityModule" value="(SELECT id FROM module WHERE name = 'utility')"/>
|
||||
<property name="imageGenerationFeature" value="(SELECT id FROM feature WHERE key = 'imageGeneration')"/>
|
||||
|
||||
<changeSet author="Sheldan" id="grrr-command">
|
||||
<insert tableName="command">
|
||||
<column name="name" value="grrr"/>
|
||||
<column name="module_id" valueComputed="${utilityModule}"/>
|
||||
<column name="feature_id" valueComputed="${imageGenerationFeature}"/>
|
||||
</insert>
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.26.xsd" >
|
||||
<include file="command.xml" relativeToChangelogFile="true"/>
|
||||
</databaseChangeLog>
|
||||
@@ -5,4 +5,5 @@
|
||||
<include file="1.5.15/collection.xml" relativeToChangelogFile="true"/>
|
||||
<include file="1.5.19/collection.xml" relativeToChangelogFile="true"/>
|
||||
<include file="1.5.22/collection.xml" relativeToChangelogFile="true"/>
|
||||
<include file="1.6.26/collection.xml" relativeToChangelogFile="true"/>
|
||||
</databaseChangeLog>
|
||||
@@ -7,4 +7,5 @@ public interface ImageGenerationService {
|
||||
File getPatGif(String imageUrl);
|
||||
File getBonkGif(String imageUrl);
|
||||
File getAmongusTextImage(String text);
|
||||
File getGrrrImage(String text);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user