mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-02 15:56:34 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa22009007 | ||
|
|
7b7fdf781a |
@@ -31,7 +31,7 @@ import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
|
||||
import net.dv8tion.jda.api.entities.emoji.CustomEmoji;
|
||||
import net.dv8tion.jda.api.entities.emoji.Emoji;
|
||||
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
|
||||
import net.dv8tion.jda.api.components.buttons.ButtonStyle;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -357,7 +357,9 @@ public class MessageEmbedServiceBean implements MessageEmbedService {
|
||||
|
||||
private Boolean shouldMentionReferencedAuthor(Message message) {
|
||||
if(message.getReferencedMessage() != null) {
|
||||
return message.getMentions().getMentions(Message.MentionType.USER).contains(message.getReferencedMessage().getAuthor());
|
||||
return message.getMentions().getMentions(Message.MentionType.USER)
|
||||
.stream()
|
||||
.anyMatch(user -> message.getReferencedMessage().getAuthor().getIdLong() == user.getIdLong());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -138,8 +138,12 @@ public abstract class StarboardListener {
|
||||
|
||||
protected void updateStarboardPost(CachedMessage message, AUserInAServer userReacting, boolean adding, StarboardPost starboardPost, List<AUserInAServer> userExceptAuthor) {
|
||||
starboardPost.setIgnored(false);
|
||||
// TODO handle futures correctly
|
||||
starboardService.updateStarboardPost(starboardPost, message, userExceptAuthor);
|
||||
starboardService.updateStarboardPost(starboardPost, message, userExceptAuthor)
|
||||
.thenAccept(unused -> log.info("Updated starboard post."))
|
||||
.exceptionally(throwable -> {
|
||||
log.error("Failed to update starboard post.", throwable);
|
||||
return null;
|
||||
});
|
||||
if(adding) {
|
||||
log.debug("Adding reactor {} from message {}", userReacting.getUserReference().getId(), message.getMessageId());
|
||||
starboardPostReactorManagementService.addReactor(starboardPost, userReacting);
|
||||
|
||||
@@ -84,7 +84,7 @@
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.dv8tion</groupId>
|
||||
<groupId>io.github.freya022</groupId>
|
||||
<artifactId>JDA</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
@@ -131,6 +131,11 @@
|
||||
<artifactId>gson</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.danilopianini</groupId>
|
||||
<artifactId>gson-extras</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
|
||||
@@ -3,9 +3,25 @@ package dev.sheldan.abstracto.core.config;
|
||||
import ch.qos.logback.core.net.ssl.SecureRandomFactoryBean;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory;
|
||||
import dev.sheldan.abstracto.core.logging.OkHttpLogger;
|
||||
import dev.sheldan.abstracto.core.metric.OkHttpMetrics;
|
||||
import dev.sheldan.abstracto.core.service.BotService;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.ActionRowButtonConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.ActionRowItemConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.ComponentConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.SectionAccessoryConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.SectionButton;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.SectionComponentConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.SectionTextDisplay;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.SectionThumbnail;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelActionRowConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelContainerConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelFileConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelMediaGalleryConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelSectionConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelSeperatorConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelTextDisplay;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -41,6 +57,27 @@ public class CoreConfig {
|
||||
GsonBuilder builder = new GsonBuilder()
|
||||
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeAdapter())
|
||||
.registerTypeAdapter(Instant.class, new InstantAdapter())
|
||||
.registerTypeAdapterFactory(RuntimeTypeAdapterFactory
|
||||
.of(ComponentConfig.class, "type")
|
||||
.registerSubtype(TopLevelActionRowConfig.class, "actionRow")
|
||||
.registerSubtype(TopLevelSectionConfig.class, "section")
|
||||
.registerSubtype(TopLevelFileConfig.class, "fileDisplay")
|
||||
.registerSubtype(TopLevelMediaGalleryConfig.class, "mediaGallery")
|
||||
.registerSubtype(TopLevelSeperatorConfig.class, "separator")
|
||||
.registerSubtype(TopLevelContainerConfig.class, "container")
|
||||
.registerSubtype(TopLevelTextDisplay.class, "textDisplay"))
|
||||
.registerTypeAdapterFactory(RuntimeTypeAdapterFactory
|
||||
.of(ActionRowItemConfig.class, "type")
|
||||
.registerSubtype(ActionRowButtonConfig.class, "button"))
|
||||
.registerTypeAdapterFactory(RuntimeTypeAdapterFactory
|
||||
.of(SectionAccessoryConfig.class, "type")
|
||||
.registerSubtype(SectionButton.class, "button")
|
||||
.registerSubtype(SectionThumbnail.class, "thumbnail")
|
||||
)
|
||||
.registerTypeAdapterFactory(RuntimeTypeAdapterFactory
|
||||
.of(SectionComponentConfig.class, "type")
|
||||
.registerSubtype(SectionTextDisplay.class, "textDisplay")
|
||||
)
|
||||
.setPrettyPrinting();
|
||||
if(customJsonDeSerializers != null) {
|
||||
customJsonDeSerializers.forEach(customJsonSerializer ->
|
||||
|
||||
@@ -6,10 +6,10 @@ import dev.sheldan.abstracto.core.service.MessageService;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
|
||||
import net.dv8tion.jda.api.entities.emoji.Emoji;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionComponent;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
||||
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
|
||||
import net.dv8tion.jda.api.components.ActionComponent;
|
||||
import net.dv8tion.jda.api.components.actionrow.ActionRow;
|
||||
import net.dv8tion.jda.api.components.buttons.Button;
|
||||
import net.dv8tion.jda.api.components.buttons.ButtonStyle;
|
||||
import org.apache.commons.collections4.ListUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -50,7 +50,7 @@ public class ComponentServiceBean implements ComponentService {
|
||||
} else {
|
||||
ActionRow lastRow = message.getActionRows().get(message.getActionRows().size() - 1);
|
||||
if(lastRow.getComponents().size() < MAX_BUTTONS_PER_ROW) {
|
||||
lastRow.getComponents().add(button);
|
||||
lastRow.getButtons().add(button);
|
||||
actionRows = message.getActionRows();
|
||||
} else {
|
||||
List<ActionRow> currentActionRows = new ArrayList<>(message.getActionRows());
|
||||
@@ -86,7 +86,7 @@ public class ComponentServiceBean implements ComponentService {
|
||||
public CompletableFuture<Void> removeComponentWithId(Message message, String componentId, Boolean rearrange) {
|
||||
List<ActionRow> actionRows = new ArrayList<>();
|
||||
if(Boolean.TRUE.equals(rearrange)) {
|
||||
List<net.dv8tion.jda.api.interactions.components.ActionComponent> components = new ArrayList<>();
|
||||
List<net.dv8tion.jda.api.components.ActionComponent> components = new ArrayList<>();
|
||||
message.getActionRows().forEach(row ->
|
||||
row
|
||||
.getComponents()
|
||||
@@ -112,8 +112,8 @@ public class ComponentServiceBean implements ComponentService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ActionRow> splitIntoActionRowsMax(List<net.dv8tion.jda.api.interactions.components.ActionComponent> allComponents) {
|
||||
List<List<net.dv8tion.jda.api.interactions.components.ActionComponent>> actionRows = ListUtils.partition(allComponents, MAX_BUTTONS_PER_ROW);
|
||||
public List<ActionRow> splitIntoActionRowsMax(List<net.dv8tion.jda.api.components.ActionComponent> allComponents) {
|
||||
List<List<net.dv8tion.jda.api.components.ActionComponent>> actionRows = ListUtils.partition(allComponents, MAX_BUTTONS_PER_ROW);
|
||||
return actionRows.stream().map(ActionRow::of).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.interactions.Interaction;
|
||||
import net.dv8tion.jda.api.interactions.InteractionHook;
|
||||
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionComponent;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||
import net.dv8tion.jda.api.components.ActionComponent;
|
||||
import net.dv8tion.jda.api.components.actionrow.ActionRow;
|
||||
import net.dv8tion.jda.api.requests.restaction.WebhookMessageCreateAction;
|
||||
import net.dv8tion.jda.api.requests.restaction.WebhookMessageEditAction;
|
||||
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
|
||||
@@ -260,56 +260,57 @@ public class InteractionServiceBean implements InteractionService {
|
||||
|
||||
public CompletableFuture<InteractionHook> replyMessageToSend(MessageToSend messageToSend, IReplyCallback callback) {
|
||||
ReplyCallbackAction action = null;
|
||||
if(messageToSend.getMessages() != null && !messageToSend.getMessages().isEmpty()) {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
action = callback.reply(messageToSend.getMessages().get(0));
|
||||
}
|
||||
|
||||
if(messageToSend.getEmbeds() != null && !messageToSend.getEmbeds().isEmpty()) {
|
||||
if(action != null) {
|
||||
action = action.addEmbeds(messageToSend.getEmbeds().subList(0, Math.min(10, messageToSend.getEmbeds().size())));
|
||||
} else {
|
||||
action = callback.replyEmbeds(messageToSend.getEmbeds());
|
||||
if(messageToSend.getUseComponentsV2()) {
|
||||
action = callback.replyComponents(messageToSend.getComponents()).useComponentsV2();
|
||||
} else {
|
||||
if(messageToSend.getMessages() != null && !messageToSend.getMessages().isEmpty()) {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
action = callback.reply(messageToSend.getMessages().get(0));
|
||||
}
|
||||
}
|
||||
|
||||
if(messageToSend.hasFilesToSend()) {
|
||||
List<FileUpload> attachedFiles = messageToSend
|
||||
if(messageToSend.getEmbeds() != null && !messageToSend.getEmbeds().isEmpty()) {
|
||||
if(action != null) {
|
||||
action = action.addEmbeds(messageToSend.getEmbeds().subList(0, Math.min(10, messageToSend.getEmbeds().size())));
|
||||
} else {
|
||||
action = callback.replyEmbeds(messageToSend.getEmbeds());
|
||||
}
|
||||
}
|
||||
if(messageToSend.hasFilesToSend()) {
|
||||
List<FileUpload> attachedFiles = messageToSend
|
||||
.getAttachedFiles()
|
||||
.stream()
|
||||
.map(AttachedFile::convertToFileUpload)
|
||||
.collect(Collectors.toList());
|
||||
if(action != null) {
|
||||
action.setFiles(attachedFiles);
|
||||
} else {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
action = callback.replyFiles(attachedFiles);
|
||||
}
|
||||
}
|
||||
|
||||
// this should be last, because we are "faking" a message, by inserting a ., in case there has not been a reply yet
|
||||
// we could also throw an exception, but we are allowing this to go through
|
||||
List<ActionRow> actionRows = messageToSend.getActionRows();
|
||||
if(actionRows != null && !actionRows.isEmpty()) {
|
||||
if(action == null) {
|
||||
action = callback.reply(".");
|
||||
}
|
||||
action = action.setComponents(actionRows);
|
||||
AServer server;
|
||||
if(ContextUtils.isGuildKnown(callback)) {
|
||||
server = serverManagementService.loadServer(callback.getGuild().getIdLong());
|
||||
} else {
|
||||
server = null;
|
||||
}
|
||||
actionRows.forEach(components -> components.forEach(component -> {
|
||||
if(component instanceof ActionComponent) {
|
||||
String id = ((ActionComponent)component).getId();
|
||||
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
|
||||
if(payload != null && payload.getPersistCallback()) {
|
||||
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());
|
||||
}
|
||||
if(action != null) {
|
||||
action.setFiles(attachedFiles);
|
||||
} else {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
action = callback.replyFiles(attachedFiles);
|
||||
}
|
||||
}));
|
||||
}
|
||||
// this should be last, because we are "faking" a message, by inserting a ., in case there has not been a reply yet
|
||||
// we could also throw an exception, but we are allowing this to go through
|
||||
List<ActionRow> actionRows = messageToSend.getActionRows();
|
||||
if(actionRows != null && !actionRows.isEmpty()) {
|
||||
if(action == null) {
|
||||
action = callback.reply(".");
|
||||
}
|
||||
action = action.setComponents(actionRows);
|
||||
AServer server;
|
||||
if(ContextUtils.isGuildKnown(callback)) {
|
||||
server = serverManagementService.loadServer(callback.getGuild().getIdLong());
|
||||
} else {
|
||||
server = null;
|
||||
}
|
||||
actionRows.forEach(components -> components.forEach(component -> {
|
||||
if(component instanceof ActionComponent) {
|
||||
String id = ((ActionComponent)component).getId();
|
||||
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
|
||||
if(payload != null && payload.getPersistCallback()) {
|
||||
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
if(messageToSend.getEphemeral()) {
|
||||
@@ -328,7 +329,7 @@ public class InteractionServiceBean implements InteractionService {
|
||||
if(ContextUtils.isGuildKnown(callback)) {
|
||||
Set<Message.MentionType> allowedMentions = allowedMentionService.getAllowedMentionsFor(callback.getMessageChannel(), messageToSend);
|
||||
if (action != null) {
|
||||
action.setAllowedMentions(allowedMentions);
|
||||
action = action.setAllowedMentions(allowedMentions);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@ import dev.sheldan.abstracto.core.interaction.modal.config.TextInputComponentSty
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||
import net.dv8tion.jda.api.components.actionrow.ActionRow;
|
||||
import net.dv8tion.jda.api.interactions.components.ItemComponent;
|
||||
import net.dv8tion.jda.api.interactions.components.text.TextInput;
|
||||
import net.dv8tion.jda.api.components.textinput.TextInput;
|
||||
import net.dv8tion.jda.api.interactions.modals.Modal;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package dev.sheldan.abstracto.core.interaction.modal.config;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
|
||||
import net.dv8tion.jda.api.components.textinput.TextInputStyle;
|
||||
|
||||
public enum TextInputComponentStyle {
|
||||
@SerializedName("short")
|
||||
|
||||
@@ -125,14 +125,26 @@ public class AllowedMentionServiceBean implements AllowedMentionService {
|
||||
}
|
||||
if(messageToSend != null && messageToSend.getMessageConfig() != null) {
|
||||
MessageConfig messageConfig = messageToSend.getMessageConfig();
|
||||
if(messageConfig.isAllowsEveryoneMention()) {
|
||||
allowedMentions.add(Message.MentionType.EVERYONE);
|
||||
if(messageConfig.getAllowsEveryoneMention() != null) {
|
||||
if(messageConfig.getAllowsEveryoneMention()) {
|
||||
allowedMentions.add(Message.MentionType.EVERYONE);
|
||||
} else {
|
||||
allowedMentions.remove(Message.MentionType.EVERYONE);
|
||||
}
|
||||
}
|
||||
if(messageConfig.isAllowsUserMention()) {
|
||||
allowedMentions.add(Message.MentionType.USER);
|
||||
if(messageConfig.getAllowsUserMention() != null) {
|
||||
if(messageConfig.getAllowsUserMention()) {
|
||||
allowedMentions.add(Message.MentionType.USER);
|
||||
} else {
|
||||
allowedMentions.remove(Message.MentionType.USER);
|
||||
}
|
||||
}
|
||||
if(messageConfig.isAllowsRoleMention()) {
|
||||
allowedMentions.add(Message.MentionType.ROLE);
|
||||
if(messageConfig.getAllowsRoleMention() != null) {
|
||||
if(messageConfig.getAllowsRoleMention()) {
|
||||
allowedMentions.add(Message.MentionType.ROLE);
|
||||
} else {
|
||||
allowedMentions.remove(Message.MentionType.ROLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
return allowedMentions;
|
||||
|
||||
@@ -74,6 +74,7 @@ public class CacheEntityServiceBean implements CacheEntityService {
|
||||
.fileName(attachment.getFileName())
|
||||
.height(attachment.getHeight())
|
||||
.spoiler(attachment.isSpoiler())
|
||||
.contentType(attachment.getContentType())
|
||||
.proxyUrl(attachment.getProxyUrl())
|
||||
.size(attachment.getSize())
|
||||
.url(attachment.getUrl())
|
||||
|
||||
@@ -17,6 +17,7 @@ import dev.sheldan.abstracto.core.utils.FileService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.components.actionrow.ActionRowChildComponent;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import net.dv8tion.jda.api.entities.channel.attribute.IThreadContainer;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.Category;
|
||||
@@ -25,9 +26,8 @@ import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionComponent;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||
import net.dv8tion.jda.api.interactions.components.ItemComponent;
|
||||
import net.dv8tion.jda.api.components.ActionComponent;
|
||||
import net.dv8tion.jda.api.components.actionrow.ActionRow;
|
||||
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction;
|
||||
import net.dv8tion.jda.api.requests.restaction.MessageEditAction;
|
||||
import net.dv8tion.jda.api.utils.FileUpload;
|
||||
@@ -221,91 +221,96 @@ public class ChannelServiceBean implements ChannelService {
|
||||
}
|
||||
List<CompletableFuture<Message>> futures = new ArrayList<>();
|
||||
List<MessageCreateAction> allMessageActions = new ArrayList<>();
|
||||
Iterator<MessageEmbed> embedIterator = messageToSend.getEmbeds().iterator();
|
||||
for (int i = 0; i < messageToSend.getMessages().size(); i++) {
|
||||
if(messageToSend.getUseComponentsV2() && messageToSend.getComponents() != null && !messageToSend.getComponents().isEmpty()) {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
String text = messageToSend.getMessages().get(i);
|
||||
allMessageActions.add(textChannel.sendMessageComponents(messageToSend.getComponents()).useComponentsV2());
|
||||
} else {
|
||||
Iterator<MessageEmbed> embedIterator = messageToSend.getEmbeds().iterator();
|
||||
for (int i = 0; i < messageToSend.getMessages().size(); i++) {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
String text = messageToSend.getMessages().get(i);
|
||||
List<MessageEmbed> messageEmbeds = new ArrayList<>();
|
||||
while(embedIterator.hasNext()) {
|
||||
MessageEmbed embedToAdd = embedIterator.next();
|
||||
if((currentEmbedLength(messageEmbeds) + embedToAdd.getLength()) >= MessageEmbed.EMBED_MAX_LENGTH_BOT) {
|
||||
break;
|
||||
}
|
||||
messageEmbeds.add(embedToAdd);
|
||||
embedIterator.remove();
|
||||
}
|
||||
MessageCreateAction messageAction = textChannel.sendMessage(text);
|
||||
if(!messageEmbeds.isEmpty()) {
|
||||
messageAction.setEmbeds(messageEmbeds);
|
||||
}
|
||||
allMessageActions.add(messageAction);
|
||||
}
|
||||
List<MessageEmbed> messageEmbeds = new ArrayList<>();
|
||||
// reset the iterator, because if the if in the above while iterator loop applied, we already took it out from the iterator
|
||||
// but we didnt add it yet, so it would be lost
|
||||
embedIterator = messageToSend.getEmbeds().iterator();
|
||||
while(embedIterator.hasNext()) {
|
||||
MessageEmbed embedToAdd = embedIterator.next();
|
||||
if((currentEmbedLength(messageEmbeds) + embedToAdd.getLength()) >= MessageEmbed.EMBED_MAX_LENGTH_BOT) {
|
||||
break;
|
||||
if((currentEmbedLength(messageEmbeds) + embedToAdd.getLength()) >= MessageEmbed.EMBED_MAX_LENGTH_BOT && !messageEmbeds.isEmpty()) {
|
||||
allMessageActions.add(textChannel.sendMessageEmbeds(messageEmbeds));
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
messageEmbeds = new ArrayList<>();
|
||||
}
|
||||
messageEmbeds.add(embedToAdd);
|
||||
embedIterator.remove();
|
||||
}
|
||||
MessageCreateAction messageAction = textChannel.sendMessage(text);
|
||||
|
||||
if(!messageEmbeds.isEmpty()) {
|
||||
messageAction.setEmbeds(messageEmbeds);
|
||||
}
|
||||
allMessageActions.add(messageAction);
|
||||
}
|
||||
List<MessageEmbed> messageEmbeds = new ArrayList<>();
|
||||
// reset the iterator, because if the if in the above while iterator loop applied, we already took it out from the iterator
|
||||
// but we didnt add it yet, so it would be lost
|
||||
embedIterator = messageToSend.getEmbeds().iterator();
|
||||
while(embedIterator.hasNext()) {
|
||||
MessageEmbed embedToAdd = embedIterator.next();
|
||||
if((currentEmbedLength(messageEmbeds) + embedToAdd.getLength()) >= MessageEmbed.EMBED_MAX_LENGTH_BOT && !messageEmbeds.isEmpty()) {
|
||||
allMessageActions.add(textChannel.sendMessageEmbeds(messageEmbeds));
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
messageEmbeds = new ArrayList<>();
|
||||
}
|
||||
messageEmbeds.add(embedToAdd);
|
||||
}
|
||||
|
||||
if(!messageEmbeds.isEmpty()) {
|
||||
allMessageActions.add(textChannel.sendMessageEmbeds(messageEmbeds));
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
}
|
||||
|
||||
List<ActionRow> actionRows = messageToSend.getActionRows();
|
||||
if(!actionRows.isEmpty()) {
|
||||
List<List<ActionRow>> groupedActionRows = ListUtils.partition(actionRows, ComponentService.MAX_BUTTONS_PER_ROW);
|
||||
for (int i = 0; i < allMessageActions.size(); i++) {
|
||||
allMessageActions.set(i, allMessageActions.get(i).setComponents(groupedActionRows.get(i)));
|
||||
}
|
||||
for (int i = allMessageActions.size(); i < groupedActionRows.size(); i++) {
|
||||
// TODO maybe possible nicer
|
||||
allMessageActions.add(textChannel.sendMessage(".").setComponents(groupedActionRows.get(i)));
|
||||
}
|
||||
AServer server = null;
|
||||
if(textChannel instanceof GuildChannel) {
|
||||
GuildChannel channel = (GuildChannel) textChannel;
|
||||
server = serverManagementService.loadServer(channel.getGuild());
|
||||
}
|
||||
for (ActionRow components : actionRows) {
|
||||
for (ItemComponent component : components) {
|
||||
if (component instanceof ActionComponent) {
|
||||
String id = ((ActionComponent) component).getId();
|
||||
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
|
||||
if (payload != null && payload.getPersistCallback()) {
|
||||
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());
|
||||
List<ActionRow> actionRows = messageToSend.getActionRows();
|
||||
if(!actionRows.isEmpty()) {
|
||||
List<List<ActionRow>> groupedActionRows = ListUtils.partition(actionRows, ComponentService.MAX_BUTTONS_PER_ROW);
|
||||
for (int i = 0; i < allMessageActions.size(); i++) {
|
||||
allMessageActions.set(i, allMessageActions.get(i).setComponents(groupedActionRows.get(i)));
|
||||
}
|
||||
for (int i = allMessageActions.size(); i < groupedActionRows.size(); i++) {
|
||||
// TODO maybe possible nicer
|
||||
allMessageActions.add(textChannel.sendMessage(".").setComponents(groupedActionRows.get(i)));
|
||||
}
|
||||
AServer server = null;
|
||||
if(textChannel instanceof GuildChannel) {
|
||||
GuildChannel channel = (GuildChannel) textChannel;
|
||||
server = serverManagementService.loadServer(channel.getGuild());
|
||||
}
|
||||
for (ActionRow row : actionRows) {
|
||||
for (ActionRowChildComponent component : row) {
|
||||
if (component instanceof ActionComponent) {
|
||||
String id = ((ActionComponent) component).getId();
|
||||
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
|
||||
if (payload != null && payload.getPersistCallback()) {
|
||||
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(messageToSend.hasFilesToSend()) {
|
||||
List<FileUpload> attachedFiles = messageToSend
|
||||
if(messageToSend.hasFilesToSend()) {
|
||||
List<FileUpload> attachedFiles = messageToSend
|
||||
.getAttachedFiles()
|
||||
.stream()
|
||||
.map(AttachedFile::convertToFileUpload)
|
||||
.collect(Collectors.toList());
|
||||
if(!allMessageActions.isEmpty()) {
|
||||
// in case there has not been a message, we need to increment it
|
||||
allMessageActions.set(0, allMessageActions.get(0).addFiles(attachedFiles));
|
||||
} else {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
allMessageActions.add(textChannel.sendFiles(attachedFiles));
|
||||
if(!allMessageActions.isEmpty()) {
|
||||
// in case there has not been a message, we need to increment it
|
||||
allMessageActions.set(0, allMessageActions.get(0).addFiles(attachedFiles));
|
||||
} else {
|
||||
metricService.incrementCounter(MESSAGE_SEND_METRIC);
|
||||
allMessageActions.add(textChannel.sendFiles(attachedFiles));
|
||||
}
|
||||
}
|
||||
}
|
||||
Set<Message.MentionType> allowedMentions = allowedMentionService.getAllowedMentionsFor(textChannel, messageToSend);
|
||||
allMessageActions.forEach(messageAction -> {
|
||||
if(messageToSend.getReferencedMessageId() != null) {
|
||||
messageAction = messageAction.setMessageReference(messageToSend.getReferencedMessageId());
|
||||
if(messageToSend.getMessageConfig() != null && !messageToSend.getMessageConfig().isMentionsReferencedMessage()) {
|
||||
if(messageToSend.getMessageConfig() != null && !messageToSend.getMessageConfig().getMentionsReferencedMessage()) {
|
||||
messageAction = messageAction.mentionRepliedUser(false);
|
||||
}
|
||||
}
|
||||
@@ -337,6 +342,15 @@ public class ChannelServiceBean implements ChannelService {
|
||||
@Override
|
||||
public CompletableFuture<Message> editMessageInAChannelFuture(MessageToSend messageToSend, MessageChannel channel, Long messageId) {
|
||||
MessageEditAction messageAction;
|
||||
if(messageToSend.getUseComponentsV2()) {
|
||||
Set<Message.MentionType> allowedMentions = allowedMentionService.getAllowedMentionsFor(channel, messageToSend);
|
||||
return channel
|
||||
.editMessageComponentsById(messageId, messageToSend.getComponents())
|
||||
.useComponentsV2()
|
||||
.setReplace(true)
|
||||
.setAllowedMentions(allowedMentions)
|
||||
.submit();
|
||||
}
|
||||
if(!messageToSend.getMessages().isEmpty() && !StringUtils.isBlank(messageToSend.getMessages().get(0))) {
|
||||
log.debug("Editing message {} with new text content.", messageId);
|
||||
messageAction = channel.editMessageById(messageId, messageToSend.getMessages().get(0));
|
||||
|
||||
@@ -16,7 +16,7 @@ import net.dv8tion.jda.api.entities.*;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||
import net.dv8tion.jda.api.components.actionrow.ActionRow;
|
||||
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
|
||||
import net.dv8tion.jda.api.requests.restaction.MessageEditAction;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
@@ -2,8 +2,6 @@ package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.config.FeatureConfig;
|
||||
import dev.sheldan.abstracto.core.config.PostTargetEnum;
|
||||
import dev.sheldan.abstracto.core.exception.ChannelNotInGuildException;
|
||||
import dev.sheldan.abstracto.core.exception.GuildNotFoundException;
|
||||
import dev.sheldan.abstracto.core.exception.PostTargetNotUsableException;
|
||||
import dev.sheldan.abstracto.core.exception.PostTargetNotValidException;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
@@ -11,6 +9,7 @@ import dev.sheldan.abstracto.core.models.database.PostTarget;
|
||||
import dev.sheldan.abstracto.core.service.management.DefaultPostTargetManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.PostTargetManagement;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import dev.sheldan.abstracto.core.utils.FutureUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
|
||||
@@ -181,40 +180,65 @@ public class PostTargetServiceBean implements PostTargetService {
|
||||
return getMessageChannelForPostTarget(target).map(messageChannel -> {
|
||||
CompletableFuture<Message> messageEditFuture = new CompletableFuture<>();
|
||||
futures.add(messageEditFuture);
|
||||
if (StringUtils.isBlank(messageToSend.getMessages().get(0).trim())) {
|
||||
if(messageToSend.getUseComponentsV2()) {
|
||||
channelService.retrieveMessageInChannel(messageChannel, messageId).thenAccept(message -> {
|
||||
log.debug("Editing existing message {} when upserting message embeds in channel {} in server {}.",
|
||||
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||
messageService.editMessage(message, messageToSend.getEmbeds().get(0))
|
||||
.queue(messageEditFuture::complete, messageEditFuture::completeExceptionally);
|
||||
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||
channelService.editMessageInAChannelFuture(messageToSend, messageChannel, messageId)
|
||||
.thenAccept(messageEditFuture::complete)
|
||||
.exceptionally(throwable -> {
|
||||
messageEditFuture.completeExceptionally(throwable);
|
||||
return null;
|
||||
});
|
||||
}).exceptionally(throwable -> {
|
||||
log.debug("Creating new message when upserting message embeds for message {} in channel {} in server {}.",
|
||||
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||
sendEmbedInPostTarget(messageToSend, target).get(0)
|
||||
.thenAccept(messageEditFuture::complete).exceptionally(innerThrowable -> {
|
||||
log.error("Failed to send message to create a message.", innerThrowable);
|
||||
messageEditFuture.completeExceptionally(innerThrowable);
|
||||
return null;
|
||||
});
|
||||
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||
List<CompletableFuture<Message>> messageFutures = channelService.sendMessageToSendToChannel(messageToSend, messageChannel);
|
||||
FutureUtils.toSingleFutureGeneric(messageFutures)
|
||||
.thenAccept(unused -> messageEditFuture.complete(futures.get(0).join()))
|
||||
.exceptionally(innerThrowable -> {
|
||||
log.error("Failed to send message to create a message.", innerThrowable);
|
||||
messageEditFuture.completeExceptionally(innerThrowable);
|
||||
return null;
|
||||
});
|
||||
return null;
|
||||
});
|
||||
} else {
|
||||
channelService.retrieveMessageInChannel(messageChannel, messageId).thenAccept(message -> {
|
||||
log.debug("Editing existing message {} when upserting message in channel {} in server {}.",
|
||||
if (StringUtils.isBlank(messageToSend.getMessages().get(0).trim())) {
|
||||
channelService.retrieveMessageInChannel(messageChannel, messageId).thenAccept(message -> {
|
||||
log.debug("Editing existing message {} when upserting message embeds in channel {} in server {}.",
|
||||
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||
messageService.editMessage(message, messageToSend.getMessages().get(0), messageToSend.getEmbeds().get(0))
|
||||
messageService.editMessage(message, messageToSend.getEmbeds().get(0))
|
||||
.queue(messageEditFuture::complete, messageEditFuture::completeExceptionally);
|
||||
}).exceptionally(throwable -> {
|
||||
log.debug("Creating new message when trying to upsert a message {} in channel {} in server {}.",
|
||||
}).exceptionally(throwable -> {
|
||||
log.debug("Creating new message when upserting message embeds for message {} in channel {} in server {}.",
|
||||
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||
sendEmbedInPostTarget(messageToSend, target).get(0)
|
||||
sendEmbedInPostTarget(messageToSend, target).get(0)
|
||||
.thenAccept(messageEditFuture::complete).exceptionally(innerThrowable -> {
|
||||
log.error("Failed to send message to create a message.", innerThrowable);
|
||||
messageEditFuture.completeExceptionally(innerThrowable);
|
||||
return null;
|
||||
});
|
||||
return null;
|
||||
});
|
||||
return null;
|
||||
});
|
||||
} else {
|
||||
channelService.retrieveMessageInChannel(messageChannel, messageId).thenAccept(message -> {
|
||||
log.debug("Editing existing message {} when upserting message in channel {} in server {}.",
|
||||
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||
messageService.editMessage(message, messageToSend.getMessages().get(0), messageToSend.getEmbeds().get(0))
|
||||
.queue(messageEditFuture::complete, messageEditFuture::completeExceptionally);
|
||||
}).exceptionally(throwable -> {
|
||||
log.debug("Creating new message when trying to upsert a message {} in channel {} in server {}.",
|
||||
messageId, messageChannel.getIdLong(), messageChannel.getGuild().getId());
|
||||
sendEmbedInPostTarget(messageToSend, target).get(0)
|
||||
.thenAccept(messageEditFuture::complete).exceptionally(innerThrowable -> {
|
||||
log.error("Failed to send message to create a message.", innerThrowable);
|
||||
messageEditFuture.completeExceptionally(innerThrowable);
|
||||
return null;
|
||||
});
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
return futures;
|
||||
}).orElse(Arrays.asList(CompletableFuture.completedFuture(null)));
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package dev.sheldan.abstracto.core.templating.model;
|
||||
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.ButtonConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.ComponentConfig;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -15,6 +17,7 @@ import java.util.List;
|
||||
@Builder
|
||||
public class MessageConfiguration {
|
||||
private List<EmbedConfiguration> embeds;
|
||||
private List<ComponentConfig> components;
|
||||
private Long referencedMessageId;
|
||||
/**
|
||||
* The message which is posted along the {@link net.dv8tion.jda.api.entities.MessageEmbed} as a normal message.
|
||||
|
||||
@@ -17,6 +17,8 @@ public class MetaMessageConfiguration {
|
||||
@Builder.Default
|
||||
private boolean allowsUserMention = true;
|
||||
@Builder.Default
|
||||
private boolean useComponentsV2 = false;
|
||||
@Builder.Default
|
||||
private boolean mentionsReferencedMessage = true;
|
||||
|
||||
private Integer messageLimit;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@Getter
|
||||
@SuperBuilder
|
||||
public class ActionRowButtonConfig extends ButtonConfig implements ActionRowItemConfig{
|
||||
|
||||
@Override
|
||||
public ActionRowItemType getType() {
|
||||
return ActionRowItemType.BUTTON;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class ActionRowConfig {
|
||||
protected List<ActionRowItemConfig> actionRowItems;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public interface ActionRowItemConfig {
|
||||
ActionRowItemType getType();
|
||||
|
||||
enum ActionRowItemType {
|
||||
@SerializedName("button")
|
||||
BUTTON,
|
||||
@SerializedName("stringSelectMenu")
|
||||
STRING_SELECT_MENU,
|
||||
@SerializedName("entitySelectMenu")
|
||||
ENTITY_SELECT_MENU
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package dev.sheldan.abstracto.core.templating.model;
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@SuperBuilder
|
||||
public class ButtonConfig {
|
||||
private String label;
|
||||
private String id;
|
||||
@@ -1,4 +1,4 @@
|
||||
package dev.sheldan.abstracto.core.templating.model;
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
@@ -1,7 +1,7 @@
|
||||
package dev.sheldan.abstracto.core.templating.model;
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
|
||||
import net.dv8tion.jda.api.components.buttons.ButtonStyle;
|
||||
|
||||
public enum ButtonStyleConfig {
|
||||
@SerializedName("primary")
|
||||
@@ -0,0 +1,27 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public interface ComponentConfig {
|
||||
ComponentTypeConfig getType();
|
||||
|
||||
enum ComponentTypeConfig {
|
||||
@SerializedName("actionRow")
|
||||
ACTION_ROW,
|
||||
@SerializedName("section")
|
||||
SECTION,
|
||||
@SerializedName("textDisplay")
|
||||
TEXT_DISPLAY,
|
||||
@SerializedName("mediaGallery")
|
||||
MEDIA_GALLERY,
|
||||
@SerializedName("separator")
|
||||
SEPARATOR,
|
||||
@SerializedName("fileDisplay")
|
||||
FILE_DISPLAY,
|
||||
@SerializedName("container")
|
||||
CONTAINER
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class ContainerConfig implements ComponentConfig {
|
||||
private List<ComponentConfig> components;
|
||||
@Override
|
||||
public ComponentTypeConfig getType() {
|
||||
return ComponentTypeConfig.CONTAINER;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import java.io.File;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import net.dv8tion.jda.api.utils.FileUpload;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class FileConfig {
|
||||
private String fileName;
|
||||
// only used for plaintext files
|
||||
private String fileContent;
|
||||
@Builder.Default
|
||||
private Boolean spoiler = false;
|
||||
|
||||
public FileUpload convertToFileUpload(File file) {
|
||||
FileUpload fileUpload = FileUpload.fromData(file, fileName);
|
||||
if(spoiler != null && spoiler) {
|
||||
fileUpload = fileUpload.asSpoiler();
|
||||
}
|
||||
return fileUpload;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class ImageConfig {
|
||||
private String url;
|
||||
private String description;
|
||||
@Builder.Default
|
||||
private Boolean spoiler = false;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class MediaGalleryConfig implements ComponentConfig {
|
||||
private List<ImageConfig> images;
|
||||
@Override
|
||||
public ComponentTypeConfig getType() {
|
||||
return ComponentTypeConfig.MEDIA_GALLERY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public interface SectionAccessoryConfig {
|
||||
SectionAccessoryType getType();
|
||||
|
||||
enum SectionAccessoryType {
|
||||
@SerializedName("button")
|
||||
BUTTON,
|
||||
@SerializedName("thumbnail")
|
||||
THUMBNAIL
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Builder
|
||||
@Getter
|
||||
public class SectionButton extends ButtonConfig implements SectionAccessoryConfig {
|
||||
@Override
|
||||
public SectionAccessoryType getType() {
|
||||
return SectionAccessoryType.BUTTON;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public interface SectionComponentConfig {
|
||||
SectionComponentType getType();
|
||||
|
||||
enum SectionComponentType {
|
||||
@SerializedName("textDisplay")
|
||||
TEXT_DISPLAY
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class SectionConfig {
|
||||
protected SectionAccessoryConfig accessory;
|
||||
protected List<SectionComponentConfig> components;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class SectionTextDisplay extends TextDisplayConfig implements SectionComponentConfig {
|
||||
@Override
|
||||
public SectionComponentType getType() {
|
||||
return SectionComponentType.TEXT_DISPLAY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
public class SectionThumbnail implements SectionAccessoryConfig {
|
||||
|
||||
private String url;
|
||||
@Builder.Default
|
||||
private Boolean spoiler = false;
|
||||
private String description;
|
||||
private Integer uniqueId;
|
||||
|
||||
@Override
|
||||
public SectionAccessoryType getType() {
|
||||
return SectionAccessoryType.THUMBNAIL;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class SeparatorConfig implements ComponentConfig {
|
||||
private Boolean divider;
|
||||
private Spacing spacing;
|
||||
@Override
|
||||
public ComponentTypeConfig getType() {
|
||||
return ComponentTypeConfig.SEPARATOR;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import net.dv8tion.jda.api.components.separator.Separator;
|
||||
|
||||
public enum Spacing {
|
||||
@SerializedName("small")
|
||||
SMALL,
|
||||
@SerializedName("large")
|
||||
LARGE;
|
||||
|
||||
public static Separator.Spacing toSpacing(Spacing spacing) {
|
||||
return switch (spacing) {
|
||||
case LARGE -> Separator.Spacing.LARGE;
|
||||
case SMALL -> Separator.Spacing.SMALL;
|
||||
default -> Separator.Spacing.UNKNOWN;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class TextDisplayConfig {
|
||||
private Integer uniqueId;
|
||||
private String content;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class TopLevelActionRowConfig extends ActionRowConfig implements ComponentConfig {
|
||||
@Override
|
||||
public ComponentTypeConfig getType() {
|
||||
return ComponentTypeConfig.ACTION_ROW;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class TopLevelContainerConfig extends ContainerConfig implements ComponentConfig {
|
||||
@Override
|
||||
public ComponentTypeConfig getType() {
|
||||
return ComponentTypeConfig.SECTION;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class TopLevelFileConfig extends FileConfig implements ComponentConfig {
|
||||
@Override
|
||||
public ComponentTypeConfig getType() {
|
||||
return ComponentTypeConfig.FILE_DISPLAY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class TopLevelMediaGalleryConfig extends MediaGalleryConfig implements ComponentConfig {
|
||||
@Override
|
||||
public ComponentTypeConfig getType() {
|
||||
return ComponentTypeConfig.MEDIA_GALLERY;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class TopLevelSectionConfig extends SectionConfig implements ComponentConfig {
|
||||
@Override
|
||||
public ComponentTypeConfig getType() {
|
||||
return ComponentTypeConfig.SECTION;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class TopLevelSeperatorConfig extends SeparatorConfig implements ComponentConfig {
|
||||
@Override
|
||||
public ComponentTypeConfig getType() {
|
||||
return ComponentTypeConfig.SEPARATOR;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package dev.sheldan.abstracto.core.templating.model.messagecomponents;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
@SuperBuilder
|
||||
@Getter
|
||||
public class TopLevelTextDisplay extends TextDisplayConfig implements ComponentConfig {
|
||||
@Override
|
||||
public ComponentTypeConfig getType() {
|
||||
return ComponentTypeConfig.TEXT_DISPLAY;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,24 @@ import dev.sheldan.abstracto.core.service.ConfigService;
|
||||
import dev.sheldan.abstracto.core.templating.Templatable;
|
||||
import dev.sheldan.abstracto.core.templating.exception.TemplatingException;
|
||||
import dev.sheldan.abstracto.core.templating.model.*;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.ButtonConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.ActionRowItemConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.ButtonMetaConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.ButtonStyleConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.ComponentConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.SectionAccessoryConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.SectionButton;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.SectionComponentConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.SectionThumbnail;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.Spacing;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TextDisplayConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelActionRowConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelContainerConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelFileConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelMediaGalleryConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelSectionConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelSeperatorConfig;
|
||||
import dev.sheldan.abstracto.core.templating.model.messagecomponents.TopLevelTextDisplay;
|
||||
import dev.sheldan.abstracto.core.utils.FileService;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
@@ -19,16 +37,28 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.components.MessageTopLevelComponent;
|
||||
import net.dv8tion.jda.api.components.actionrow.ActionRowChildComponent;
|
||||
import net.dv8tion.jda.api.components.container.Container;
|
||||
import net.dv8tion.jda.api.components.container.ContainerChildComponent;
|
||||
import net.dv8tion.jda.api.components.filedisplay.FileDisplay;
|
||||
import net.dv8tion.jda.api.components.mediagallery.MediaGallery;
|
||||
import net.dv8tion.jda.api.components.mediagallery.MediaGalleryItem;
|
||||
import net.dv8tion.jda.api.components.section.Section;
|
||||
import net.dv8tion.jda.api.components.section.SectionAccessoryComponent;
|
||||
import net.dv8tion.jda.api.components.section.SectionContentComponent;
|
||||
import net.dv8tion.jda.api.components.separator.Separator;
|
||||
import net.dv8tion.jda.api.components.textdisplay.TextDisplay;
|
||||
import net.dv8tion.jda.api.components.thumbnail.Thumbnail;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.entities.channel.ChannelType;
|
||||
import net.dv8tion.jda.api.entities.emoji.Emoji;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||
import net.dv8tion.jda.api.components.actionrow.ActionRow;
|
||||
import net.dv8tion.jda.api.interactions.components.ItemComponent;
|
||||
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
||||
import net.dv8tion.jda.api.interactions.components.selections.EntitySelectMenu;
|
||||
import net.dv8tion.jda.api.interactions.components.selections.SelectOption;
|
||||
import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu;
|
||||
import net.dv8tion.jda.api.components.buttons.Button;
|
||||
import net.dv8tion.jda.api.components.selections.EntitySelectMenu;
|
||||
import net.dv8tion.jda.api.components.selections.SelectOption;
|
||||
import net.dv8tion.jda.api.components.selections.StringSelectMenu;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -68,7 +98,7 @@ public class TemplateServiceBean implements TemplateService {
|
||||
private FileService fileService;
|
||||
|
||||
/**
|
||||
* Formats the passed passed count with the embed used for formatting pages.
|
||||
* Formats the passed count with the embed used for formatting pages.
|
||||
*
|
||||
* @param count The index of the page you want formatted.
|
||||
* @return The rendered template as a string object
|
||||
@@ -100,90 +130,103 @@ public class TemplateServiceBean implements TemplateService {
|
||||
}
|
||||
|
||||
public MessageToSend convertEmbedConfigurationToMessageToSend(MessageConfiguration messageConfiguration) {
|
||||
List<EmbedBuilder> embedBuilders = new ArrayList<>();
|
||||
if(messageConfiguration.getEmbeds() != null && !messageConfiguration.getEmbeds().isEmpty()) {
|
||||
convertEmbeds(messageConfiguration, embedBuilders);
|
||||
}
|
||||
|
||||
List<MessageEmbed> embeds = new ArrayList<>();
|
||||
List<ActionRow> actionRows = new ArrayList<>();
|
||||
Map<String, MessageToSend.ComponentConfig> componentPayloads = new HashMap<>();
|
||||
if(messageConfiguration.getButtons() != null || messageConfiguration.getSelectionMenus() != null) {
|
||||
// this basically preprocesses the buttons and select menus
|
||||
// by getting the positions of the items first
|
||||
// we only need this, because the current message config does not have them in the same item
|
||||
// they are two distinct lists, but map to the same concept in discord: components
|
||||
Set<Integer> positions = new HashSet<>();
|
||||
HashMap<Integer, ButtonConfig> buttonPositions = new HashMap<>();
|
||||
List<ButtonConfig> buttonsWithoutPosition = new ArrayList<>();
|
||||
HashMap<Integer, SelectionMenuConfig> selectionMenuPositions = new HashMap<>();
|
||||
List<SelectionMenuConfig> selectionMenusWithoutPosition = new ArrayList<>();
|
||||
// we do this by getting all positions which are part of the config
|
||||
// we also track which positions are buttons and which are select menus
|
||||
if(messageConfiguration.getButtons() != null) {
|
||||
messageConfiguration.getButtons().forEach(buttonConfig -> {
|
||||
if(buttonConfig.getPosition() != null) {
|
||||
positions.add(buttonConfig.getPosition());
|
||||
buttonPositions.put(buttonConfig.getPosition(), buttonConfig);
|
||||
} else {
|
||||
buttonsWithoutPosition.add(buttonConfig);
|
||||
List<MessageTopLevelComponent> discordComponents = new ArrayList<>();
|
||||
boolean useComponentsV2;
|
||||
if(messageConfiguration.getMessageConfig() != null && messageConfiguration.getMessageConfig().isUseComponentsV2()) {
|
||||
useComponentsV2 = true;
|
||||
if(messageConfiguration.getComponents() != null) {
|
||||
for (ComponentConfig componentConfig : messageConfiguration.getComponents()) {
|
||||
net.dv8tion.jda.api.components.Component createdComponent = getComponent(componentConfig, componentPayloads);
|
||||
if(createdComponent != null) {
|
||||
discordComponents.add((MessageTopLevelComponent) createdComponent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if(messageConfiguration.getSelectionMenus() != null) {
|
||||
messageConfiguration.getSelectionMenus().forEach(selectionMenuConfig -> {
|
||||
if(selectionMenuConfig.getPosition() != null) {
|
||||
positions.add(selectionMenuConfig.getPosition());
|
||||
selectionMenuPositions.put(selectionMenuConfig.getPosition(), selectionMenuConfig);
|
||||
} else {
|
||||
selectionMenusWithoutPosition.add(selectionMenuConfig);
|
||||
}
|
||||
});
|
||||
}
|
||||
List<Integer> positionsSorted = new ArrayList<>(positions);
|
||||
Collections.sort(positionsSorted);
|
||||
List<ButtonConfig> currentButtons = new ArrayList<>();
|
||||
// we go over all positions, and if its part of the buttons, we only add it to a list of buttons
|
||||
// this will then mean, that all buttons are processed as a group
|
||||
// this is necessary, because we can only add buttons as part of an action row
|
||||
// and in order to make it easier, we process the whole chunk of buttons at once, producing
|
||||
// at least one or more action rows
|
||||
for (Integer position : positionsSorted) {
|
||||
if (buttonPositions.containsKey(position)) {
|
||||
currentButtons.add(buttonPositions.get(position));
|
||||
} else {
|
||||
// if we get interrupted by a selection menu, we process the buttons we have so far
|
||||
// because those should be handled as a group
|
||||
// and then process the selection menu, the selection menu will always represent one full action row
|
||||
// it is not possible to have a button and a menu in the same row
|
||||
if(!currentButtons.isEmpty()) {
|
||||
addButtons(actionRows, componentPayloads, currentButtons);
|
||||
currentButtons.clear();
|
||||
}
|
||||
addSelectionMenu(actionRows, selectionMenuPositions.get(position));
|
||||
}
|
||||
}
|
||||
if(!currentButtons.isEmpty()) {
|
||||
addButtons(actionRows, componentPayloads, currentButtons);
|
||||
currentButtons.clear();
|
||||
} else {
|
||||
useComponentsV2 = false;
|
||||
List<EmbedBuilder> embedBuilders = new ArrayList<>();
|
||||
if(messageConfiguration.getEmbeds() != null && !messageConfiguration.getEmbeds().isEmpty()) {
|
||||
convertEmbeds(messageConfiguration, embedBuilders);
|
||||
}
|
||||
// all the rest without positions will be processed at the end (probably default case for most cases)
|
||||
addButtons(actionRows, componentPayloads, buttonsWithoutPosition);
|
||||
// selection menus are handled afterwards, that is just implied logic
|
||||
// to have a select menu before a button, one would need to set accordingly, or only
|
||||
// set the position for the selection menu, and not for the button
|
||||
selectionMenusWithoutPosition.forEach(selectionMenuConfig -> addSelectionMenu(actionRows, selectionMenuConfig));
|
||||
}
|
||||
|
||||
setPagingFooters(embedBuilders);
|
||||
if(messageConfiguration.getButtons() != null || messageConfiguration.getSelectionMenus() != null) {
|
||||
// this basically preprocesses the buttons and select menus
|
||||
// by getting the positions of the items first
|
||||
// we only need this, because the current message config does not have them in the same item
|
||||
// they are two distinct lists, but map to the same concept in discord: components
|
||||
Set<Integer> positions = new HashSet<>();
|
||||
HashMap<Integer, ButtonConfig> buttonPositions = new HashMap<>();
|
||||
List<ButtonConfig> buttonsWithoutPosition = new ArrayList<>();
|
||||
HashMap<Integer, SelectionMenuConfig> selectionMenuPositions = new HashMap<>();
|
||||
List<SelectionMenuConfig> selectionMenusWithoutPosition = new ArrayList<>();
|
||||
// we do this by getting all positions which are part of the config
|
||||
// we also track which positions are buttons and which are select menus
|
||||
if(messageConfiguration.getButtons() != null) {
|
||||
messageConfiguration.getButtons().forEach(buttonConfig -> {
|
||||
if(buttonConfig.getPosition() != null) {
|
||||
positions.add(buttonConfig.getPosition());
|
||||
buttonPositions.put(buttonConfig.getPosition(), buttonConfig);
|
||||
} else {
|
||||
buttonsWithoutPosition.add(buttonConfig);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
List<MessageEmbed> embeds = new ArrayList<>();
|
||||
if (!embedBuilders.isEmpty()) {
|
||||
embeds = embedBuilders
|
||||
if(messageConfiguration.getSelectionMenus() != null) {
|
||||
messageConfiguration.getSelectionMenus().forEach(selectionMenuConfig -> {
|
||||
if(selectionMenuConfig.getPosition() != null) {
|
||||
positions.add(selectionMenuConfig.getPosition());
|
||||
selectionMenuPositions.put(selectionMenuConfig.getPosition(), selectionMenuConfig);
|
||||
} else {
|
||||
selectionMenusWithoutPosition.add(selectionMenuConfig);
|
||||
}
|
||||
});
|
||||
}
|
||||
List<Integer> positionsSorted = new ArrayList<>(positions);
|
||||
Collections.sort(positionsSorted);
|
||||
List<ButtonConfig> currentButtons = new ArrayList<>();
|
||||
// we go over all positions, and if its part of the buttons, we only add it to a list of buttons
|
||||
// this will then mean, that all buttons are processed as a group
|
||||
// this is necessary, because we can only add buttons as part of an action row
|
||||
// and in order to make it easier, we process the whole chunk of buttons at once, producing
|
||||
// at least one or more action rows
|
||||
for (Integer position : positionsSorted) {
|
||||
if (buttonPositions.containsKey(position)) {
|
||||
currentButtons.add(buttonPositions.get(position));
|
||||
} else {
|
||||
// if we get interrupted by a selection menu, we process the buttons we have so far
|
||||
// because those should be handled as a group
|
||||
// and then process the selection menu, the selection menu will always represent one full action row
|
||||
// it is not possible to have a button and a menu in the same row
|
||||
if(!currentButtons.isEmpty()) {
|
||||
addButtons(actionRows, componentPayloads, currentButtons);
|
||||
currentButtons.clear();
|
||||
}
|
||||
addSelectionMenu(actionRows, selectionMenuPositions.get(position));
|
||||
}
|
||||
}
|
||||
if(!currentButtons.isEmpty()) {
|
||||
addButtons(actionRows, componentPayloads, currentButtons);
|
||||
currentButtons.clear();
|
||||
}
|
||||
// all the rest without positions will be processed at the end (probably default case for most cases)
|
||||
addButtons(actionRows, componentPayloads, buttonsWithoutPosition);
|
||||
// selection menus are handled afterwards, that is just implied logic
|
||||
// to have a select menu before a button, one would need to set accordingly, or only
|
||||
// set the position for the selection menu, and not for the button
|
||||
selectionMenusWithoutPosition.forEach(selectionMenuConfig -> addSelectionMenu(actionRows, selectionMenuConfig));
|
||||
}
|
||||
setPagingFooters(embedBuilders);
|
||||
if (!embedBuilders.isEmpty()) {
|
||||
embeds = embedBuilders
|
||||
.stream()
|
||||
.filter(embedBuilder -> !embedBuilder.isEmpty())
|
||||
.map(EmbedBuilder::build)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
List<String> messages = new ArrayList<>();
|
||||
@@ -269,6 +312,8 @@ public class TemplateServiceBean implements TemplateService {
|
||||
.messageConfig(createMessageConfig(messageConfiguration.getMessageConfig()))
|
||||
.messages(messages)
|
||||
.ephemeral(isEphemeral)
|
||||
.useComponentsV2(useComponentsV2)
|
||||
.components(discordComponents)
|
||||
.attachedFiles(files)
|
||||
.actionRows(actionRows)
|
||||
.componentPayloads(componentPayloads)
|
||||
@@ -276,6 +321,105 @@ public class TemplateServiceBean implements TemplateService {
|
||||
.build();
|
||||
}
|
||||
|
||||
private net.dv8tion.jda.api.components.Component getComponent(ComponentConfig componentConfig, Map<String, MessageToSend.ComponentConfig> componentPayloads) {
|
||||
if(componentConfig instanceof TopLevelActionRowConfig actionRowConfig) {
|
||||
List<ActionRowChildComponent> actionRowChildComponents = new ArrayList<>();
|
||||
for (ActionRowItemConfig actionRowItemConfig : actionRowConfig.getActionRowItems()) {
|
||||
if(actionRowItemConfig instanceof ButtonConfig actionRowButtonConfig) {
|
||||
Button createdButton = createButtonFromConfig(componentPayloads, actionRowButtonConfig);
|
||||
actionRowChildComponents.add(createdButton);
|
||||
}
|
||||
}
|
||||
return ActionRow.of(actionRowChildComponents);
|
||||
} else if(componentConfig instanceof TopLevelTextDisplay textDisplayConfig) {
|
||||
return createTextDisplay(textDisplayConfig);
|
||||
} else if (componentConfig instanceof TopLevelSectionConfig sectionConfig) {
|
||||
SectionAccessoryComponent accessory = null;
|
||||
if(sectionConfig.getAccessory() != null) {
|
||||
SectionAccessoryConfig sectionAccessoryConfig = sectionConfig.getAccessory();
|
||||
if(sectionAccessoryConfig instanceof SectionButton buttonConfig) {
|
||||
accessory = createButtonFromConfig(componentPayloads, buttonConfig);
|
||||
} else if (sectionAccessoryConfig instanceof SectionThumbnail sectionThumbnailConfig) {
|
||||
accessory = createThumbnail(sectionThumbnailConfig);;
|
||||
}
|
||||
}
|
||||
if(sectionConfig.getComponents() != null && !sectionConfig.getComponents().isEmpty()) {
|
||||
List<SectionContentComponent> sectionComponents = new ArrayList<>();
|
||||
for (SectionComponentConfig sectionComponentConfig : sectionConfig.getComponents()) {
|
||||
if (sectionComponentConfig instanceof TextDisplayConfig textDisplayConfig) {
|
||||
sectionComponents.add(createTextDisplay(textDisplayConfig));
|
||||
}
|
||||
}
|
||||
if(accessory != null) {
|
||||
return Section.of(accessory, sectionComponents);
|
||||
}
|
||||
}
|
||||
} else if(componentConfig instanceof TopLevelFileConfig fileConfig) {
|
||||
if(fileConfig.getFileContent() != null) {
|
||||
File file = fileService.createTempFile(fileConfig.getFileName());
|
||||
try {
|
||||
fileService.writeContentToFile(file, fileConfig.getFileContent());
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to write local temporary file.", e);
|
||||
throw new AbstractoRunTimeException(e);
|
||||
}
|
||||
return FileDisplay.fromFile(fileConfig.convertToFileUpload(file));
|
||||
} else {
|
||||
// NOT SUPPORTED ( right now)
|
||||
}
|
||||
} else if(componentConfig instanceof TopLevelMediaGalleryConfig mediaGalleryConfig) {
|
||||
if(mediaGalleryConfig.getImages() != null) {
|
||||
List<MediaGalleryItem> galleryItems = new ArrayList<>();
|
||||
mediaGalleryConfig.getImages().forEach(imageConfig -> {
|
||||
MediaGalleryItem item = MediaGalleryItem.fromUrl(imageConfig.getUrl());
|
||||
if(imageConfig.getSpoiler() != null) {
|
||||
item = item.withSpoiler(imageConfig.getSpoiler());
|
||||
}
|
||||
if(imageConfig.getDescription() != null) {
|
||||
item = item.withDescription(imageConfig.getDescription());
|
||||
}
|
||||
galleryItems.add(item);
|
||||
});
|
||||
return MediaGallery.of(galleryItems);
|
||||
}
|
||||
} else if(componentConfig instanceof TopLevelSeperatorConfig seperatorConfig) {
|
||||
return Separator.create(seperatorConfig.getDivider(),
|
||||
Spacing.toSpacing(seperatorConfig.getSpacing()));
|
||||
} else if(componentConfig instanceof TopLevelContainerConfig topLevelContainerConfig) {
|
||||
List<ContainerChildComponent> childComponents = new ArrayList<>();
|
||||
for (ComponentConfig containerComponent : topLevelContainerConfig.getComponents()) {
|
||||
net.dv8tion.jda.api.components.Component createdComponent = getComponent(containerComponent, componentPayloads);
|
||||
if(createdComponent != null) {
|
||||
childComponents.add((ContainerChildComponent) createdComponent);
|
||||
}
|
||||
}
|
||||
return Container.of(childComponents);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Thumbnail createThumbnail(SectionThumbnail sectionThumbnailConfig) {
|
||||
Thumbnail thumbnail = Thumbnail.fromUrl(sectionThumbnailConfig.getUrl());
|
||||
if(sectionThumbnailConfig.getSpoiler() != null) {
|
||||
thumbnail.withSpoiler(sectionThumbnailConfig.getSpoiler());
|
||||
}
|
||||
if(sectionThumbnailConfig.getDescription() != null) {
|
||||
thumbnail.withDescription(sectionThumbnailConfig.getDescription());
|
||||
}
|
||||
if(sectionThumbnailConfig.getUniqueId() != null) {
|
||||
thumbnail.withUniqueId(sectionThumbnailConfig.getUniqueId());
|
||||
}
|
||||
return thumbnail;
|
||||
}
|
||||
|
||||
private TextDisplay createTextDisplay(TextDisplayConfig textDisplayConfig) {
|
||||
TextDisplay textDisplay = TextDisplay.of(textDisplayConfig.getContent());
|
||||
if(textDisplayConfig.getUniqueId() != null) {
|
||||
textDisplay.withUniqueId(textDisplayConfig.getUniqueId());
|
||||
}
|
||||
return textDisplay;
|
||||
}
|
||||
|
||||
private void addSelectionMenu(List<ActionRow> actionRows, SelectionMenuConfig selectionMenuConfig) {
|
||||
ItemComponent selectionMenu;
|
||||
if (selectionMenuConfig.getType() == SelectionMenuType.STRING) {
|
||||
@@ -346,53 +490,64 @@ public class TemplateServiceBean implements TemplateService {
|
||||
}
|
||||
|
||||
private void addButtons(List<ActionRow> actionRows, Map<String, MessageToSend.ComponentConfig> componentPayloads, List<ButtonConfig> buttonConfigs) {
|
||||
ActionRow currentRow = null;
|
||||
List<Button> currentButtons = null;
|
||||
for (ButtonConfig buttonConfig : buttonConfigs) {
|
||||
ButtonMetaConfig metaConfig = buttonConfig.getMetaConfig() != null ? buttonConfig.getMetaConfig() : null;
|
||||
String id = metaConfig != null && Boolean.TRUE.equals(metaConfig.getGenerateRandomUUID()) ?
|
||||
UUID.randomUUID().toString() : buttonConfig.getId();
|
||||
String componentOrigin = metaConfig != null ? metaConfig.getButtonOrigin() : null;
|
||||
MessageToSend.ComponentConfig componentConfig = null;
|
||||
try {
|
||||
componentConfig = MessageToSend.ComponentConfig
|
||||
.builder()
|
||||
.componentOrigin(componentOrigin)
|
||||
.componentType(ComponentType.BUTTON)
|
||||
.persistCallback(metaConfig != null && Boolean.TRUE.equals(metaConfig.getPersistCallback()))
|
||||
.payload(buttonConfig.getButtonPayload())
|
||||
.payloadType(buttonConfig.getPayloadType() != null ? Class.forName(buttonConfig.getPayloadType()) : null)
|
||||
.build();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new AbstractoRunTimeException("Referenced class in button config could not be found: " + buttonConfig.getPayloadType(), e);
|
||||
}
|
||||
componentPayloads.put(id, componentConfig);
|
||||
String idOrUl = buttonConfig.getUrl() == null ? buttonConfig.getId() : buttonConfig.getUrl();
|
||||
Button createdButton = Button.of(ButtonStyleConfig.getStyle(buttonConfig.getButtonStyle()), idOrUl, buttonConfig.getLabel());
|
||||
if (buttonConfig.getDisabled() != null) {
|
||||
createdButton = createdButton.withDisabled(buttonConfig.getDisabled());
|
||||
}
|
||||
if (buttonConfig.getEmoteMarkdown() != null) {
|
||||
createdButton = createdButton.withEmoji(Emoji.fromFormatted(buttonConfig.getEmoteMarkdown()));
|
||||
}
|
||||
if(currentRow == null) {
|
||||
currentRow = ActionRow.of(createdButton);
|
||||
} else if (
|
||||
(
|
||||
metaConfig != null &&
|
||||
Boolean.TRUE.equals(metaConfig.getForceNewRow())
|
||||
)
|
||||
|| currentRow.getComponents().size() == ComponentServiceBean.MAX_BUTTONS_PER_ROW) {
|
||||
actionRows.add(currentRow);
|
||||
currentRow = ActionRow.of(createdButton);
|
||||
boolean forceNewRowFromConfig = metaConfig != null &&
|
||||
Boolean.TRUE.equals(metaConfig.getForceNewRow());
|
||||
Button createdButton = createButtonFromConfig(componentPayloads, buttonConfig);
|
||||
if(currentButtons == null) {
|
||||
currentButtons = new ArrayList<>();
|
||||
currentButtons.add(createdButton);
|
||||
} else {
|
||||
currentRow.getComponents().add(createdButton);
|
||||
if (
|
||||
forceNewRowFromConfig
|
||||
|| currentButtons.size() == ComponentServiceBean.MAX_BUTTONS_PER_ROW) {
|
||||
actionRows.add(ActionRow.of(currentButtons));
|
||||
currentButtons = new ArrayList<>();
|
||||
currentButtons.add(createdButton);
|
||||
} else {
|
||||
currentButtons.add(createdButton);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(currentRow != null) {
|
||||
actionRows.add(currentRow);
|
||||
if(currentButtons != null) {
|
||||
actionRows.add(ActionRow.of(currentButtons));
|
||||
}
|
||||
}
|
||||
|
||||
private Button createButtonFromConfig(Map<String, MessageToSend.ComponentConfig> componentPayloads, ButtonConfig buttonConfig) {
|
||||
ButtonMetaConfig metaConfig = buttonConfig.getMetaConfig() != null ? buttonConfig.getMetaConfig() : null;
|
||||
String id = metaConfig != null && Boolean.TRUE.equals(metaConfig.getGenerateRandomUUID()) ?
|
||||
UUID.randomUUID().toString() : buttonConfig.getId();
|
||||
String componentOrigin = metaConfig != null ? metaConfig.getButtonOrigin() : null;
|
||||
MessageToSend.ComponentConfig componentConfig = null;
|
||||
try {
|
||||
componentConfig = MessageToSend.ComponentConfig
|
||||
.builder()
|
||||
.componentOrigin(componentOrigin)
|
||||
.componentType(ComponentType.BUTTON)
|
||||
.persistCallback(metaConfig != null && Boolean.TRUE.equals(metaConfig.getPersistCallback()))
|
||||
.payload(buttonConfig.getButtonPayload())
|
||||
.payloadType(buttonConfig.getPayloadType() != null ? Class.forName(buttonConfig.getPayloadType()) : null)
|
||||
.build();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new AbstractoRunTimeException("Referenced class in button config could not be found: " + buttonConfig.getPayloadType(), e);
|
||||
}
|
||||
componentPayloads.put(id, componentConfig);
|
||||
String idOrUl = buttonConfig.getUrl() == null ? buttonConfig.getId() : buttonConfig.getUrl();
|
||||
Button createdButton;
|
||||
if(StringUtils.isBlank(buttonConfig.getLabel())) {
|
||||
createdButton = Button.of(ButtonStyleConfig.getStyle(buttonConfig.getButtonStyle()), idOrUl, buttonConfig.getEmoteMarkdown());
|
||||
} else {
|
||||
createdButton = Button.of(ButtonStyleConfig.getStyle(buttonConfig.getButtonStyle()), idOrUl, buttonConfig.getLabel());
|
||||
}
|
||||
if (buttonConfig.getDisabled() != null) {
|
||||
createdButton = createdButton.withDisabled(buttonConfig.getDisabled());
|
||||
}
|
||||
return createdButton;
|
||||
}
|
||||
|
||||
private void convertEmbeds(MessageConfiguration messageConfiguration, List<EmbedBuilder> embedBuilders) {
|
||||
int currentEffectiveEmbed;
|
||||
for (int embedIndex = 0; embedIndex < messageConfiguration.getEmbeds().size(); embedIndex++) {
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.dv8tion</groupId>
|
||||
<groupId>io.github.freya022</groupId>
|
||||
<artifactId>JDA</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@@ -3,10 +3,10 @@ package dev.sheldan.abstracto.core.interaction;
|
||||
import dev.sheldan.abstracto.core.interaction.button.ButtonConfigModel;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionComponent;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||
import net.dv8tion.jda.api.interactions.components.Component;
|
||||
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
|
||||
import net.dv8tion.jda.api.components.ActionComponent;
|
||||
import net.dv8tion.jda.api.components.actionrow.ActionRow;
|
||||
import net.dv8tion.jda.api.components.Component;
|
||||
import net.dv8tion.jda.api.components.buttons.ButtonStyle;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@@ -14,6 +14,7 @@ public class CachedAttachment implements Serializable {
|
||||
private Long id;
|
||||
private String proxyUrl;
|
||||
private String fileName;
|
||||
private String contentType;
|
||||
private Boolean spoiler;
|
||||
private Integer size;
|
||||
private Integer height;
|
||||
|
||||
@@ -10,7 +10,7 @@ import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||
import net.dv8tion.jda.api.components.actionrow.ActionRow;
|
||||
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
|
||||
import net.dv8tion.jda.api.requests.restaction.MessageEditAction;
|
||||
|
||||
|
||||
@@ -8,10 +8,10 @@ import lombok.Setter;
|
||||
@Setter
|
||||
@Builder
|
||||
public class MessageConfig {
|
||||
private boolean allowsRoleMention;
|
||||
private boolean allowsEveryoneMention;
|
||||
private Boolean allowsRoleMention;
|
||||
private Boolean allowsEveryoneMention;
|
||||
@Builder.Default
|
||||
private boolean allowsUserMention = true;
|
||||
private Boolean allowsUserMention = true;
|
||||
@Builder.Default
|
||||
private boolean mentionsReferencedMessage = true;
|
||||
private Boolean mentionsReferencedMessage = true;
|
||||
}
|
||||
|
||||
@@ -4,8 +4,9 @@ import dev.sheldan.abstracto.core.models.database.ComponentType;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.dv8tion.jda.api.components.MessageTopLevelComponent;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||
import net.dv8tion.jda.api.components.actionrow.ActionRow;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -20,10 +21,16 @@ import java.util.Map;
|
||||
@Builder
|
||||
public class MessageToSend {
|
||||
/**
|
||||
* The collections of embeds to be send. The first embed is in the same message as the string message.
|
||||
* The collections of embeds to send. The first embed is in the same message as the string message.
|
||||
*/
|
||||
@Builder.Default
|
||||
private List<MessageEmbed> embeds = new ArrayList<>();
|
||||
|
||||
@Builder.Default
|
||||
private List<MessageTopLevelComponent> components = new ArrayList<>();
|
||||
|
||||
@Builder.Default
|
||||
private Boolean useComponentsV2 = false;
|
||||
/**
|
||||
* The string content to be used in the first message.
|
||||
*/
|
||||
|
||||
@@ -55,7 +55,7 @@
|
||||
|
||||
<properties>
|
||||
<maven.build.timestamp.format>yyyy/MM/dd HH:mm</maven.build.timestamp.format>
|
||||
<jda.version>5.4.0</jda.version>
|
||||
<jda.version>92f4c2d210</jda.version>
|
||||
<asciidoctor.maven.plugin.version>2.2.6</asciidoctor.maven.plugin.version>
|
||||
<asciidoctorj.pdf.version>1.5.3</asciidoctorj.pdf.version>
|
||||
<asciidoctorj.version>2.3.0</asciidoctorj.version>
|
||||
@@ -63,7 +63,7 @@
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<guava.version>32.1.1-jre</guava.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
<gson.version>2.13.1</gson.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
<commons-io.version>2.13.0</commons-io.version>
|
||||
<mockito-core.version>5.4.0</mockito-core.version>
|
||||
@@ -81,6 +81,7 @@
|
||||
<ehcache.version>3.10.8</ehcache.version>
|
||||
<hibernate-jcache.version>6.2.5.Final</hibernate-jcache.version>
|
||||
<javax-annotation-api.version>1.3.2</javax-annotation-api.version>
|
||||
<gson-extras.version>3.3.0</gson-extras.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
@@ -153,7 +154,7 @@
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.dv8tion</groupId>
|
||||
<groupId>io.github.freya022</groupId>
|
||||
<artifactId>JDA</artifactId>
|
||||
<version>${jda.version}</version>
|
||||
<exclusions>
|
||||
@@ -197,6 +198,12 @@
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.danilopianini</groupId>
|
||||
<artifactId>gson-extras</artifactId>
|
||||
<version>${gson-extras.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
|
||||
Reference in New Issue
Block a user