mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-27 23:09:05 +00:00
[AB-82] adding feature mode to make it configurable whether or not the youtube video will show details or not
adding convenience method to config service
This commit is contained in:
@@ -10,10 +10,12 @@ import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
|||||||
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
|
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
|
||||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
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.model.MessageToSend;
|
||||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||||
import dev.sheldan.abstracto.webservices.config.WebserviceFeatureDefinition;
|
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.YoutubeVideo;
|
||||||
import dev.sheldan.abstracto.webservices.youtube.model.command.YoutubeVideoSearchCommandModel;
|
import dev.sheldan.abstracto.webservices.youtube.model.command.YoutubeVideoSearchCommandModel;
|
||||||
import dev.sheldan.abstracto.webservices.youtube.service.YoutubeSearchService;
|
import dev.sheldan.abstracto.webservices.youtube.service.YoutubeSearchService;
|
||||||
@@ -37,15 +39,23 @@ public class YoutubeVideoSearch extends AbstractConditionableCommand {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ChannelService channelService;
|
private ChannelService channelService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FeatureModeService featureModeService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||||
String query = (String) commandContext.getParameters().getParameters().get(0);
|
String query = (String) commandContext.getParameters().getParameters().get(0);
|
||||||
YoutubeVideo foundVideo = youtubeSearchService.searchOneVideoForQuery(query);
|
YoutubeVideo foundVideo = youtubeSearchService.searchOneVideoForQuery(query);
|
||||||
YoutubeVideoSearchCommandModel model = (YoutubeVideoSearchCommandModel) ContextConverter.slimFromCommandContext(commandContext, YoutubeVideoSearchCommandModel.class);
|
YoutubeVideoSearchCommandModel model = (YoutubeVideoSearchCommandModel) ContextConverter.slimFromCommandContext(commandContext, YoutubeVideoSearchCommandModel.class);
|
||||||
model.setVideo(foundVideo);
|
model.setVideo(foundVideo);
|
||||||
|
CompletableFuture<Void> infoEmbedFuture;
|
||||||
|
if(featureModeService.featureModeActive(WebserviceFeatureDefinition.YOUTUBE, commandContext.getGuild().getIdLong(), YoutubeWebServiceFeatureMode.VIDEO_DETAILS)) {
|
||||||
MessageToSend message = templateService.renderEmbedTemplate("youtube_search_command_response", model);
|
MessageToSend message = templateService.renderEmbedTemplate("youtube_search_command_response", model);
|
||||||
|
infoEmbedFuture = FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(message, commandContext.getChannel()));
|
||||||
|
} else {
|
||||||
|
infoEmbedFuture = CompletableFuture.completedFuture(null);
|
||||||
|
}
|
||||||
MessageToSend linkEmbed = templateService.renderEmbedTemplate("youtube_search_command_response_link", model);
|
MessageToSend linkEmbed = templateService.renderEmbedTemplate("youtube_search_command_response_link", model);
|
||||||
CompletableFuture<Void> infoEmbedFuture = FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(message, commandContext.getChannel()));
|
|
||||||
CompletableFuture<Void> linkEmbedFuture = FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(linkEmbed, commandContext.getChannel()));
|
CompletableFuture<Void> linkEmbedFuture = FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(linkEmbed, commandContext.getChannel()));
|
||||||
return CompletableFuture.allOf(infoEmbedFuture, linkEmbedFuture)
|
return CompletableFuture.allOf(infoEmbedFuture, linkEmbedFuture)
|
||||||
.thenApply(unused -> CommandResult.fromSuccess());
|
.thenApply(unused -> CommandResult.fromSuccess());
|
||||||
|
|||||||
@@ -7,3 +7,7 @@ abstracto.featureFlags.urban.enabled=false
|
|||||||
abstracto.feature.youtube.apiKey=${YOUTUBE_API_KEY}
|
abstracto.feature.youtube.apiKey=${YOUTUBE_API_KEY}
|
||||||
|
|
||||||
abstracto.feature.webservices.urban.requestURL=https://api.urbandictionary.com/v0/define?term=%s
|
abstracto.feature.webservices.urban.requestURL=https://api.urbandictionary.com/v0/define?term=%s
|
||||||
|
|
||||||
|
abstracto.featureModes.videoDetails.featureName=youtube
|
||||||
|
abstracto.featureModes.videoDetails.mode=videoDetails
|
||||||
|
abstracto.featureModes.videoDetails.enabled=false
|
||||||
@@ -2,13 +2,22 @@ package dev.sheldan.abstracto.webservices.youtube.config;
|
|||||||
|
|
||||||
import dev.sheldan.abstracto.core.config.FeatureConfig;
|
import dev.sheldan.abstracto.core.config.FeatureConfig;
|
||||||
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
import dev.sheldan.abstracto.core.config.FeatureDefinition;
|
||||||
|
import dev.sheldan.abstracto.core.config.FeatureMode;
|
||||||
import dev.sheldan.abstracto.webservices.config.WebserviceFeatureDefinition;
|
import dev.sheldan.abstracto.webservices.config.WebserviceFeatureDefinition;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class YoutubeFeatureConfig implements FeatureConfig {
|
public class YoutubeFeatureConfig implements FeatureConfig {
|
||||||
@Override
|
@Override
|
||||||
public FeatureDefinition getFeature() {
|
public FeatureDefinition getFeature() {
|
||||||
return WebserviceFeatureDefinition.YOUTUBE;
|
return WebserviceFeatureDefinition.YOUTUBE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<FeatureMode> getAvailableModes() {
|
||||||
|
return Arrays.asList(YoutubeWebServiceFeatureMode.VIDEO_DETAILS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package dev.sheldan.abstracto.webservices.youtube.config;
|
||||||
|
|
||||||
|
import dev.sheldan.abstracto.core.config.FeatureMode;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public enum YoutubeWebServiceFeatureMode implements FeatureMode {
|
||||||
|
VIDEO_DETAILS("videoDetails");
|
||||||
|
|
||||||
|
private final String key;
|
||||||
|
|
||||||
|
YoutubeWebServiceFeatureMode(String key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import dev.sheldan.abstracto.core.models.database.AConfig;
|
|||||||
import dev.sheldan.abstracto.core.models.property.SystemConfigProperty;
|
import dev.sheldan.abstracto.core.models.property.SystemConfigProperty;
|
||||||
import dev.sheldan.abstracto.core.service.management.ConfigManagementService;
|
import dev.sheldan.abstracto.core.service.management.ConfigManagementService;
|
||||||
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
|
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
|
||||||
|
import org.apache.commons.lang3.BooleanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -73,6 +74,11 @@ public class ConfigServiceBean implements ConfigService {
|
|||||||
return config.getLongValue();
|
return config.getLongValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean getBooleanValueOrConfigDefault(String name, Long serverId) {
|
||||||
|
return BooleanUtils.toBoolean(getStringValueOrConfigDefault(name, serverId));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setDoubleValue(String name, Long serverId, Double value) {
|
public void setDoubleValue(String name, Long serverId, Double value) {
|
||||||
if(configManagementService.configExists(serverId, name)) {
|
if(configManagementService.configExists(serverId, name)) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ public interface ConfigService {
|
|||||||
String getStringValue(String name, Long serverId, String defaultValue);
|
String getStringValue(String name, Long serverId, String defaultValue);
|
||||||
String getStringValueOrConfigDefault(String name, Long serverId);
|
String getStringValueOrConfigDefault(String name, Long serverId);
|
||||||
Long getLongValue(String name, Long serverId, Long defaultValue);
|
Long getLongValue(String name, Long serverId, Long defaultValue);
|
||||||
|
Boolean getBooleanValueOrConfigDefault(String name, Long serverId);
|
||||||
AConfig setOrCreateConfigValue(Long serverId, String name, AConfig value);
|
AConfig setOrCreateConfigValue(Long serverId, String name, AConfig value);
|
||||||
void setDoubleValue(String name, Long serverId, Double value);
|
void setDoubleValue(String name, Long serverId, Double value);
|
||||||
void setLongValue(String name, Long serverId, Long value);
|
void setLongValue(String name, Long serverId, Long value);
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ Integrates different web APIs to be used via the bot.
|
|||||||
=== Youtube
|
=== Youtube
|
||||||
Feature key: `youtube`
|
Feature key: `youtube`
|
||||||
|
|
||||||
|
==== Feature modes
|
||||||
|
`videoDetails`:: if enabled, the video shown with video details. Disabled by default.
|
||||||
|
|
||||||
==== Command
|
==== Command
|
||||||
Search for a youtube video::
|
Search for a youtube video::
|
||||||
* Usage: `youtubeSearch <query>`
|
* Usage: `youtubeSearch <query>`
|
||||||
|
|||||||
Reference in New Issue
Block a user