Files
Bat/src/main/java/cc/fascinated/bat/command/BatCommand.java

123 lines
4.5 KiB
Java
Raw Normal View History

2024-06-24 13:56:01 +01:00
package cc.fascinated.bat.command;
2024-06-30 05:15:37 +01:00
import cc.fascinated.bat.features.Feature;
2024-07-04 08:47:32 -04:00
import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser;
import lombok.AccessLevel;
2024-06-24 13:56:01 +01:00
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
2024-07-04 08:47:32 -04:00
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.interactions.IntegrationType;
import net.dv8tion.jda.api.interactions.InteractionContextType;
2024-06-24 13:56:01 +01:00
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
2024-07-04 08:47:32 -04:00
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
2024-06-24 13:56:01 +01:00
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
2024-07-04 08:47:32 -04:00
import java.util.*;
2024-06-24 13:56:01 +01:00
/**
2024-07-04 08:47:32 -04:00
* @author Braydon
2024-06-24 13:56:01 +01:00
*/
2024-06-28 03:01:21 +01:00
@Getter
2024-07-04 08:47:32 -04:00
public abstract class BatCommand {
2024-06-27 16:01:27 +01:00
/**
2024-07-04 08:47:32 -04:00
* The info of this command.
2024-06-24 13:56:01 +01:00
*/
2024-07-04 08:47:32 -04:00
@NonNull private final InternalCommandInfo info;
2024-06-24 13:56:01 +01:00
/**
2024-07-04 08:47:32 -04:00
* The feature this command belongs to.
2024-06-24 13:56:01 +01:00
*/
2024-07-04 08:47:32 -04:00
@Setter private Feature feature;
2024-06-24 13:56:01 +01:00
/**
2024-07-04 08:47:32 -04:00
* The snowflake of this command, set when
* this command is registered with Discord.
2024-06-24 13:56:01 +01:00
*/
2024-07-04 08:47:32 -04:00
@Setter private long snowflake;
2024-06-24 13:56:01 +01:00
/**
2024-07-04 08:47:32 -04:00
* The sub commands of this command, if any.
2024-06-24 13:56:01 +01:00
*/
2024-07-04 08:47:32 -04:00
private final Map<String, BatCommand> subCommands = Collections.synchronizedMap(new HashMap<>());
2024-06-24 13:56:01 +01:00
2024-06-30 05:15:37 +01:00
/**
2024-07-04 08:47:32 -04:00
* The internal data for this command.
2024-06-30 05:15:37 +01:00
*/
2024-07-04 08:47:32 -04:00
@Setter(AccessLevel.PRIVATE) private CommandDataImpl commandData;
2024-06-30 05:15:37 +01:00
2024-06-27 21:12:31 +01:00
/**
2024-07-04 08:47:32 -04:00
* The internal subcommand data for this command.
2024-06-27 21:12:31 +01:00
*/
2024-07-04 08:47:32 -04:00
@Setter(AccessLevel.PRIVATE) private SubcommandData subcommandData;
2024-06-27 16:01:27 +01:00
2024-06-27 19:36:52 +01:00
public BatCommand() {
2024-07-04 08:47:32 -04:00
if (!getClass().isAnnotationPresent(CommandInfo.class)) {
throw new IllegalStateException("Missing @CommandInfo annotation in " + getClass().getSimpleName());
}
info = new InternalCommandInfo(getClass().getAnnotation(CommandInfo.class));
2024-07-04 08:47:32 -04:00
List<IntegrationType> integrationTypes = new ArrayList<>(Collections.singletonList(IntegrationType.GUILD_INSTALL));
if (info.isUserInstall()) {
integrationTypes.add(IntegrationType.USER_INSTALL);
}
commandData = new CommandDataImpl(info.getName(), info.getDescription())
.setContexts(InteractionContextType.ALL)
2024-07-04 08:47:32 -04:00
.setIntegrationTypes(integrationTypes)
2024-07-04 15:20:26 +01:00
.setGuildOnly(!getInfo().isUserInstall() && getInfo().isGuildOnly());
2024-06-24 13:56:01 +01:00
}
/**
2024-07-04 08:47:32 -04:00
* Fired when this command is executed.
2024-06-24 13:56:01 +01:00
*
2024-07-04 08:47:32 -04:00
* @param guild the guild the command was executed in, if any
* @param user the user who executed the command
* @param channel the channel the command was executed in
* @param member the member who executed the command, null if not a guild
* @param event the event that invoked this command
2024-06-24 13:56:01 +01:00
*/
2024-07-04 08:47:32 -04:00
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction event) { }
/**
2024-07-04 08:47:32 -04:00
* Register the given sub commands.
*
2024-07-04 08:47:32 -04:00
* @param commands the commands to register
*/
2024-07-04 08:47:32 -04:00
protected final void addSubCommands(@NonNull BatCommand... commands) {
for (BatCommand command : commands) {
// Copy info from the parent command
if (command.getInfo().getCategory() != info.getCategory()) {
command.getInfo().setCategory(info.getCategory());
}
if (command.getInfo().getPermissions().length == 0) {
command.getInfo().setPermissions(info.getPermissions());
}
if (command.getInfo().isGuildOnly() != info.isGuildOnly()) {
command.getInfo().setGuildOnly(info.isGuildOnly());
}
if (command.getInfo().isBotOwnerOnly() != info.isBotOwnerOnly()) {
command.getInfo().setBotOwnerOnly(info.isBotOwnerOnly());
}
command.setSubcommandData(new SubcommandData(command.getInfo().getName(), command.getInfo().getDescription()));
2024-07-04 08:51:27 -04:00
for (OptionData option : command.getCommandData().getOptions()) {
command.getSubcommandData().addOptions(option);
}
2024-07-04 08:47:32 -04:00
commandData.addSubcommands(command.getSubcommandData());
subCommands.put(command.getInfo().getName(), command);
}
2024-06-24 13:56:01 +01:00
}
/**
2024-07-04 08:47:32 -04:00
* Add the given options
* to this command.
2024-06-24 13:56:01 +01:00
*
2024-07-04 08:47:32 -04:00
* @param options the options to add
2024-06-24 13:56:01 +01:00
*/
2024-07-04 08:47:32 -04:00
protected final void addOptions(OptionData... options) {
2024-07-04 08:51:27 -04:00
commandData.addOptions(options);
2024-06-24 13:56:01 +01:00
}
2024-07-04 08:47:32 -04:00
}