Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.togetherjava.tjbot.commands.Features;
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
import org.togetherjava.tjbot.commands.system.BotCore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.Contract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.togetherjava.tjbot.commands.BotCommand;
import org.togetherjava.tjbot.commands.CommandVisibility;
import org.togetherjava.tjbot.commands.system.CommandProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;

import java.util.List;
Expand All @@ -25,16 +24,6 @@
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
*/
public interface BotCommand extends UserInteractor {

/**
* Gets the type of this command.
* <p>
* After registration of the command, the type must not change anymore.
*
* @return the type of the command
*/
Command.Type getType();

/**
* Gets the visibility of this command.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import org.jetbrains.annotations.Contract;
import org.togetherjava.tjbot.commands.componentids.ComponentId;

import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator;
import org.togetherjava.tjbot.commands.componentids.ComponentIdInteractor;
import org.togetherjava.tjbot.commands.componentids.Lifespan;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

Expand All @@ -23,26 +23,23 @@
* {@link #onSelectionMenu(SelectMenuInteractionEvent, List)} can be overridden if desired. The
* default implementation is empty, the adapter will not react to such events.
* <p>
* <p>
* The adapter manages some getters for you, you've to create the {@link CommandData} yourself. See
* {@link #BotCommandAdapter(CommandData, CommandVisibility)}} for more info on that. Minimal
* modifications can be done on the {@link CommandData} returned by {@link #getData()}.
* <p>
* <p>
* If implementations want to add buttons or selection menus, it is highly advised to use component
* IDs generated by {@link #generateComponentId(String...)}, which will automatically create IDs
* that are valid per {@link SlashCommand#onSlashCommand(SlashCommandInteractionEvent)}.
* <p>
* <p>
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
* Registration of commands can be done in {@link Features}.
*/
public abstract class BotCommandAdapter implements BotCommand {
private final String name;
private final Command.Type type;
private final UserInteractionType interactionType;
private final CommandVisibility visibility;
private final CommandData data;
private ComponentIdGenerator componentIdGenerator;
private final ComponentIdInteractor componentIdInteractor;

/**
* Creates a new adapter with the given data.
Expand All @@ -54,8 +51,19 @@ protected BotCommandAdapter(CommandData data, CommandVisibility visibility) {
this.data = data.setGuildOnly(visibility == CommandVisibility.GUILD);
this.visibility = Objects.requireNonNull(visibility, "The visibility shouldn't be null");

this.name = data.getName();
this.type = data.getType();
name = data.getName();
interactionType = commandTypeToInteractionType(data.getType());
componentIdInteractor = new ComponentIdInteractor(interactionType, name);
}

private static UserInteractionType commandTypeToInteractionType(Command.Type type) {
return switch (type) {
case SLASH -> UserInteractionType.SLASH_COMMAND;
case USER -> UserInteractionType.USER_CONTEXT_COMMAND;
case MESSAGE -> UserInteractionType.MESSAGE_CONTEXT_COMMAND;
case UNKNOWN -> throw new IllegalArgumentException(
"Can not infer the type of user interaction for this command, it is unknown.");
};
}

@Override
Expand All @@ -64,8 +72,8 @@ public final String getName() {
}

@Override
public Command.Type getType() {
return type;
public final UserInteractionType getInteractionType() {
return interactionType;
}

@Override
Expand All @@ -81,8 +89,7 @@ public CommandData getData() {
@Override
@Contract(mutates = "this")
public final void acceptComponentIdGenerator(ComponentIdGenerator generator) {
componentIdGenerator =
Objects.requireNonNull(generator, "The given generator cannot be null");
componentIdInteractor.acceptComponentIdGenerator(generator);
}

@SuppressWarnings("NoopMethodInAbstractClass")
Expand Down Expand Up @@ -113,7 +120,7 @@ public void onSelectionMenu(SelectMenuInteractionEvent event, List<String> args)
*/
@SuppressWarnings("OverloadedVarargsMethod")
protected final String generateComponentId(String... args) {
return generateComponentId(Lifespan.REGULAR, args);
return componentIdInteractor.generateComponentId(args);
}

/**
Expand All @@ -130,8 +137,6 @@ protected final String generateComponentId(String... args) {
*/
@SuppressWarnings({"OverloadedVarargsMethod", "WeakerAccess"})
protected final String generateComponentId(Lifespan lifespan, String... args) {
return componentIdGenerator
.generate(new ComponentId(UserInteractorPrefix.getPrefixedNameFromInstance(this),
Arrays.asList(args)), lifespan);
return componentIdInteractor.generateComponentId(lifespan, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.togetherjava.tjbot.commands;

import net.dv8tion.jda.api.JDA;

import org.togetherjava.tjbot.commands.basic.PingCommand;
import org.togetherjava.tjbot.commands.basic.RoleSelectCommand;
import org.togetherjava.tjbot.commands.basic.SuggestionsUpDownVoter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;

import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator;

import java.util.List;
Expand All @@ -21,7 +22,6 @@
* {@link BotCommandAdapter} available that implemented most methods already. A new command can then
* be registered by adding it to {@link Features}.
* <p>
* <p>
* Context commands can either be visible globally in Discord or just to specific guilds. Minor
* adjustments can be made via {@link CommandData}, which is then to be returned by
* {@link #getData()} where the system will then pick it up from.
Expand All @@ -31,11 +31,9 @@
* ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus
* ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered.
* <p>
* <p>
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
*/
public interface MessageContextCommand extends BotCommand {

/**
* Triggered by the core system when a message context-command corresponding to this
* implementation (based on {@link #getData()}) has been triggered.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;

import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator;

import java.util.List;
Expand Down Expand Up @@ -39,7 +40,6 @@
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
*/
public interface SlashCommand extends BotCommand {

/**
* Gets the description of the command.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle;

import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator;

import java.util.List;
Expand All @@ -22,7 +23,6 @@
* {@link BotCommandAdapter} available that implemented most methods already. A new command can then
* be registered by adding it to {@link Features}.
* <p>
* <p>
* Context commands can either be visible globally in Discord or just to specific guilds. Minor
* adjustments can be made via {@link CommandData}, which is then to be returned by
* {@link #getData()} where the system will then pick it up from.
Expand All @@ -32,11 +32,9 @@
* ({@link #onButtonClick(ButtonInteractionEvent, List)}) or menus
* ({@link #onSelectionMenu(SelectMenuInteractionEvent, List)}) have been triggered.
* <p>
* <p>
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
*/
public interface UserContextCommand extends BotCommand {

/**
* Triggered by the core system when a user context-command corresponding to this implementation
* (based on {@link #getData()}) has been triggered.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.togetherjava.tjbot.commands;

/**
* Different types of user interactions.
* <p>
* Also allows user interactors of different type to have the same name.
*/
public enum UserInteractionType {
/**
* User types a command leaded by a slash, such as {@code /ping}.
*/
SLASH_COMMAND(SlashCommand.class, "s-"),
/**
* User right-clicks a message and selects a command.
*/
MESSAGE_CONTEXT_COMMAND(MessageContextCommand.class, "mc-"),
/**
* User right-clicks a user and selects a command.
*/
USER_CONTEXT_COMMAND(UserContextCommand.class, "uc-"),
/**
* Other types of interactions, for example a routine that offers buttons to click on.
*/
OTHER(UserInteractor.class, "");

private final Class<? extends UserInteractor> classType;
private final String prefix;

UserInteractionType(Class<? extends UserInteractor> classType, final String prefix) {
this.classType = classType;
this.prefix = prefix;
}

/**
* The prefix for the command
*
* @return the command's prefix
*/
public String getPrefix() {
return prefix;
}

/**
* Returns the name, attached with the prefix in front of it.
*
* @param name the name
* @return the name, with the prefix in front of it.
*/
public String getPrefixedName(String name) {
return prefix + name;
}

/**
* The class type that should receive the prefix
*
* @return a {@link Class} instance of the type
*/
public Class<? extends UserInteractor> getClassType() {
return classType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;

import org.togetherjava.tjbot.commands.componentids.ComponentId;
import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator;
import org.togetherjava.tjbot.commands.componentids.Lifespan;
Expand All @@ -15,21 +16,31 @@
* <p>
* An interactor can react to button clicks and selection menu actions. This is done based on the
* given {@link #getName()}, because of this names have to be unique. But, names can be complicated
* if their type is different, all the types can be seen in {@link UserInteractorPrefix}
* if their type is different, all the types can be seen in {@link UserInteractionType}
*/
public interface UserInteractor extends Feature {

/**
* Gets the name of the interactor.
* <p>
* You cannot start the name with any of the prefixes found in {@link UserInteractorPrefix}
* You cannot start the name with any of the prefixes found in {@link UserInteractionType}
* <p>
* After registration of the interactor, the name must not change anymore.
*
* @return the name of the interactor
*/
String getName();


/**
* Gets the type of interactors this interactor allows.
* <p>
* After registration of the interactor, the type must not change anymore.
*
* @return the type of the interaction allowed by this interactor
*/
UserInteractionType getInteractionType();

/**
* Triggered by the core system when a button corresponding to this implementation (based on
* {@link #getName()}) has been clicked.
Expand Down
Loading