Skip to content

Commit 8ad3f8c

Browse files
committed
Migrating to components v2 api
1 parent 93128e2 commit 8ad3f8c

27 files changed

+175
-153
lines changed

application/src/main/java/org/togetherjava/tjbot/features/MessageContextCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.togetherjava.tjbot.features;
22

3+
import net.dv8tion.jda.api.components.buttons.ButtonStyle;
34
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
45
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
56
import net.dv8tion.jda.api.events.interaction.component.EntitySelectInteractionEvent;
67
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
78
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
89
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
9-
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
1010

1111
import org.togetherjava.tjbot.features.componentids.ComponentIdGenerator;
1212

@@ -49,8 +49,8 @@ public interface MessageContextCommand extends BotCommand {
4949
* <p>
5050
* Buttons or menus have to be created with a component ID (see
5151
* {@link ComponentInteraction#getComponentId()},
52-
* {@link net.dv8tion.jda.api.interactions.components.buttons.Button#of(ButtonStyle, String, String)})
53-
* in a very specific format, otherwise the core system will fail to identify the command that
52+
* {@link net.dv8tion.jda.api.components.buttons.Button#of(ButtonStyle, String, String)}) in a
53+
* very specific format, otherwise the core system will fail to identify the command that
5454
* corresponded to the button or menu click event and is unable to route it back.
5555
* <p>
5656
* See {@link #acceptComponentIdGenerator(ComponentIdGenerator)} for more info.

application/src/main/java/org/togetherjava/tjbot/features/SlashCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.togetherjava.tjbot.features;
22

3+
import net.dv8tion.jda.api.components.buttons.ButtonStyle;
34
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
45
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
56
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
@@ -10,7 +11,6 @@
1011
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
1112
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
1213
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
13-
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
1414

1515
import org.togetherjava.tjbot.features.componentids.ComponentIdGenerator;
1616

@@ -82,8 +82,8 @@ public interface SlashCommand extends BotCommand {
8282
* <p>
8383
* Buttons or menus have to be created with a component ID (see
8484
* {@link ComponentInteraction#getComponentId()},
85-
* {@link net.dv8tion.jda.api.interactions.components.buttons.Button#of(ButtonStyle, String, String)})
86-
* in a very specific format, otherwise the core system will fail to identify the command that
85+
* {@link net.dv8tion.jda.api.components.buttons.Button#of(ButtonStyle, String, String)}) in a
86+
* very specific format, otherwise the core system will fail to identify the command that
8787
* corresponded to the button or menu click event and is unable to route it back.
8888
* <p>
8989
* See {@link #acceptComponentIdGenerator(ComponentIdGenerator)} for more info on the ID's.

application/src/main/java/org/togetherjava/tjbot/features/UserContextCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.togetherjava.tjbot.features;
22

3+
import net.dv8tion.jda.api.components.buttons.ButtonStyle;
34
import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent;
45
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
56
import net.dv8tion.jda.api.events.interaction.component.EntitySelectInteractionEvent;
67
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
78
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
89
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
9-
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
1010

1111
import org.togetherjava.tjbot.features.componentids.ComponentIdGenerator;
1212

@@ -50,8 +50,8 @@ public interface UserContextCommand extends BotCommand {
5050
* <p>
5151
* Buttons or menus have to be created with a component ID (see
5252
* {@link ComponentInteraction#getComponentId()},
53-
* {@link net.dv8tion.jda.api.interactions.components.buttons.Button#of(ButtonStyle, String, String)})
54-
* in a very specific format, otherwise the core system will fail to identify the command that
53+
* {@link net.dv8tion.jda.api.components.buttons.Button#of(ButtonStyle, String, String)}) in a
54+
* very specific format, otherwise the core system will fail to identify the command that
5555
* corresponded to the button or menu click event and is unable to route it back.
5656
* <p>
5757
* See {@link #acceptComponentIdGenerator(ComponentIdGenerator)} for more info.

application/src/main/java/org/togetherjava/tjbot/features/basic/RoleSelectCommand.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import net.dv8tion.jda.api.EmbedBuilder;
44
import net.dv8tion.jda.api.Permission;
5+
import net.dv8tion.jda.api.components.actionrow.ActionRow;
6+
import net.dv8tion.jda.api.components.selections.SelectOption;
7+
import net.dv8tion.jda.api.components.selections.StringSelectMenu;
8+
import net.dv8tion.jda.api.components.selections.StringSelectMenu.Builder;
59
import net.dv8tion.jda.api.entities.Guild;
610
import net.dv8tion.jda.api.entities.IMentionable;
711
import net.dv8tion.jda.api.entities.Member;
@@ -16,8 +20,6 @@
1620
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
1721
import net.dv8tion.jda.api.interactions.commands.OptionType;
1822
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
19-
import net.dv8tion.jda.api.interactions.components.selections.SelectOption;
20-
import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu;
2123
import org.jetbrains.annotations.Contract;
2224
import org.slf4j.Logger;
2325
import org.slf4j.LoggerFactory;
@@ -183,7 +185,7 @@ private static boolean handleInteractableRolesSelected(IReplyCallback event,
183185

184186
private void sendRoleSelectionMenu(final CommandInteraction event,
185187
final Collection<? extends Role> selectableRoles) {
186-
StringSelectMenu.Builder menu = StringSelectMenu
188+
Builder menu = StringSelectMenu
187189
.create(generateComponentId(Lifespan.PERMANENT, event.getUser().getId()))
188190
.setPlaceholder("Select your roles")
189191
.setMinValues(0)
@@ -197,7 +199,7 @@ private void sendRoleSelectionMenu(final CommandInteraction event,
197199
event.getOption(TITLE_OPTION, "Select your roles:", OptionMapping::getAsString);
198200
MessageEmbed embed = createEmbed(title, event.getOption(DESCRIPTION_OPTION).getAsString());
199201

200-
event.replyEmbeds(embed).addActionRow(menu.build()).queue();
202+
event.replyEmbeds(embed).addComponents(ActionRow.of(menu.build())).queue();
201203
}
202204

203205
private static SelectOption mapToSelectOption(Role role) {

application/src/main/java/org/togetherjava/tjbot/features/bookmarks/BookmarksListRemoveHandler.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
import net.dv8tion.jda.api.EmbedBuilder;
44
import net.dv8tion.jda.api.JDA;
5+
import net.dv8tion.jda.api.components.MessageTopLevelComponent;
6+
import net.dv8tion.jda.api.components.actionrow.ActionRow;
7+
import net.dv8tion.jda.api.components.buttons.Button;
8+
import net.dv8tion.jda.api.components.selections.SelectMenu;
9+
import net.dv8tion.jda.api.components.selections.SelectOption;
10+
import net.dv8tion.jda.api.components.selections.StringSelectMenu;
511
import net.dv8tion.jda.api.entities.MessageEmbed;
612
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
713
import net.dv8tion.jda.api.entities.emoji.Emoji;
814
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent;
915
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
1016
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
11-
import net.dv8tion.jda.api.interactions.components.ActionRow;
1217
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
13-
import net.dv8tion.jda.api.interactions.components.LayoutComponent;
14-
import net.dv8tion.jda.api.interactions.components.buttons.Button;
15-
import net.dv8tion.jda.api.interactions.components.selections.SelectMenu;
16-
import net.dv8tion.jda.api.interactions.components.selections.SelectOption;
17-
import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu;
1818

1919
import org.togetherjava.tjbot.db.generated.tables.records.BookmarksRecord;
2020
import org.togetherjava.tjbot.features.utils.MessageUtils;
@@ -75,7 +75,7 @@ private void handlePaginatedRequest(GenericCommandInteractionEvent event,
7575

7676
MessageEmbed pageEmbed = generatePageEmbed(bookmarks, requestType, 0);
7777

78-
Collection<LayoutComponent> components = new ArrayList<>();
78+
Collection<MessageTopLevelComponent> components = new ArrayList<>();
7979
components.add(generateNavigationComponent(requestType, 0));
8080

8181
if (requestType == RequestType.REMOVE) {
@@ -199,7 +199,7 @@ private void updatePagination(ComponentInteraction event, Request request,
199199
MessageEmbed pageEmbed =
200200
generatePageEmbed(bookmarks, request.type, request.pageToDisplayIndex);
201201

202-
Collection<LayoutComponent> components = new ArrayList<>();
202+
Collection<MessageTopLevelComponent> components = new ArrayList<>();
203203
components.add(generateNavigationComponent(request.type, request.pageToDisplayIndex));
204204
if (request.type == RequestType.REMOVE) {
205205
components.addAll(generateRemoveComponents(event.getJDA(), bookmarks, request.type,
@@ -209,7 +209,7 @@ private void updatePagination(ComponentInteraction event, Request request,
209209
event.editMessageEmbeds(pageEmbed).setComponents(components).queue();
210210
}
211211

212-
private LayoutComponent generateNavigationComponent(RequestType requestType,
212+
private MessageTopLevelComponent generateNavigationComponent(RequestType requestType,
213213
int pageToDisplayIndex) {
214214
UnaryOperator<String> generateNavigationComponentId = name -> {
215215
Request request = new Request(requestType, name, pageToDisplayIndex, Set.of());
@@ -226,7 +226,7 @@ private LayoutComponent generateNavigationComponent(RequestType requestType,
226226
return ActionRow.of(buttonPrev, buttonNext);
227227
}
228228

229-
private List<LayoutComponent> generateRemoveComponents(JDA jda,
229+
private List<MessageTopLevelComponent> generateRemoveComponents(JDA jda,
230230
List<? extends BookmarksRecord> bookmarks, RequestType requestType,
231231
int pageToDisplayIndex, Set<Long> bookmarksToRemoveChannelIDs) {
232232
List<PageEntry> pageEntries = getPageEntries(bookmarks, pageToDisplayIndex);

application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptCommand.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import com.github.benmanes.caffeine.cache.Cache;
44
import com.github.benmanes.caffeine.cache.Caffeine;
5+
import net.dv8tion.jda.api.components.label.Label;
6+
import net.dv8tion.jda.api.components.textinput.TextInput;
7+
import net.dv8tion.jda.api.components.textinput.TextInputStyle;
58
import net.dv8tion.jda.api.entities.MessageEmbed;
69
import net.dv8tion.jda.api.entities.SelfUser;
710
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
811
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
9-
import net.dv8tion.jda.api.interactions.components.text.TextInput;
10-
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
11-
import net.dv8tion.jda.api.interactions.modals.Modal;
12+
import net.dv8tion.jda.api.modals.Modal;
1213

1314
import org.togetherjava.tjbot.features.CommandVisibility;
1415
import org.togetherjava.tjbot.features.SlashCommandAdapter;
@@ -65,14 +66,14 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
6566
return;
6667
}
6768

68-
TextInput body = TextInput
69-
.create(QUESTION_INPUT, "Ask ChatGPT a question or get help with code",
70-
TextInputStyle.PARAGRAPH)
69+
TextInput body = TextInput.create(QUESTION_INPUT, TextInputStyle.PARAGRAPH)
7170
.setPlaceholder("Put your question for ChatGPT here")
7271
.setRequiredRange(MIN_MESSAGE_INPUT_LENGTH, MAX_MESSAGE_INPUT_LENGTH)
7372
.build();
7473

75-
Modal modal = Modal.create(generateComponentId(), "ChatGPT").addActionRow(body).build();
74+
Modal modal = Modal.create(generateComponentId(), "ChatGPT")
75+
.addComponents(Label.of("Ask ChatGPT a question or get help with code", body))
76+
.build();
7677
event.replyModal(modal).queue();
7778
}
7879

application/src/main/java/org/togetherjava/tjbot/features/code/CodeMessageHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import com.github.benmanes.caffeine.cache.Cache;
44
import com.github.benmanes.caffeine.cache.Caffeine;
5+
import net.dv8tion.jda.api.components.actionrow.ActionRow;
6+
import net.dv8tion.jda.api.components.buttons.Button;
57
import net.dv8tion.jda.api.entities.Message;
68
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
79
import net.dv8tion.jda.api.events.message.MessageDeleteEvent;
810
import net.dv8tion.jda.api.events.message.MessageUpdateEvent;
9-
import net.dv8tion.jda.api.interactions.components.buttons.Button;
1011
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
1112
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
1213
import net.dv8tion.jda.internal.requests.CompletedRestAction;
@@ -126,7 +127,7 @@ private MessageCreateData createCodeReplyMessage(long originalMessageId,
126127
}
127128

128129
return new MessageCreateBuilder().setContent("Detected code, here are some useful tools:")
129-
.setActionRow(codeActionButtons)
130+
.setComponents(ActionRow.of(codeActionButtons))
130131
.build();
131132
}
132133

@@ -185,7 +186,7 @@ public void onButtonClick(ButtonInteractionEvent event, List<String> args) {
185186
// Apply the selected action
186187
return event.getHook()
187188
.editOriginalEmbeds(codeAction.apply(code))
188-
.setActionRow(createButtons(originalMessageId, codeAction));
189+
.setComponents(ActionRow.of(createButtons(originalMessageId, codeAction)));
189190
})
190191
.queue();
191192
}
@@ -232,7 +233,8 @@ public void onMessageUpdated(MessageUpdateEvent event) {
232233

233234
private Optional<CodeAction> getCurrentActionFromCodeReply(Message codeReplyMessage) {
234235
// The disabled action is the currently applied action
235-
return codeReplyMessage.getButtons()
236+
return codeReplyMessage.getComponentTree()
237+
.findAll(Button.class)
236238
.stream()
237239
.filter(Button::isDisabled)
238240
.map(Button::getLabel)

application/src/main/java/org/togetherjava/tjbot/features/componentids/ComponentIdGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.togetherjava.tjbot.features.componentids;
22

3+
import net.dv8tion.jda.api.components.buttons.Button;
4+
import net.dv8tion.jda.api.components.buttons.ButtonStyle;
35
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
46
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
5-
import net.dv8tion.jda.api.interactions.components.buttons.Button;
6-
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
77

88
import org.togetherjava.tjbot.features.SlashCommand;
99

application/src/main/java/org/togetherjava/tjbot/features/componentids/ComponentIdParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.togetherjava.tjbot.features.componentids;
22

3+
import net.dv8tion.jda.api.components.buttons.Button;
4+
import net.dv8tion.jda.api.components.buttons.ButtonStyle;
35
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
46
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
5-
import net.dv8tion.jda.api.interactions.components.buttons.Button;
6-
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;
77

88
import java.util.Optional;
99

application/src/main/java/org/togetherjava/tjbot/features/filesharing/FileSharingMessageListener.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package org.togetherjava.tjbot.features.filesharing;
22

3+
import net.dv8tion.jda.api.components.actionrow.ActionRow;
4+
import net.dv8tion.jda.api.components.buttons.Button;
35
import net.dv8tion.jda.api.entities.Member;
46
import net.dv8tion.jda.api.entities.Message;
57
import net.dv8tion.jda.api.entities.User;
68
import net.dv8tion.jda.api.entities.channel.ChannelType;
79
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
810
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
911
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
10-
import net.dv8tion.jda.api.interactions.components.buttons.Button;
1112
import org.kohsuke.github.GHGist;
1213
import org.kohsuke.github.GHGistBuilder;
1314
import org.kohsuke.github.GitHubBuilder;
@@ -190,7 +191,9 @@ private void sendResponse(MessageReceivedEvent event, String url, String gistId)
190191
componentIdInteractor.generateComponentId(message.getAuthor().getId(), gistId),
191192
"Delete");
192193

193-
message.reply(messageContent).setActionRow(gist, delete).queue();
194+
message.reply(messageContent)
195+
.setComponents(ActionRow.of(gist), ActionRow.of(delete))
196+
.queue();
194197
}
195198

196199
private boolean isHelpThread(MessageReceivedEvent event) {

0 commit comments

Comments
 (0)