refactored the naming in channel service

added initial version of closing and logging the thread
This commit is contained in:
Sheldan
2020-05-06 18:32:08 +02:00
parent 54327c3e58
commit ad1cbb54fd
37 changed files with 392 additions and 64 deletions

View File

@@ -43,7 +43,7 @@ public class ListChannelGroups extends AbstractConditionableCommand {
ListChannelGroupsModel template = (ListChannelGroupsModel) ContextConverter.fromCommandContext(commandContext, ListChannelGroupsModel.class);
template.setGroups(convertAChannelGroupToChannelGroupChannel(channelGroups));
MessageToSend response = templateService.renderEmbedTemplate("listChannelGroups_response", template);
channelService.sendMessageToEndInTextChannel(response, commandContext.getChannel());
channelService.sendMessageToSendToChannel(response, commandContext.getChannel());
return CommandResult.fromSuccess();
}

View File

@@ -41,7 +41,7 @@ public class Disable extends AbstractConditionableCommand {
EnableModel model = (EnableModel) ContextConverter.fromCommandContext(commandContext, EnableModel.class);
model.setFeatures(featureFlagService.getAllFeatures());
String response = templateService.renderTemplate("disable_features_response", model);
channelService.sendTextInAChannel(response, commandContext.getChannel());
channelService.sendTextToChannelNoFuture(response, commandContext.getChannel());
} else {
String flagKey = (String) commandContext.getParameters().getParameters().get(0);
FeatureConfig feature = featureFlagService.getFeatureDisplayForFeature(flagKey);

View File

@@ -40,7 +40,7 @@ public class Enable extends AbstractConditionableCommand {
EnableModel model = (EnableModel) ContextConverter.fromCommandContext(commandContext, EnableModel.class);
model.setFeatures(featureFlagService.getAllFeatures());
String response = templateService.renderTemplate("enable_features_response", model);
channelService.sendTextInAChannel(response, commandContext.getChannel());
channelService.sendTextToChannelNoFuture(response, commandContext.getChannel());
} else {
String flagKey = (String) commandContext.getParameters().getParameters().get(0);
FeatureConfig feature = featureFlagService.getFeatureDisplayForFeature(flagKey);

View File

@@ -42,7 +42,7 @@ public class Features extends AbstractConditionableCommand {
FeaturesModel featuresModel = (FeaturesModel) ContextConverter.fromCommandContext(commandContext, FeaturesModel.class);
featuresModel.setFeatures(featureFlagConverter.fromFeatureFlags(features));
MessageToSend messageToSend = templateService.renderEmbedTemplate("features_response", featuresModel);
channelService.sendMessageToEndInTextChannel(messageToSend, commandContext.getChannel());
channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel());
return CommandResult.fromSuccess();
}

View File

@@ -59,7 +59,7 @@ public class Help implements Command {
HelpModuleOverviewModel model = (HelpModuleOverviewModel) ContextConverter.fromCommandContext(commandContext, HelpModuleOverviewModel.class);
model.setModules(subModules);
MessageToSend messageToSend = templateService.renderEmbedTemplate("help_module_overview_response", model);
channelService.sendMessageToEndInTextChannel(messageToSend, commandContext.getChannel());
channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel());
} else {
String parameter = (String) parameters.get(0);
if(moduleService.moduleExists(parameter)){
@@ -70,7 +70,7 @@ public class Help implements Command {
model.setModule(module);
model.setSubModules(subModules);
MessageToSend messageToSend = templateService.renderEmbedTemplate("help_module_details_response", model);
channelService.sendMessageToEndInTextChannel(messageToSend, commandContext.getChannel());
channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel());
} else if(commandRegistry.commandExists(parameter)) {
Command command = commandRegistry.getCommandByName(parameter);
ACommand aCommand = commandManagementService.findCommandByName(parameter);
@@ -83,7 +83,7 @@ public class Help implements Command {
}
model.setCommand(command.getConfiguration());
MessageToSend messageToSend = templateService.renderEmbedTemplate("help_command_details_response", model);
channelService.sendMessageToEndInTextChannel(messageToSend, commandContext.getChannel());
channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel());
}
}
return CommandResult.fromSuccess();

View File

