From f737b7d571af4afc27bafd45505d9f60e3e51a07 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 28 Jun 2024 19:37:10 +0100 Subject: [PATCH] merge cat, dog, and fox commands into 1 --- .../CatSubCommand.java} | 9 ++++---- .../DogSubCommand.java} | 9 ++++---- .../FoxSubCommand.java} | 9 ++++---- .../command/impl/fun/image/ImageCommand.java | 22 +++++++++++++++++++ .../bat/service/CommandService.java | 7 +++--- 5 files changed, 38 insertions(+), 18 deletions(-) rename src/main/java/cc/fascinated/bat/command/impl/fun/{CatCommand.java => image/CatSubCommand.java} (85%) rename src/main/java/cc/fascinated/bat/command/impl/fun/{DogCommand.java => image/DogSubCommand.java} (85%) rename src/main/java/cc/fascinated/bat/command/impl/fun/{FoxCommand.java => image/FoxSubCommand.java} (85%) create mode 100644 src/main/java/cc/fascinated/bat/command/impl/fun/image/ImageCommand.java diff --git a/src/main/java/cc/fascinated/bat/command/impl/fun/CatCommand.java b/src/main/java/cc/fascinated/bat/command/impl/fun/image/CatSubCommand.java similarity index 85% rename from src/main/java/cc/fascinated/bat/command/impl/fun/CatCommand.java rename to src/main/java/cc/fascinated/bat/command/impl/fun/image/CatSubCommand.java index cdd4192..c276b6a 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/fun/CatCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/fun/image/CatSubCommand.java @@ -1,7 +1,6 @@ -package cc.fascinated.bat.command.impl.fun; +package cc.fascinated.bat.command.impl.fun.image; -import cc.fascinated.bat.command.BatCommand; -import cc.fascinated.bat.command.Category; +import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.WebRequest; @@ -18,8 +17,8 @@ import org.springframework.stereotype.Component; * @author Fascinated (fascinated7) */ @Component -@CommandInfo(name = "cat", description = "Get a random cat image", category = Category.FUN, guildOnly = false) -public class CatCommand extends BatCommand { +@CommandInfo(name = "cat", description = "Get a random cat image") +public class CatSubCommand extends BatSubCommand { @Override public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { CatImageToken[] responseEntity = WebRequest.getAsEntity("https://api.thecatapi.com/v1/images/search", CatImageToken[].class); diff --git a/src/main/java/cc/fascinated/bat/command/impl/fun/DogCommand.java b/src/main/java/cc/fascinated/bat/command/impl/fun/image/DogSubCommand.java similarity index 85% rename from src/main/java/cc/fascinated/bat/command/impl/fun/DogCommand.java rename to src/main/java/cc/fascinated/bat/command/impl/fun/image/DogSubCommand.java index 0e2fe74..07bd9ef 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/fun/DogCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/fun/image/DogSubCommand.java @@ -1,7 +1,6 @@ -package cc.fascinated.bat.command.impl.fun; +package cc.fascinated.bat.command.impl.fun.image; -import cc.fascinated.bat.command.BatCommand; -import cc.fascinated.bat.command.Category; +import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.WebRequest; @@ -18,8 +17,8 @@ import org.springframework.stereotype.Component; * @author Fascinated (fascinated7) */ @Component -@CommandInfo(name = "dog", description = "Get a random dog image", category = Category.FUN, guildOnly = false) -public class DogCommand extends BatCommand { +@CommandInfo(name = "dog", description = "Get a random dog image") +public class DogSubCommand extends BatSubCommand { @Override public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { RandomImage responseEntity = WebRequest.getAsEntity("https://dog.ceo/api/breeds/image/random", RandomImage.class); diff --git a/src/main/java/cc/fascinated/bat/command/impl/fun/FoxCommand.java b/src/main/java/cc/fascinated/bat/command/impl/fun/image/FoxSubCommand.java similarity index 85% rename from src/main/java/cc/fascinated/bat/command/impl/fun/FoxCommand.java rename to src/main/java/cc/fascinated/bat/command/impl/fun/image/FoxSubCommand.java index 4da33d9..03ec1d0 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/fun/FoxCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/fun/image/FoxSubCommand.java @@ -1,7 +1,6 @@ -package cc.fascinated.bat.command.impl.fun; +package cc.fascinated.bat.command.impl.fun.image; -import cc.fascinated.bat.command.BatCommand; -import cc.fascinated.bat.command.Category; +import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.WebRequest; @@ -18,8 +17,8 @@ import org.springframework.stereotype.Component; * @author Nick (okNick) */ @Component -@CommandInfo(name = "fox", description = "Get a random fox image", category = Category.FUN, guildOnly = false) -public class FoxCommand extends BatCommand { +@CommandInfo(name = "fox", description = "Get a random fox image") +public class FoxSubCommand extends BatSubCommand { @Override public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { RandomFoxToken responseEntity = WebRequest.getAsEntity("https://randomfox.ca/floof/", RandomFoxToken.class); diff --git a/src/main/java/cc/fascinated/bat/command/impl/fun/image/ImageCommand.java b/src/main/java/cc/fascinated/bat/command/impl/fun/image/ImageCommand.java new file mode 100644 index 0000000..3d88067 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/command/impl/fun/image/ImageCommand.java @@ -0,0 +1,22 @@ +package cc.fascinated.bat.command.impl.fun.image; + +import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.CommandInfo; +import lombok.NonNull; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +/** + * @author Fascinated (fascinated7) + */ +@Component +@CommandInfo(name = "image", description = "View a random image", guildOnly = false) +public class ImageCommand extends BatCommand { + @Autowired + public ImageCommand(@NonNull ApplicationContext context) { + super.addSubCommand(context.getBean(CatSubCommand.class)); + super.addSubCommand(context.getBean(DogSubCommand.class)); + super.addSubCommand(context.getBean(FoxSubCommand.class)); + } +} diff --git a/src/main/java/cc/fascinated/bat/service/CommandService.java b/src/main/java/cc/fascinated/bat/service/CommandService.java index 82e8d3e..9c97498 100644 --- a/src/main/java/cc/fascinated/bat/service/CommandService.java +++ b/src/main/java/cc/fascinated/bat/service/CommandService.java @@ -75,8 +75,9 @@ public class CommandService extends ListenerAdapter { // Unregister all commands that Discord has but we don't jda.retrieveCommands().complete().forEach(command -> { - CommandInfo commandInfo = commands.get(command.getName()).getCommandInfo(); - if (commands.containsKey(command.getName()) && (commandInfo.category().isHidden() || commandInfo.botOwnerOnly())) { + if (commands.containsKey(command.getName()) + && (commands.get(command.getName()).getCommandInfo().category().isHidden() + || commands.get(command.getName()).getCommandInfo().botOwnerOnly())) { jda.deleteCommandById(command.getId()).complete(); // Unregister the command on Discord log.info("Unregistered hidden command \"{}\" from Discord", command.getName()); return; @@ -111,7 +112,7 @@ public class CommandService extends ListenerAdapter { log.error("Unable to find the admin guild to register hidden commands"); } - log.info("Registered all slash commands in {}ms", System.currentTimeMillis() - before); + log.info("Registered {} slash commands in {}ms", discordCommands.size(), System.currentTimeMillis() - before); } /**