[AB-xxx] upgrading to JDA 6.2.0

changing how assignable role places change their buttons
This commit is contained in:
Sheldan
2025-12-28 19:17:58 +01:00
parent cf9e5f67f6
commit 4180a07243
24 changed files with 117 additions and 173 deletions

View File

@@ -1,32 +1,21 @@
package dev.sheldan.abstracto.core.interaction;
import dev.sheldan.abstracto.core.interaction.button.ButtonConfigModel;
import dev.sheldan.abstracto.core.service.ChannelService;
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.components.ActionComponent;
import net.dv8tion.jda.api.components.actionrow.ActionRow;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.dv8tion.jda.api.components.buttons.Button;
import net.dv8tion.jda.api.components.buttons.ButtonStyle;
import org.apache.commons.collections4.ListUtils;
import net.dv8tion.jda.api.components.tree.MessageComponentTree;
import net.dv8tion.jda.api.entities.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
@Component
public class ComponentServiceBean implements ComponentService {
@Autowired
private MessageService messageService;
@Autowired
private ChannelService channelService;
@Override
public String generateComponentId(Long serverId) {
return generateComponentId();
@@ -38,95 +27,48 @@ public class ComponentServiceBean implements ComponentService {
}
@Override
public CompletableFuture<Message> addButtonToMessage(Long messageId, GuildMessageChannel textChannel, String buttonId, String description, String emoteMarkdown, ButtonStyle style) {
return channelService.retrieveMessageInChannel(textChannel, messageId).thenCompose(message -> {
Button button = Button.of(style, buttonId, description);
if(emoteMarkdown != null) {
button = button.withEmoji(Emoji.fromFormatted(emoteMarkdown));
}
List<ActionRow> actionRows;
if(message.getActionRows().isEmpty()) {
actionRows = Arrays.asList(ActionRow.of(button));
public CompletableFuture<Message> clearComponents(Message message) {
MessageComponentTree tree = message.getComponentTree().replace(oldComponent -> null);
return messageService.editMessage(message, tree);
}
@Override
public CompletableFuture<Message> disableAllComponents(Message message) {
return setAllComponentStatesTo(message, true);
}
@Override
public CompletableFuture<Message> enableAllComponents(Message message) {
return setAllComponentStatesTo(message, false);
}
@Override
public CompletableFuture<Message> removeComponentById(Message message, String componentId) {
MessageComponentTree tree;
tree = message.getComponentTree().replace(oldComponent -> {
if (oldComponent instanceof Button && componentId.equals(((Button) oldComponent).getCustomId())) {
return null;
} else {
ActionRow lastRow = message.getActionRows().get(message.getActionRows().size() - 1);
if(lastRow.getComponents().size() < MAX_BUTTONS_PER_ROW) {
lastRow.getButtons().add(button);
actionRows = message.getActionRows();
} else {
List<ActionRow> currentActionRows = new ArrayList<>(message.getActionRows());
currentActionRows.add(ActionRow.of(button));
actionRows = currentActionRows;
}
return oldComponent;
}
return messageService.editMessageWithActionRowsMessage(message, actionRows);
});
return messageService.editMessage(message, tree);
}
@Override
public CompletableFuture<Void> clearButtons(Message message) {
return messageService.editMessageWithActionRows(message, new ArrayList<>());
}
@Override
public CompletableFuture<Void> disableAllButtons(Message message) {
return setAllButtonStatesTo(message, true);
}
@Override
public CompletableFuture<Void> enableAllButtons(Message message) {
return setAllButtonStatesTo(message, false);
}
@Override
public CompletableFuture<Void> removeComponentWithId(Message message, String componentId) {
return removeComponentWithId(message, componentId, false);
}
@Override
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.components.ActionComponent> components = new ArrayList<>();
message.getActionRows().forEach(row ->
row
.getComponents()
.stream()
.filter(ActionComponent.class::isInstance)
.map(ActionComponent.class::cast)
.filter(component -> component.getId() == null || !component.getId().equals(componentId))
.forEach(components::add));
actionRows = splitIntoActionRowsMax(components);
} else {
for (ActionRow row : message.getActionRows()) {
actionRows.add(ActionRow.of(
row
.getComponents()
.stream()
.filter(ActionComponent.class::isInstance)
.map(ActionComponent.class::cast)
.filter(component -> component.getId() == null || !component.getId().equals(componentId))
.collect(Collectors.toList())));
}
}
return messageService.editMessageWithActionRows(message, actionRows);
}
@Override
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());
}
@Override
public ButtonConfigModel createButtonConfigModel() {
return ButtonConfigModel.builder().buttonId(generateComponentId()).build();
}
private CompletableFuture<Void> setAllButtonStatesTo(Message message, Boolean disabled) {
List<ActionRow> actionRows = new ArrayList<>();
message.getActionRows().forEach(row -> actionRows.add(row.withDisabled(disabled)));
return messageService.editMessageWithActionRows(message, actionRows);
private CompletableFuture<Message> setAllComponentStatesTo(Message message, Boolean disabled) {
MessageComponentTree tree;
if(disabled) {
tree = message.getComponentTree().asDisabled();
} else {
tree = message.getComponentTree().asEnabled();
}
return messageService.editMessage(message, tree);
}
}

View File

@@ -94,9 +94,9 @@ public class InteractionServiceBean implements InteractionService {
if(!actionRows.isEmpty()) {
AServer server = serverManagementService.loadServer(interactionHook.getInteraction().getGuild());
allMessageActions.set(0, allMessageActions.get(0).addComponents(actionRows));
actionRows.forEach(components -> components.forEach(component -> {
actionRows.forEach(components -> components.getComponents().forEach(component -> {
if(component instanceof ActionComponent) {
String id = ((ActionComponent)component).getId();
String id = ((ActionComponent)component).getCustomId();
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
if(payload != null && payload.getPersistCallback()) {
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());
@@ -228,9 +228,9 @@ public class InteractionServiceBean implements InteractionService {
server = serverManagementService.loadServer(serverId);
} else {
server = null; }
actionRows.forEach(components -> components.forEach(component -> {
actionRows.forEach(components -> components.getComponents().forEach(component -> {
if(component instanceof ActionComponent) {
String id = ((ActionComponent)component).getId();
String id = ((ActionComponent)component).getCustomId();
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
if(payload != null && payload.getPersistCallback()) {
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());
@@ -301,9 +301,9 @@ public class InteractionServiceBean implements InteractionService {
} else {
server = null;
}
actionRows.forEach(components -> components.forEach(component -> {
actionRows.forEach(components -> components.getComponents().forEach(component -> {
if(component instanceof ActionComponent) {
String id = ((ActionComponent)component).getId();
String id = ((ActionComponent)component).getCustomId();
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
if(payload != null && payload.getPersistCallback()) {
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());

View File

@@ -6,12 +6,12 @@ import dev.sheldan.abstracto.core.interaction.modal.config.ModalConfig;
import dev.sheldan.abstracto.core.interaction.modal.config.TextInputComponent;
import dev.sheldan.abstracto.core.interaction.modal.config.TextInputComponentStyle;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import net.dv8tion.jda.api.components.ModalTopLevelComponent;
import net.dv8tion.jda.api.components.label.Label;
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.components.actionrow.ActionRow;
import net.dv8tion.jda.api.interactions.components.ItemComponent;
import net.dv8tion.jda.api.components.textinput.TextInput;
import net.dv8tion.jda.api.interactions.modals.Modal;
import net.dv8tion.jda.api.modals.Modal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -55,29 +55,27 @@ public class ModalServiceBean implements ModalService {
.build();
}
private List<ActionRow> convertToActionRows(List<ModalComponent> components) {
private List<ModalTopLevelComponent> convertToActionRows(List<ModalComponent> components) {
return components
.stream()
.map(this::convertComponent)
.map(ActionRow::of)
.collect(Collectors.toList());
}
private ItemComponent convertComponent(ModalComponent component) {
private ModalTopLevelComponent convertComponent(ModalComponent component) {
if(component instanceof TextInputComponent) {
TextInputComponent tic = (TextInputComponent) component;
TextInput.Builder builder = TextInput
.create(tic.getId(), tic.getLabel(), TextInputComponentStyle.getStyle(tic.getStyle()));
TextInput.Builder tiBuilder = TextInput.create(tic.getId(), TextInputComponentStyle.getStyle(tic.getStyle()));
if(tic.getMinLength() != null) {
builder.setMinLength(tic.getMinLength());
tiBuilder.setMinLength(tic.getMinLength());
}
if(tic.getMaxLength() != null) {
builder.setMaxLength(tic.getMaxLength());
tiBuilder.setMaxLength(tic.getMaxLength());
}
if(tic.getRequired() != null) {
builder.setRequired(tic.getRequired());
tiBuilder.setRequired(tic.getRequired());
}
return builder.build();
return Label.of(tic.getLabel(), tiBuilder.build());
}
return null;
}

View File

@@ -37,7 +37,7 @@ public class BotServiceBean implements BotService {
@Autowired
private OkHttpLogger okHttpLogger;
@Value("${abstracto.intents:GUILD_VOICE_STATES,GUILD_MODERATION,MESSAGE_CONTENT,GUILD_EMOJIS_AND_STICKERS,GUILD_MEMBERS,GUILD_MESSAGES,GUILD_MESSAGE_REACTIONS,DIRECT_MESSAGES,GUILD_PRESENCES}")
@Value("${abstracto.intents:GUILD_VOICE_STATES,GUILD_MODERATION,MESSAGE_CONTENT,GUILD_EXPRESSIONS,GUILD_MEMBERS,GUILD_MESSAGES,GUILD_MESSAGE_REACTIONS,DIRECT_MESSAGES,GUILD_PRESENCES}")
private String[] intents;
@Value("${abstracto.cacheFlags:ACTIVITY,ONLINE_STATUS,VOICE_STATE}")

View File

@@ -279,9 +279,9 @@ public class ChannelServiceBean implements ChannelService {
server = serverManagementService.loadServer(channel.getGuild());
}
for (ActionRow row : actionRows) {
for (ActionRowChildComponent component : row) {
for (ActionRowChildComponent component : row.getComponents()) {
if (component instanceof ActionComponent) {
String id = ((ActionComponent) component).getId();
String id = ((ActionComponent) component).getCustomId();
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
if (payload != null && payload.getPersistCallback()) {
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());

View File

@@ -12,11 +12,11 @@ import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.components.tree.MessageComponentTree;
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.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;
@@ -267,14 +267,9 @@ public class MessageServiceBean implements MessageService {
}
@Override
public CompletableFuture<Void> editMessageWithActionRows(Message message, List<ActionRow> rows) {
return editMessageWithActionRowsMessage(message, rows).thenApply(message1 -> null);
}
@Override
public CompletableFuture<Message> editMessageWithActionRowsMessage(Message message, List<ActionRow> rows) {
public CompletableFuture<Message> editMessage(Message message, MessageComponentTree componentTree) {
metricService.incrementCounter(MESSAGE_EDIT_METRIC);
return message.editMessageComponents(rows).submit();
return message.editMessageComponents(componentTree.getComponents()).submit();
}
@Override

View File

@@ -47,6 +47,7 @@ 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.selections.SelectMenu;
import net.dv8tion.jda.api.components.separator.Separator;
import net.dv8tion.jda.api.components.textdisplay.TextDisplay;
import net.dv8tion.jda.api.components.thumbnail.Thumbnail;
@@ -54,7 +55,6 @@ 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.components.actionrow.ActionRow;
import net.dv8tion.jda.api.interactions.components.ItemComponent;
import net.dv8tion.jda.api.components.buttons.Button;
import net.dv8tion.jda.api.components.selections.EntitySelectMenu;
import net.dv8tion.jda.api.components.selections.SelectOption;
@@ -449,7 +449,7 @@ public class TemplateServiceBean implements TemplateService {
}
private void addSelectionMenu(List<ActionRow> actionRows, SelectionMenuConfig selectionMenuConfig) {
ItemComponent selectionMenu;
SelectMenu selectionMenu;
if (selectionMenuConfig.getType() == SelectionMenuType.STRING) {
List<SelectOption> selectOptions = selectionMenuConfig.getMenuEntries().stream().map(selectionMenuEntry -> {
SelectOption option = SelectOption.of(selectionMenuEntry.getLabel(), selectionMenuEntry.getValue());