Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Internal Command Structure #64

Merged
merged 8 commits into from
Nov 12, 2022
30 changes: 22 additions & 8 deletions src/main/java/xyz/dynxsty/dih4jda/DIH4JDA.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import xyz.dynxsty.dih4jda.config.DIH4JDAConfig;
import xyz.dynxsty.dih4jda.events.DIH4JDAEventListener;
import xyz.dynxsty.dih4jda.exceptions.DIH4JDAException;
import xyz.dynxsty.dih4jda.interactions.commands.ContextCommand;
import xyz.dynxsty.dih4jda.interactions.commands.RegistrationType;
import xyz.dynxsty.dih4jda.interactions.commands.SlashCommand;
import xyz.dynxsty.dih4jda.interactions.commands.application.BaseApplicationCommand;
import xyz.dynxsty.dih4jda.interactions.commands.application.ContextCommand;
import xyz.dynxsty.dih4jda.interactions.commands.application.RegistrationType;
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
import xyz.dynxsty.dih4jda.interactions.components.ButtonHandler;
import xyz.dynxsty.dih4jda.interactions.components.EntitySelectMenuHandler;
import xyz.dynxsty.dih4jda.interactions.components.ModalHandler;
Expand Down Expand Up @@ -38,9 +39,9 @@ public class DIH4JDA extends ListenerAdapter {

/**
* The default {@link RegistrationType} which is used for queuing new commands.
* This can be overridden using {@link xyz.dynxsty.dih4jda.interactions.commands.AbstractCommand#setRegistrationType(RegistrationType)}
* This can be overridden using {@link BaseApplicationCommand#setRegistrationType(RegistrationType)}
*/
public static RegistrationType defaultCommandType;
private static RegistrationType defaultRegistrationType = RegistrationType.GLOBAL;

// Component Handler
private static final Map<List<String>, ButtonHandler> buttonHandlers;
Expand All @@ -64,8 +65,7 @@ public class DIH4JDA extends ListenerAdapter {
*
* @param config The instance's configuration.
*/
protected DIH4JDA(DIH4JDAConfig config) throws DIH4JDAException {
if (defaultCommandType == null) defaultCommandType = RegistrationType.GUILD;
protected DIH4JDA(@Nonnull DIH4JDAConfig config) throws DIH4JDAException {
this.config = config;
listeners = new HashSet<>();
DIH4JDALogger.blockedLogTypes = config.getBlockedLogTypes();
Expand All @@ -91,6 +91,20 @@ public void onReady(@Nonnull ReadyEvent event) {
}
}

/**
* Sets the default {@link RegistrationType} for all Commands.
* This is set to {@link RegistrationType#GLOBAL} if not set otherwise.
*
* @param type The {@link RegistrationType}.
*/
public static void setDefaultRegistrationType(RegistrationType type) {
DIH4JDA.defaultRegistrationType = type;
}

public static RegistrationType getDefaultRegistrationType() {
return defaultRegistrationType;
}

/**
* Registers all Interactions and replaces the old ones.
* Please note that global commands may need up to an hour before they're fully registered.
Expand Down Expand Up @@ -157,7 +171,7 @@ public void addSlashCommands(SlashCommand... commands) {
*
* @param commands An array of commands to register.
*/
public void addContextCommands(ContextCommand... commands) {
public void addContextCommands(ContextCommand<?>... commands) {
handler.contextCommands.addAll(List.of(commands));
}

Expand Down
13 changes: 2 additions & 11 deletions src/main/java/xyz/dynxsty/dih4jda/DIH4JDABuilder.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package xyz.dynxsty.dih4jda;

import net.dv8tion.jda.annotations.ReplaceWith;
import net.dv8tion.jda.api.JDA;
import xyz.dynxsty.dih4jda.config.DIH4JDAConfig;
import xyz.dynxsty.dih4jda.exceptions.CommandNotRegisteredException;
import xyz.dynxsty.dih4jda.exceptions.DIH4JDAException;
import xyz.dynxsty.dih4jda.exceptions.InvalidPackageException;
import xyz.dynxsty.dih4jda.interactions.commands.RegistrationType;
import xyz.dynxsty.dih4jda.interactions.commands.application.RegistrationType;
import xyz.dynxsty.dih4jda.util.ClassWalker;
import xyz.dynxsty.dih4jda.util.ClasspathHelper;

Expand Down Expand Up @@ -111,16 +112,6 @@ private DIH4JDABuilder(@Nonnull JDA jda) {
return this;
}

/**
* Sets the default {@link RegistrationType} for all Commands.
*
* @param type The {@link RegistrationType}.
*/
public @Nonnull DIH4JDABuilder setDefaultCommandType(@Nonnull RegistrationType type) {
DIH4JDA.defaultCommandType = type;
return this;
}

/**
* Disables deletion of unknown/unused commands when using SmartQueue.
*/
Expand Down
101 changes: 50 additions & 51 deletions src/main/java/xyz/dynxsty/dih4jda/InteractionHandler.java

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions src/main/java/xyz/dynxsty/dih4jda/SmartQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import xyz.dynxsty.dih4jda.interactions.commands.ContextCommand;
import xyz.dynxsty.dih4jda.interactions.commands.SlashCommand;
import xyz.dynxsty.dih4jda.interactions.commands.application.ContextCommand;
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
import xyz.dynxsty.dih4jda.util.CommandUtils;
import xyz.dynxsty.dih4jda.util.Pair;

Expand Down Expand Up @@ -35,10 +35,10 @@
*/
public class SmartQueue {
private final Set<SlashCommand> slashCommands;
private final Set<ContextCommand> contextCommands;
private final Set<ContextCommand<?>> contextCommands;
private final boolean deleteUnknown;

protected SmartQueue(@Nonnull Set<SlashCommand> slashCommands, @Nonnull Set<ContextCommand> contextCommands, boolean deleteUnknown) {
protected SmartQueue(@Nonnull Set<SlashCommand> slashCommands, @Nonnull Set<ContextCommand<?>> contextCommands, boolean deleteUnknown) {
this.slashCommands = slashCommands;
this.contextCommands = contextCommands;
this.deleteUnknown = deleteUnknown;
Expand All @@ -51,7 +51,7 @@ protected SmartQueue(@Nonnull Set<SlashCommand> slashCommands, @Nonnull Set<Cont
* @return A {@link Pair} with the remaining {@link SlashCommandData} and {@link CommandData}.
* @since v1.5
*/
protected @Nonnull Pair<Set<SlashCommand>, Set<ContextCommand>> checkGlobal(@Nonnull JDA jda, @Nonnull List<Command> existing) {
protected @Nonnull Pair<Set<SlashCommand>, Set<ContextCommand<?>>> checkGlobal(@Nonnull JDA jda, @Nonnull List<Command> existing) {
if (!existing.isEmpty()) {
return removeDuplicates(jda, existing, null);
}
Expand All @@ -65,7 +65,7 @@ protected SmartQueue(@Nonnull Set<SlashCommand> slashCommands, @Nonnull Set<Cont
* @return A {@link Pair} with the remaining {@link SlashCommandData} and {@link CommandData}.
* @since v1.5
*/
protected @Nonnull Pair<Set<SlashCommand>, Set<ContextCommand>> checkGuild(@Nonnull Guild guild, @Nonnull List<Command> existing) {
protected @Nonnull Pair<Set<SlashCommand>, Set<ContextCommand<?>>> checkGuild(@Nonnull Guild guild, @Nonnull List<Command> existing) {
if (!existing.isEmpty()) {
return removeDuplicates(guild.getJDA(), existing, guild);
}
Expand All @@ -81,20 +81,20 @@ protected SmartQueue(@Nonnull Set<SlashCommand> slashCommands, @Nonnull Set<Cont
* @return A {@link Pair} with the remaining {@link SlashCommandData} & {@link CommandData}.
* @since v1.5
*/
private @Nonnull Pair<Set<SlashCommand>, Set<ContextCommand>> removeDuplicates(@Nonnull JDA jda, @Nonnull final List<Command> existing, @Nullable Guild guild) {
private @Nonnull Pair<Set<SlashCommand>, Set<ContextCommand<?>>> removeDuplicates(@Nonnull JDA jda, @Nonnull final List<Command> existing, @Nullable Guild guild) {
List<Command> commands = new ArrayList<>(existing);
boolean global = guild == null;
String prefix = String.format("[%s] ", global ? "Global" : guild.getName());
DIH4JDALogger.info(DIH4JDALogger.Type.SMART_QUEUE, prefix + "Found %s existing command(s)", existing.size());
// remove already-existing commands
commands.removeIf(cmd -> {
if (contextCommands.stream().anyMatch(data -> CommandUtils.equals(cmd, data.getCommandData(), global)) ||
slashCommands.stream().anyMatch(data -> CommandUtils.equals(cmd, data.getSlashCommandData(), global))) {
slashCommands.stream().anyMatch(data -> CommandUtils.equals(cmd, data.getCommandData(), global))) {
// check for command in blacklisted guilds
// this may be refactored soonTM, as its kinda clunky
if (!global) {
for (SlashCommand d : slashCommands) {
if (CommandUtils.equals(cmd, d.getSlashCommandData(), false)) {
if (CommandUtils.equals(cmd, d.getCommandData(), false)) {
if (d.getRequiredGuilds().getFirst() == null) {
return true;
} else {
Expand All @@ -121,7 +121,7 @@ protected SmartQueue(@Nonnull Set<SlashCommand> slashCommands, @Nonnull Set<Cont
return false;
});
contextCommands.removeIf(data -> existing.stream().anyMatch(p -> CommandUtils.equals(p, data.getCommandData(), global)));
slashCommands.removeIf(data -> existing.stream().anyMatch(p -> CommandUtils.equals(p, data.getSlashCommandData(), global)));
slashCommands.removeIf(data -> existing.stream().anyMatch(p -> CommandUtils.equals(p, data.getCommandData(), global)));
// remove unknown commands, if enabled
if (!commands.isEmpty()) {
for (Command command : commands) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
import net.dv8tion.jda.api.interactions.commands.CommandAutoCompleteInteraction;
import xyz.dynxsty.dih4jda.DIH4JDA;
import xyz.dynxsty.dih4jda.interactions.commands.AutoCompletable;
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;

/**
* An event that gets fired when an exception gets raised while handling an autocomplete interaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.CommandInteraction;
import xyz.dynxsty.dih4jda.DIH4JDA;
import xyz.dynxsty.dih4jda.interactions.commands.ContextCommand;
import xyz.dynxsty.dih4jda.interactions.commands.SlashCommand;
import xyz.dynxsty.dih4jda.interactions.commands.application.ContextCommand;
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;

/**
* An event that gets fired when an exception gets raised while executing any command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
import xyz.dynxsty.dih4jda.interactions.commands.AbstractCommand;
import xyz.dynxsty.dih4jda.interactions.commands.AutoCompletable;
import xyz.dynxsty.dih4jda.interactions.commands.ContextCommand;
import xyz.dynxsty.dih4jda.interactions.commands.SlashCommand;
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;
import xyz.dynxsty.dih4jda.interactions.commands.application.ContextCommand;
import xyz.dynxsty.dih4jda.interactions.commands.RestrictedCommand;
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;

/**
* An interface containing all events and their method that DIH4JDA can fire.
Expand Down Expand Up @@ -51,23 +51,23 @@ default void onModalException(ModalExceptionEvent event) {}
* An event that gets fired when the user, which invoked the command, does NOT have one of the required permissions.
*
* @param event The {@link InsufficientPermissionsEvent} that was fired.
* @see AbstractCommand#setRequiredPermissions(Permission...)
* @see RestrictedCommand#setRequiredPermissions(Permission...)
*/
default void onInsufficientPermissions(InsufficientPermissionsEvent event) {}

/**
* An event that gets fired when the user, which invoked the command, is NOT allowed to use this command.
*
* @param event The {@link InvalidUserEvent} that was fired.
* @see AbstractCommand#setRequiredUsers(Long...)
* @see RestrictedCommand#setRequiredUsers(Long...)
*/
default void onInvalidUser(InvalidUserEvent event) {}

/**
* An event that gets fired when the user, which invoked the command, does NOT have the required roles to use this command.
*
* @param event The {@link InvalidRoleEvent} that was fired.
* @see AbstractCommand#setRequiredRoles(Long...)
* @see RestrictedCommand#setRequiredRoles(Long...)
*/
default void onInvalidRole(InvalidRoleEvent event) {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.interactions.commands.CommandInteraction;
import xyz.dynxsty.dih4jda.DIH4JDA;
import xyz.dynxsty.dih4jda.interactions.commands.AbstractCommand;
import xyz.dynxsty.dih4jda.interactions.commands.RestrictedCommand;

import java.util.Set;

/**
* An event that gets fired when the user, which invoked the command, does NOT have one of the required permissions.
*
* @see AbstractCommand#setRequiredPermissions(Permission...)
* @see RestrictedCommand#setRequiredPermissions(Permission...)
*/
public class InsufficientPermissionsEvent extends GenericDIH4JDAEvent<CommandInteraction> {

Expand All @@ -23,7 +23,7 @@ public InsufficientPermissionsEvent(DIH4JDA dih4jda, CommandInteraction interact

/**
* @return An immutable {@link Set} of all "required" permissions for the executed command.
* @see AbstractCommand#setRequiredPermissions(Permission...)
* @see RestrictedCommand#setRequiredPermissions(Permission...)
*/
public Set<Permission> getPermissions() {
return permissions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import net.dv8tion.jda.api.interactions.commands.CommandInteraction;
import xyz.dynxsty.dih4jda.DIH4JDA;
import xyz.dynxsty.dih4jda.interactions.commands.AbstractCommand;
import xyz.dynxsty.dih4jda.interactions.commands.RestrictedCommand;

import java.util.Set;

/**
* An event that gets fired when the user, which invoked the command, does NOT have the required roles to use this command.
*
* @see AbstractCommand#setRequiredRoles(Long...)
* @see RestrictedCommand#setRequiredRoles(Long...)
*/
public class InvalidRoleEvent extends GenericDIH4JDAEvent<CommandInteraction> {

Expand All @@ -22,7 +22,7 @@ public InvalidRoleEvent(DIH4JDA dih4jda, CommandInteraction interaction, Set<Lon

/**
* @return An immutable {@link Set} of all "required" roles for the executed command.
* @see AbstractCommand#setRequiredRoles(Long...)
* @see RestrictedCommand#setRequiredRoles(Long...)
*/
public Set<Long> getRoleIds() {
return roleIds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import net.dv8tion.jda.api.interactions.commands.CommandInteraction;
import xyz.dynxsty.dih4jda.DIH4JDA;
import xyz.dynxsty.dih4jda.interactions.commands.AbstractCommand;
import xyz.dynxsty.dih4jda.interactions.commands.RestrictedCommand;

import java.util.Set;

/**
* An event that gets fired when the user, which invoked the command, is NOT allowed to use this command.
*
* @see AbstractCommand#setRequiredUsers(Long...)
* @see RestrictedCommand#setRequiredUsers(Long...)
*/
public class InvalidUserEvent extends GenericDIH4JDAEvent<CommandInteraction> {

Expand All @@ -22,7 +22,7 @@ public InvalidUserEvent(DIH4JDA dih4jda, CommandInteraction interaction, Set<Lon

/**
* @return An immutable {@link Set} of all "required" users for the executed command.
* @see AbstractCommand#setRequiredUsers(Long...)
* @see RestrictedCommand#setRequiredUsers(Long...)
*/
public Set<Long> getUserIds() {
return userIds;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.dynxsty.dih4jda.interactions.commands;
package xyz.dynxsty.dih4jda.interactions;

import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
Expand All @@ -13,7 +13,7 @@
* public class PingCommand extends SlashCommand implements AutoCompletable {
*
* public PingCommand() {
* setSlashCommandData(Commands.slash("ping", "Ping someone").addOption(OptionType.STRING, "user-id", "The user's id", true, true));
* setCommandData(Commands.slash("ping", "Ping someone").addOption(OptionType.STRING, "user-id", "The user's id", true, true));
* }
*
* @Override
Expand Down

This file was deleted.

Loading