Skip to content

Commit

Permalink
Merge pull request #47 from freya022/update/jda-5-alpha-22
Browse files Browse the repository at this point in the history
Update to JDA 5 alpha 22
  • Loading branch information
freya022 authored Oct 26, 2022
2 parents e056108 + 5437f7b commit 94c170b
Show file tree
Hide file tree
Showing 33 changed files with 514 additions and 99 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![](https://img.shields.io/maven-central/v/io.github.freya022/BotCommands)](#getting-the-library)
[![](https://img.shields.io/badge/JDA%20Version-5.0.0--alpha.21-important)](https://github.com/DV8FromTheWorld/JDA/releases)
[![](https://img.shields.io/badge/JDA%20Version-5.0.0--alpha.22-important)](https://github.com/DV8FromTheWorld/JDA/releases)
[![image](https://discord.com/api/guilds/848502702731165738/embed.png?style=shield)](https://discord.gg/frpCcQfvTz)
[![image](https://img.shields.io/badge/Wiki-Home-blue)](https://freya022.github.io/BotCommands-Wiki/)

Expand Down
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
<release>17</release>
<additionalJOptions>
<additionalJOption>-Xdoclint:-missing</additionalJOption> <!-- No need to print "no comment" warnings -->
<additionalJOption>--enable-preview</additionalJOption>
</additionalJOptions>
<links>
<link>https://ci.dv8tion.net/job/JDA5/javadoc</link>
Expand Down Expand Up @@ -331,7 +330,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.0-alpha.21</version>
<version>5.0.0-alpha.22</version>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.EntitySelectInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

Expand All @@ -25,7 +26,9 @@ public final void onException(BContext context, Event event, Throwable throwable
handle(context, e, throwable);
} else if (event instanceof ButtonInteractionEvent e) {
handle(context, e, throwable);
} else if (event instanceof SelectMenuInteractionEvent e) {
} else if (event instanceof StringSelectInteractionEvent e) {
handle(context, e, throwable);
} else if (event instanceof EntitySelectInteractionEvent e) {
handle(context, e, throwable);
} else if (event instanceof ModalInteractionEvent e) {
handle(context, e, throwable);
Expand All @@ -48,5 +51,7 @@ public void handle(BContext context, ModalInteractionEvent event, Throwable thro

public void handle(BContext context, ButtonInteractionEvent event, Throwable throwable) {}

public void handle(BContext context, SelectMenuInteractionEvent event, Throwable throwable) {}
public void handle(BContext context, StringSelectInteractionEvent event, Throwable throwable) {}

public void handle(BContext context, EntitySelectInteractionEvent event, Throwable throwable) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
import com.freya02.botcommands.api.ExceptionHandler;
import com.freya02.botcommands.api.Logging;
import com.freya02.botcommands.api.components.event.ButtonEvent;
import com.freya02.botcommands.api.components.event.SelectionEvent;
import com.freya02.botcommands.api.components.event.EntitySelectionEvent;
import com.freya02.botcommands.api.components.event.StringSelectionEvent;
import com.freya02.botcommands.api.parameters.ComponentParameterResolver;
import com.freya02.botcommands.internal.BContextImpl;
import com.freya02.botcommands.internal.RunnableEx;
import com.freya02.botcommands.internal.application.CommandParameter;
import com.freya02.botcommands.internal.components.ComponentDescriptor;
import com.freya02.botcommands.internal.utils.Utils;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent;
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.*;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.hooks.SubscribeEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -67,7 +68,7 @@ public ComponentListener(BContextImpl context, Map<String, ComponentDescriptor>
@SubscribeEvent
@Override
public void onGenericComponentInteractionCreate(@NotNull GenericComponentInteractionCreateEvent event) {
if (!(event instanceof ButtonInteractionEvent) && !(event instanceof SelectMenuInteractionEvent)) return;
if (!(event instanceof ButtonInteractionEvent) && !(event instanceof GenericSelectMenuInteractionEvent<?, ?>)) return;

runHandler(() -> handleComponentInteraction(event), event);
}
Expand Down Expand Up @@ -99,8 +100,8 @@ private void handleComponentInteraction(@NotNull GenericComponentInteractionCrea
return;
}

if ((idType == ComponentType.PERSISTENT_SELECTION_MENU || idType == ComponentType.LAMBDA_SELECTION_MENU) && !(event instanceof SelectMenuInteractionEvent)) {
LOGGER.error("Received a selection menu id type but event is not a SelectMenuInteractionEvent");
if ((idType == ComponentType.PERSISTENT_SELECTION_MENU || idType == ComponentType.LAMBDA_SELECTION_MENU) && !(event instanceof GenericSelectMenuInteractionEvent<?, ?>)) {
LOGGER.error("Received a selection menu id type but event is not a GenericSelectMenuInteractionEvent");

return;
}
Expand All @@ -127,12 +128,12 @@ private void handleComponentInteraction(@NotNull GenericComponentInteractionCrea
selectionMenuMap,
data.getHandlerName(),
data.getArgs(),
descriptor -> new SelectionEvent(descriptor.getMethod(), context, (SelectMenuInteractionEvent) event)),
descriptor -> transformSelectEvent(descriptor.getMethod(), context, event)),
event));
case LAMBDA_SELECTION_MENU -> componentManager.handleLambdaSelectMenu(event,
fetchResult,
e -> onError(event, e),
data -> runCallback(() -> data.getConsumer().accept(new SelectionEvent(null, context, (SelectMenuInteractionEvent) event)), event));
data -> runCallback(() -> data.getConsumer().accept(transformSelectEvent(null, context, event)), event));
default -> throw new IllegalArgumentException("Unknown id type: " + idType.name());
}
}
Expand Down Expand Up @@ -198,6 +199,17 @@ private void runCallback(RunnableEx code, @NotNull GenericComponentInteractionCr
});
}

@NotNull
private GenericSelectMenuInteractionEvent<?, ?> transformSelectEvent(@Nullable Method method, BContextImpl context, GenericComponentInteractionCreateEvent event) {
if (event instanceof StringSelectInteractionEvent stringSelectEvent) {
return new StringSelectionEvent(method, context, stringSelectEvent);
} else if (event instanceof EntitySelectInteractionEvent entitySelectEvent) {
return new EntitySelectionEvent(null, context, entitySelectEvent);
} else {
throw new IllegalArgumentException("Invalid select menu type: " + event.getClass().getName());
}
}

private void handlePersistentComponent(GenericComponentInteractionCreateEvent event,
Map<String, ComponentDescriptor> map,
String handlerName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.freya02.botcommands.api.components;

import com.freya02.botcommands.api.components.builder.LambdaButtonBuilder;
import com.freya02.botcommands.api.components.builder.LambdaSelectionMenuBuilder;
import com.freya02.botcommands.api.components.builder.PersistentButtonBuilder;
import com.freya02.botcommands.api.components.builder.PersistentSelectionMenuBuilder;
import com.freya02.botcommands.api.components.builder.buttons.LambdaButtonBuilder;
import com.freya02.botcommands.api.components.builder.buttons.PersistentButtonBuilder;
import com.freya02.botcommands.api.components.builder.selects.LambdaSelectionMenuBuilder;
import com.freya02.botcommands.api.components.builder.selects.PersistentSelectionMenuBuilder;
import com.freya02.botcommands.internal.components.data.LambdaButtonData;
import com.freya02.botcommands.internal.components.data.LambdaSelectionMenuData;
import com.freya02.botcommands.internal.components.data.PersistentButtonData;
import com.freya02.botcommands.internal.components.data.PersistentSelectionMenuData;
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent;
import net.dv8tion.jda.api.events.interaction.component.GenericSelectMenuInteractionEvent;
import net.dv8tion.jda.api.interactions.components.ActionComponent;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import org.jetbrains.annotations.NotNull;
Expand All @@ -28,7 +29,7 @@ public interface ComponentManager {

void handleLambdaButton(GenericComponentInteractionCreateEvent event, FetchResult fetchResult, Consumer<ComponentErrorReason> onError, Consumer<LambdaButtonData> dataConsumer);

void handleLambdaSelectMenu(GenericComponentInteractionCreateEvent event, FetchResult fetchResult, Consumer<ComponentErrorReason> onError, Consumer<LambdaSelectionMenuData> dataConsumer);
<E extends GenericSelectMenuInteractionEvent<?, ?>> void handleLambdaSelectMenu(GenericComponentInteractionCreateEvent event, FetchResult fetchResult, Consumer<ComponentErrorReason> onError, Consumer<LambdaSelectionMenuData<E>> dataConsumer);

void handlePersistentButton(GenericComponentInteractionCreateEvent event, FetchResult fetchResult, Consumer<ComponentErrorReason> onError, Consumer<PersistentButtonData> dataConsumer);

Expand All @@ -38,13 +39,13 @@ public interface ComponentManager {
String putLambdaButton(LambdaButtonBuilder builder);

@NotNull
String putLambdaSelectMenu(LambdaSelectionMenuBuilder builder);
<E extends GenericSelectMenuInteractionEvent<?, ?>> String putLambdaSelectMenu(LambdaSelectionMenuBuilder<?, E> builder);

@NotNull
String putPersistentButton(PersistentButtonBuilder builder);

@NotNull
String putPersistentSelectMenu(PersistentSelectionMenuBuilder builder);
<T extends PersistentSelectionMenuBuilder<T>> String putPersistentSelectMenu(T builder);

void registerGroup(Collection<String> builders);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
import com.freya02.botcommands.api.Logging;
import com.freya02.botcommands.api.components.annotations.JDAButtonListener;
import com.freya02.botcommands.api.components.annotations.JDASelectionMenuListener;
import com.freya02.botcommands.api.components.builder.*;
import com.freya02.botcommands.api.components.builder.ComponentBuilder;
import com.freya02.botcommands.api.components.builder.buttons.LambdaButtonBuilder;
import com.freya02.botcommands.api.components.builder.buttons.PersistentButtonBuilder;
import com.freya02.botcommands.api.components.builder.selects.LambdaEntitySelectionMenuBuilder;
import com.freya02.botcommands.api.components.builder.selects.LambdaStringSelectionMenuBuilder;
import com.freya02.botcommands.api.components.builder.selects.PersistentEntitySelectionMenuBuilder;
import com.freya02.botcommands.api.components.builder.selects.PersistentStringSelectionMenuBuilder;
import com.freya02.botcommands.api.components.event.ButtonEvent;
import com.freya02.botcommands.api.components.event.SelectionEvent;
import com.freya02.botcommands.api.components.event.EntitySelectionEvent;
import com.freya02.botcommands.api.components.event.StringSelectionEvent;
import com.freya02.botcommands.internal.utils.Utils;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.Channel;
Expand Down Expand Up @@ -345,18 +352,47 @@ public static PersistentButtonBuilder button(@NotNull ButtonStyle style, @NotNul
}

/**
* Creates a new selection menu with a lambda {@link SelectionEvent} handler<br>
* Creates a new selection menu with a lambda {@link StringSelectionEvent} handler<br>
* <b>These selection menus are not persistent and will not exist anymore once the bot restarts</b>
*
* @param consumer The {@link SelectionEvent} handler, fired after all conditions are met (defined when creating the selection menu)
* @param consumer The {@link StringSelectionEvent} handler, fired after all conditions are met (defined when creating the selection menu)
* @return A selection menu builder to configure behavior
*/
@NotNull
@Contract("_ -> new")
public static LambdaSelectionMenuBuilder selectionMenu(@NotNull SelectionConsumer consumer) {
public static LambdaStringSelectionMenuBuilder stringSelectionMenu(@NotNull StringSelectionConsumer consumer) {
checkCapturedVars(consumer);

return new LambdaSelectionMenuBuilder(context, consumer);
return new LambdaStringSelectionMenuBuilder(context, consumer);
}

/**
* Creates a new selection menu with a lambda {@link EntitySelectionEvent} handler<br>
* <b>These selection menus are not persistent and will not exist anymore once the bot restarts</b>
*
* @param consumer The {@link EntitySelectionEvent} handler, fired after all conditions are met (defined when creating the selection menu)
* @return A selection menu builder to configure behavior
*/
@NotNull
@Contract("_ -> new")
public static LambdaEntitySelectionMenuBuilder entitySelectionMenu(@NotNull EntitySelectionConsumer consumer) {
checkCapturedVars(consumer);

return new LambdaEntitySelectionMenuBuilder(context, consumer);
}

/**
* Creates a new selection menu with the given handler name, which must exist as one registered with {@link JDASelectionMenuListener}, and the given arguments<br>
* <b>These selection menus <i>are</i> persistent and will still exist even if the bot restarts</b>
*
* @param handlerName The name of this component's handler
* @param args The args to pass to this component's handler method
* @return A selection menu builder to configure behavior
*/
@NotNull
@Contract("_, _ -> new")
public static PersistentStringSelectionMenuBuilder stringSelectionMenu(@NotNull String handlerName, @NotNull Object @NotNull ... args) {
return new PersistentStringSelectionMenuBuilder(context, handlerName, processArgs(args));
}

/**
Expand All @@ -369,7 +405,7 @@ public static LambdaSelectionMenuBuilder selectionMenu(@NotNull SelectionConsume
*/
@NotNull
@Contract("_, _ -> new")
public static PersistentSelectionMenuBuilder selectionMenu(@NotNull String handlerName, @NotNull Object @NotNull ... args) {
return new PersistentSelectionMenuBuilder(context, handlerName, processArgs(args));
public static PersistentEntitySelectionMenuBuilder entitySelectionMenu(@NotNull String handlerName, @NotNull Object @NotNull ... args) {
return new PersistentEntitySelectionMenuBuilder(context, handlerName, processArgs(args));
}
}
Loading

0 comments on commit 94c170b

Please sign in to comment.