@@ -6,13 +6,16 @@ import dev.sheldan.abstracto.core.exception.GuildException;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.templating.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.requests.restaction.MessageAction;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.nio.channels.Channel;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@@ -25,23 +28,26 @@ public class ChannelServiceBean implements ChannelService {
@Autowired
private BotService botService;
@Autowired
private TemplateService templateService;
@Override
public void sendTextInAChannel(String text, AChannel channel) {
sendTextInAChannelFuture(text, channel);
public void sendTextToAChannelNoFuture(String text, AChannel channel) {
sendTextToAChannel(text, channel);
}
@Override
public void sendTextInAChannel(String text, MessageChannel channel) {
sendTextInAChannelFuture(text, channel);
public void sendTextToChannelNoFuture(String text, MessageChannel channel) {
sendTextToChannel(text, channel);
}
@Override
public CompletableFuture<Message> sendTextInAChannelFuture(String text, AChannel channel) {
public CompletableFuture<Message> sendTextToAChannel(String text, AChannel channel) {
Guild guild = botService.getInstance().getGuildById(channel.getServer().getId());
if (guild != null) {
TextChannel textChannel = guild.getTextChannelById(channel.getId());
if(textChannel != null) {
return sendTextInAChannelFuture(text, textChannel);
return sendTextToChannel(text, textChannel);
} else {
log.error("Channel {} to post towards was not found in server {}", channel.getId(), channel.getServer().getId());
throw new ChannelException(String.format("Channel %s to post to not found.", channel.getId()));
@@ -53,17 +59,32 @@ public class ChannelServiceBean implements ChannelService {
}
@Override
public CompletableFuture<Message> sendTextInAChannelFuture(String text, MessageChannel channel) {
public CompletableFuture<Message> sendMessageToAChannel(Message message, AChannel channel) {
Optional<TextChannel> textChannelOpt = botService.getTextChannelFromServer(channel.getServer().getId(), channel.getId());
if(textChannelOpt.isPresent()) {
TextChannel textChannel = textChannelOpt.get();
return sendMessageToChannel(message, textChannel);
}
throw new ChannelException(String.format("Channel %s in guild %s not found.", channel.getId(), channel.getServer().getId()));
}
@Override
public CompletableFuture<Message> sendMessageToChannel(Message message, MessageChannel channel) {
return channel.sendMessage(message).submit();
}
@Override
public CompletableFuture<Message> sendTextToChannel(String text, MessageChannel channel) {
return channel.sendMessage(text).submit();
}
@Override
public CompletableFuture<Message> sendEmbedInAChannelFuture(MessageEmbed embed, AChannel channel) {
public CompletableFuture<Message> sendEmbedToAChannel(MessageEmbed embed, AChannel channel) {
Guild guild = botService.getInstance().getGuildById(channel.getServer().getId());
if (guild != null) {
TextChannel textChannel = guild.getTextChannelById(channel.getId());
if(textChannel != null) {
return sendEmbedInAChannelFuture(embed, textChannel);
return sendEmbedToChannel(embed, textChannel);
} else {
log.error("Channel {} to post towards was not found in server {}", channel.getId(), channel.getServer().getId());
throw new ChannelException(String.format("Channel %s to post to not found.", channel.getId()));
@@ -75,26 +96,26 @@ public class ChannelServiceBean implements ChannelService {
}
@Override
public CompletableFuture<Message> sendEmbedInAChannelFuture(MessageEmbed embed, MessageChannel channel) {
public CompletableFuture<Message> sendEmbedToChannel(MessageEmbed embed, MessageChannel channel) {
return channel.sendMessage(embed).submit();
}
@Override
public List<CompletableFuture<Message>> sendMessageToEndInAChannel(MessageToSend messageToSend, AChannel channel) {
public List<CompletableFuture<Message>> sendMessageToSendToAChannel(MessageToSend messageToSend, AChannel channel) {
Optional<TextChannel> textChannelFromServer = botService.getTextChannelFromServer(channel.getServer().getId(), channel.getId());
if(textChannelFromServer.isPresent()) {
return sendMessageToEndInTextChannel(messageToSend, textChannelFromServer.get());
return sendMessageToSendToChannel(messageToSend, textChannelFromServer.get());
}
throw new ChannelException(String.format("Channel %s was not found.", channel.getId()));
}
@Override
public List<CompletableFuture<Message>> sendMessageToEndInTextChannel(MessageToSend messageToSend, MessageChannel textChannel) {
public List<CompletableFuture<Message>> sendMessageToSendToChannel(MessageToSend messageToSend, MessageChannel textChannel) {
String messageText = messageToSend.getMessage();
List<CompletableFuture<Message>> futures = new ArrayList<>();
if(StringUtils.isBlank(messageText)) {
messageToSend.getEmbeds().forEach(embed ->
futures.add(sendEmbedInAChannelFuture(embed, textChannel))
futures.add(sendEmbedToChannel(embed, textChannel))
);
} else {
MessageAction messageAction = textChannel.sendMessage(messageText);
@@ -102,7 +123,7 @@ public class ChannelServiceBean implements ChannelService {
CompletableFuture<Message> messageFuture = messageAction.embed(messageToSend.getEmbeds().get(0)).submit();
futures.add(messageFuture);
messageToSend.getEmbeds().stream().skip(1).forEach(embed ->
futures.add(sendEmbedInAChannelFuture(embed, textChannel))
futures.add(sendEmbedToChannel(embed, textChannel))
);
} else {
futures.add(messageAction.submit());
@@ -145,6 +166,22 @@ public class ChannelServiceBean implements ChannelService {
messageAction.queue();
}
@Override
public CompletableFuture<Void> deleteTextChannel(AChannel channel) {
TextChannel textChannelById = botService.getInstance().getTextChannelById(channel.getId());
if(textChannelById != null) {
return textChannelById.delete().submit();
}
throw new ChannelException(String.format("Failed to delete channel %s in server %s", channel.getId(), channel.getServer().getId()));
}
@Override
@Transactional
public List<CompletableFuture<Message>> sendTemplateInChannel(String templateKey, Object model, MessageChannel channel) {
MessageToSend messageToSend = templateService.renderEmbedTemplate(templateKey, model);
return sendMessageToSendToChannel(messageToSend, channel);
}
@Override
public CompletableFuture<TextChannel> createTextChannel(String name, AServer server, Long categoryId) {
Optional<Guild> guildById = botService.getGuildById(server.getId());

View File

@@ -2,6 +2,7 @@ package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.EmoteException;
import dev.sheldan.abstracto.core.exception.GuildException;
import dev.sheldan.abstracto.core.models.ServerChannelMessage;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.management.EmoteManagementService;
@@ -13,6 +14,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@@ -70,7 +73,7 @@ public class MessageServiceBean implements MessageService {
@Override
public CompletableFuture<Message> createStatusMessage(MessageToSend messageToSend, AChannel channel) {
return channelService.sendMessageToEndInAChannel(messageToSend, channel).get(0);
return channelService.sendMessageToSendToAChannel(messageToSend, channel).get(0);
}
@Override
@@ -103,6 +106,6 @@ public class MessageServiceBean implements MessageService {
@Transactional
public void sendFeedbackAboutException(Throwable e, TextChannel feedbackChannel) {
channelService.sendTextInAChannel(String.format("Failed to send message: %s", e.getMessage()), feedbackChannel);
channelService.sendTextToChannelNoFuture(String.format("Failed to send message: %s", e.getMessage()), feedbackChannel);
}
}

View File

@@ -38,7 +38,7 @@ public class PostTargetServiceBean implements PostTargetService {
@Override
public CompletableFuture<Message> sendTextInPostTarget(String text, PostTarget target) {
return channelService.sendTextInAChannelFuture(text, target.getChannelReference());
return channelService.sendTextToAChannel(text, target.getChannelReference());
}
@Override
@@ -86,6 +86,17 @@ public class PostTargetServiceBean implements PostTargetService {
return this.sendEmbedInPostTarget(embed, postTarget);
}
@Override
public CompletableFuture<Message> sendMessageInPostTarget(Message message, String postTargetName, Long serverId) {
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);
return sendMessageInPostTarget(message, postTarget);
}
@Override
public CompletableFuture<Message> sendMessageInPostTarget(Message message, PostTarget target) {
return channelService.sendMessageToAChannel(message, target.getChannelReference());
}
@Override
public List<CompletableFuture<Message>> sendEmbedInPostTarget(MessageToSend message, String postTargetName, Long serverId) {
PostTarget postTarget = this.getPostTarget(postTargetName, serverId);
@@ -95,7 +106,7 @@ public class PostTargetServiceBean implements PostTargetService {
@Override
public List<CompletableFuture<Message>> sendEmbedInPostTarget(MessageToSend message, PostTarget target) {
TextChannel textChannelForPostTarget = getTextChannelForPostTarget(target);
return channelService.sendMessageToEndInTextChannel(message, textChannelForPostTarget);
return channelService.sendMessageToSendToChannel(message, textChannelForPostTarget);
}
@Override