Skip to content

Commit

Permalink
Add "Slash" to the start of command classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlmao committed Feb 16, 2022
1 parent fa823fe commit c23636c
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 79 deletions.
18 changes: 9 additions & 9 deletions src/main/java/dev/negativekb/api/DiscordBot.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.negativekb.api;

import dev.negativekb.api.commands.Command;
import dev.negativekb.api.commands.SlashCommand;
import dev.negativekb.api.commands.CommandCooldownManager;
import dev.negativekb.api.commands.internal.CommandMap;
import dev.negativekb.api.listener.SlashCommandListener;
Expand Down Expand Up @@ -31,21 +31,21 @@ public void initCommandMap(@NotNull JDABuilder builder) {
}

/**
* Register a {@link Command} as a global command
* @param command {@link Command} instance
* Register a {@link SlashCommand} as a global command
* @param command {@link SlashCommand} instance
* @apiNote This may take up to an hour for Discord to register it!
*/
public void registerGlobalCommand(@NotNull Command command) {
public void registerGlobalCommand(@NotNull SlashCommand command) {
commandMap.registerGlobalCommand(command.getName(), command);
}

/**
* Register a {@link Command} as a server command
* Register a {@link SlashCommand} as a server command
* @param serverID {@link Guild} ID
* @param command {@link Command} instance
* @param command {@link SlashCommand} instance
* @apiNote This should register almost instantly!
*/
public void registerServerCommand(@NotNull String serverID, @NotNull Command command) {
public void registerServerCommand(@NotNull String serverID, @NotNull SlashCommand command) {
commandMap.registerServerCommand(serverID, command.getName(), command);
}

Expand All @@ -57,7 +57,7 @@ public void registerServerCommand(@NotNull String serverID, @NotNull Command com
@SuppressWarnings("all")
public void initializeCommands(@NotNull JDA jda) {
// Global Commands
Collection<Command> globalCommands = commandMap.getGlobalCommands();
Collection<SlashCommand> globalCommands = commandMap.getGlobalCommands();
CommandListUpdateAction commands = jda.updateCommands();

globalCommands.forEach(command -> {
Expand Down Expand Up @@ -114,7 +114,7 @@ public void initializeCommands(@NotNull JDA jda) {
assert guild != null;
CommandListUpdateAction guildCommands = guild.updateCommands();

Collection<Command> serverCommands = serverEntry.getValue();
Collection<SlashCommand> serverCommands = serverEntry.getValue();
serverCommands.forEach(command -> {
if (!command.getAliases().isEmpty()) {
command.getAliases().forEach(name -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

public interface CommandCooldownManager {

void addCooldown(@NotNull String id, @NotNull Command command, long duration);
void addCooldown(@NotNull String id, @NotNull SlashCommand command, long duration);

void addCooldown(@NotNull String id, @NotNull SubCommand subCommand, long duration);
void addCooldown(@NotNull String id, @NotNull SlashSubCommand subCommand, long duration);

boolean checkCooldown(@NotNull String id, @NotNull Command command);
boolean checkCooldown(@NotNull String id, @NotNull SlashCommand command);

boolean checkCooldown(@NotNull String id, @NotNull SubCommand subCommand);
boolean checkCooldown(@NotNull String id, @NotNull SlashSubCommand subCommand);

long getCooldown(@NotNull String id, @NotNull Command command);
long getCooldown(@NotNull String id, @NotNull SlashCommand command);

long getCooldown(@NotNull String id, @NotNull SubCommand command);
long getCooldown(@NotNull String id, @NotNull SlashSubCommand command);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.function.Consumer;

@Getter
public abstract class Command {
public abstract class SlashCommand {

private final String name;
private final String description;
Expand All @@ -26,18 +26,18 @@ public abstract class Command {
@Setter
private Consumer<CommandData> data;
@Getter
private final ArrayList<SubCommand> subCommands = new ArrayList<>();
private final ArrayList<SlashSubCommand> subCommands = new ArrayList<>();
@Setter
private int cooldownInSeconds = 0;
private final CommandCooldownManager cooldownManager;

public Command() {
if (!getClass().isAnnotationPresent(CommandInfo.class))
public SlashCommand() {
if (!getClass().isAnnotationPresent(SlashCommandInfo.class))
throw new InvalidCommandInfoException();

cooldownManager = CommandCooldownManagerProvider.getInstance();

CommandInfo info = getClass().getAnnotation(CommandInfo.class);
SlashCommandInfo info = getClass().getAnnotation(SlashCommandInfo.class);
this.name = info.name();
this.description = info.description();

Expand All @@ -54,7 +54,7 @@ public void runCommand(SlashCommandEvent event) {
boolean hasSubCommand = Optional.ofNullable(event.getSubcommandName()).isPresent();
if (hasSubCommand && !subCommands.isEmpty()) {
String subcommandName = event.getSubcommandName();
Optional<SubCommand> first = subCommands.stream()
Optional<SlashSubCommand> first = subCommands.stream()
.filter(subCommand -> subCommand.getName().equalsIgnoreCase(subcommandName))
.findFirst();

Expand Down Expand Up @@ -84,7 +84,7 @@ public void runCommand(SlashCommandEvent event) {

public abstract void onCommand(SlashCommandEvent event);

public void addSubCommands(SubCommand... subCommands) {
public void addSubCommands(SlashSubCommand... subCommands) {
this.subCommands.addAll(Arrays.asList(subCommands));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CommandInfo {
public @interface SlashCommandInfo {

String name();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.function.Consumer;

@Getter
public abstract class SubCommand {
public abstract class SlashSubCommand {

private final String name;
private final String description;
Expand All @@ -28,13 +28,13 @@ public abstract class SubCommand {
private int cooldownInSeconds = 0;
private final CommandCooldownManager cooldownManager;

public SubCommand() {
if (!getClass().isAnnotationPresent(CommandInfo.class))
public SlashSubCommand() {
if (!getClass().isAnnotationPresent(SlashCommandInfo.class))
throw new InvalidCommandInfoException();

cooldownManager = CommandCooldownManagerProvider.getInstance();

CommandInfo info = getClass().getAnnotation(CommandInfo.class);
SlashCommandInfo info = getClass().getAnnotation(SlashCommandInfo.class);
this.name = info.name();
this.description = info.description();
List<String> a = new ArrayList<>(Arrays.asList(info.aliases()));
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/dev/negativekb/api/commands/internal/CommandMap.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.negativekb.api.commands.internal;

import dev.negativekb.api.commands.Command;
import dev.negativekb.api.commands.SlashCommand;
import net.dv8tion.jda.api.entities.Guild;
import org.jetbrains.annotations.NotNull;

Expand All @@ -11,31 +11,31 @@
* Command Map Module
*
* This is responsible for handling and registering all
* commands using the {@link Command} abstract class
* commands using the {@link SlashCommand} abstract class
*/
public interface CommandMap {

/**
* Register a Global Command
* @param name {@link Command} Name
* @param command {@link Command} instance
* @param name {@link SlashCommand} Name
* @param command {@link SlashCommand} instance
*/
void registerGlobalCommand(@NotNull String name, @NotNull Command command);
void registerGlobalCommand(@NotNull String name, @NotNull SlashCommand command);

/**
* Register a {@link Command} to a {@link Guild}
* Register a {@link SlashCommand} to a {@link Guild}
* @param serverID ID ({@link String}) of the {@link Guild}
* @param name {@link Command} name
* @param command {@link Command} instance
* @param name {@link SlashCommand} name
* @param command {@link SlashCommand} instance
*/
void registerServerCommand(@NotNull String serverID, @NotNull String name, @NotNull Command command);
void registerServerCommand(@NotNull String serverID, @NotNull String name, @NotNull SlashCommand command);

@NotNull
Collection<Command> getGlobalCommands();
Collection<SlashCommand> getGlobalCommands();

@NotNull
Collection<Command> getServerCommands(@NotNull String serverID);
Collection<SlashCommand> getServerCommands(@NotNull String serverID);

HashMap<String, Collection<Command>> getAllServerCommands();
HashMap<String, Collection<SlashCommand>> getAllServerCommands();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.negativekb.api.listener;

import dev.negativekb.api.commands.Command;
import dev.negativekb.api.commands.SlashCommand;
import dev.negativekb.api.commands.internal.CommandMap;
import lombok.RequiredArgsConstructor;
import net.dv8tion.jda.api.entities.Guild;
Expand All @@ -23,8 +23,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
return;

String id = guild.getId();
Collection<Command> serverCommands = commandMap.getServerCommands(id);
Optional<Command> firstServerCommand = serverCommands.stream()
Collection<SlashCommand> serverCommands = commandMap.getServerCommands(id);
Optional<SlashCommand> firstServerCommand = serverCommands.stream()
.filter(command -> command.getName().equalsIgnoreCase(event.getName()) ||
command.getAliases().stream().anyMatch(alias -> alias.equalsIgnoreCase(event.getName())))
.findFirst();
Expand All @@ -34,7 +34,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
return;
}

Collection<Command> globalCommands = commandMap.getGlobalCommands();
Collection<SlashCommand> globalCommands = commandMap.getGlobalCommands();
globalCommands.stream().filter(command -> command.getName().equalsIgnoreCase(event.getName()) ||
command.getAliases().stream().anyMatch(alias -> alias.equalsIgnoreCase(event.getName())))
.findFirst().ifPresent(command -> command.runCommand(event));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.negativekb.api.provider;

import dev.negativekb.api.commands.Command;
import dev.negativekb.api.commands.SlashCommand;
import dev.negativekb.api.commands.CommandCooldownManager;
import dev.negativekb.api.commands.SubCommand;
import dev.negativekb.api.commands.SlashSubCommand;
import lombok.Getter;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
Expand All @@ -11,8 +11,8 @@

public class CommandCooldownManagerProvider implements CommandCooldownManager {

private final HashMap<String, HashMap<Command, Long>> commandCooldowns = new HashMap<>();
private final HashMap<String, HashMap<SubCommand, Long>> subCommandCooldowns = new HashMap<>();
private final HashMap<String, HashMap<SlashCommand, Long>> commandCooldowns = new HashMap<>();
private final HashMap<String, HashMap<SlashSubCommand, Long>> subCommandCooldowns = new HashMap<>();

@Getter
private static CommandCooldownManagerProvider instance;
Expand All @@ -24,8 +24,8 @@ public CommandCooldownManagerProvider() {
}

@Override
public void addCooldown(@NotNull String id, @NotNull Command command, long duration) {
HashMap<Command, Long> activeCooldowns = commandCooldowns.get(id);
public void addCooldown(@NotNull String id, @NotNull SlashCommand command, long duration) {
HashMap<SlashCommand, Long> activeCooldowns = commandCooldowns.get(id);
if (activeCooldowns == null)
activeCooldowns = new HashMap<>();

Expand All @@ -38,8 +38,8 @@ public void addCooldown(@NotNull String id, @NotNull Command command, long durat
}

@Override
public void addCooldown(@NotNull String id, @NotNull SubCommand subCommand, long duration) {
HashMap<SubCommand, Long> activeCooldowns = subCommandCooldowns.get(id);
public void addCooldown(@NotNull String id, @NotNull SlashSubCommand subCommand, long duration) {
HashMap<SlashSubCommand, Long> activeCooldowns = subCommandCooldowns.get(id);
if (activeCooldowns == null)
activeCooldowns = new HashMap<>();

Expand All @@ -52,14 +52,14 @@ public void addCooldown(@NotNull String id, @NotNull SubCommand subCommand, long
}

@Override
public boolean checkCooldown(@NotNull String id, @NotNull Command command) {
Optional<Map.Entry<String, HashMap<Command, Long>>> first = commandCooldowns.entrySet().stream()
public boolean checkCooldown(@NotNull String id, @NotNull SlashCommand command) {
Optional<Map.Entry<String, HashMap<SlashCommand, Long>>> first = commandCooldowns.entrySet().stream()
.filter(cooldownEntry -> cooldownEntry.getKey().equalsIgnoreCase(id)).findFirst();

if (!first.isPresent())
return false;

Map.Entry<String, HashMap<Command, Long>> commandEntries = first.get();
Map.Entry<String, HashMap<SlashCommand, Long>> commandEntries = first.get();
return commandEntries.getValue()
.entrySet()
.stream()
Expand All @@ -68,35 +68,35 @@ public boolean checkCooldown(@NotNull String id, @NotNull Command command) {
}

@Override
public boolean checkCooldown(@NotNull String id, @NotNull SubCommand subCommand) {
Optional<Map.Entry<String, HashMap<SubCommand, Long>>> first = subCommandCooldowns.entrySet()
public boolean checkCooldown(@NotNull String id, @NotNull SlashSubCommand subCommand) {
Optional<Map.Entry<String, HashMap<SlashSubCommand, Long>>> first = subCommandCooldowns.entrySet()
.stream().filter(cooldownEntry -> cooldownEntry.getKey().equalsIgnoreCase(id)).findFirst();

if (!first.isPresent())
return false;

Map.Entry<String, HashMap<SubCommand, Long>> commandEntries = first.get();
Map.Entry<String, HashMap<SlashSubCommand, Long>> commandEntries = first.get();
return commandEntries.getValue()
.entrySet()
.stream()
.anyMatch(subCommandEntry -> subCommandEntry.getKey().getName().equalsIgnoreCase(subCommand.getName()));
}

@Override
public long getCooldown(@NotNull String id, @NotNull Command command) {
public long getCooldown(@NotNull String id, @NotNull SlashCommand command) {
if (!checkCooldown(id, command))
return 0;

HashMap<Command, Long> active = commandCooldowns.get(id);
HashMap<SlashCommand, Long> active = commandCooldowns.get(id);
return active.getOrDefault(command, 0L);
}

@Override
public long getCooldown(@NotNull String id, @NotNull SubCommand command) {
public long getCooldown(@NotNull String id, @NotNull SlashSubCommand command) {
if (!checkCooldown(id, command))
return 0;

HashMap<SubCommand, Long> active = subCommandCooldowns.get(id);
HashMap<SlashSubCommand, Long> active = subCommandCooldowns.get(id);
return active.getOrDefault(command, 0L);
}

Expand All @@ -114,11 +114,11 @@ private class Task extends TimerTask {
@SneakyThrows
@Override
public void run() {
HashMap<String, ArrayList<Command>> commandsToRemove = new HashMap<>();
HashMap<String, ArrayList<SubCommand>> subCommandsToRemove = new HashMap<>();
HashMap<String, ArrayList<SlashCommand>> commandsToRemove = new HashMap<>();
HashMap<String, ArrayList<SlashSubCommand>> subCommandsToRemove = new HashMap<>();

commandCooldowns.forEach((user, cooldowns) -> {
ArrayList<Command> removable = new ArrayList<>();
ArrayList<SlashCommand> removable = new ArrayList<>();
cooldowns.entrySet()
.stream()
.filter(commandEntry -> System.currentTimeMillis() >= commandEntry.getValue())
Expand All @@ -128,7 +128,7 @@ public void run() {
});

subCommandCooldowns.forEach((user, cooldowns) -> {
ArrayList<SubCommand> removable = new ArrayList<>();
ArrayList<SlashSubCommand> removable = new ArrayList<>();
cooldowns.entrySet()
.stream()
.filter(commandEntry -> System.currentTimeMillis() >= commandEntry.getValue())
Expand All @@ -138,7 +138,7 @@ public void run() {
});

commandsToRemove.forEach((user, commands) -> {
HashMap<Command, Long> active = commandCooldowns.get(user);
HashMap<SlashCommand, Long> active = commandCooldowns.get(user);
commands.forEach(active::remove);

if (active.isEmpty())
Expand All @@ -148,7 +148,7 @@ public void run() {
});

subCommandsToRemove.forEach((user, subCommands) -> {
HashMap<SubCommand, Long> active = subCommandCooldowns.get(user);
HashMap<SlashSubCommand, Long> active = subCommandCooldowns.get(user);
subCommands.forEach(active::remove);

if (active.isEmpty())
Expand Down
Loading

0 comments on commit c23636c

Please sign in to comment.