merge cat, dog, and fox commands into 1

This commit is contained in:
Lee 2024-06-28 19:37:10 +01:00
parent dc18c9fe7a
commit f737b7d571
5 changed files with 38 additions and 18 deletions

@ -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.BatSubCommand;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.command.CommandInfo;
import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.WebRequest; import cc.fascinated.bat.common.WebRequest;
@ -18,8 +17,8 @@ import org.springframework.stereotype.Component;
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Component @Component
@CommandInfo(name = "cat", description = "Get a random cat image", category = Category.FUN, guildOnly = false) @CommandInfo(name = "cat", description = "Get a random cat image")
public class CatCommand extends BatCommand { public class CatSubCommand extends BatSubCommand {
@Override @Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { 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); CatImageToken[] responseEntity = WebRequest.getAsEntity("https://api.thecatapi.com/v1/images/search", CatImageToken[].class);

@ -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.BatSubCommand;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.command.CommandInfo;
import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.WebRequest; import cc.fascinated.bat.common.WebRequest;
@ -18,8 +17,8 @@ import org.springframework.stereotype.Component;
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Component @Component
@CommandInfo(name = "dog", description = "Get a random dog image", category = Category.FUN, guildOnly = false) @CommandInfo(name = "dog", description = "Get a random dog image")
public class DogCommand extends BatCommand { public class DogSubCommand extends BatSubCommand {
@Override @Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { 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); RandomImage responseEntity = WebRequest.getAsEntity("https://dog.ceo/api/breeds/image/random", RandomImage.class);

@ -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.BatSubCommand;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.command.CommandInfo;
import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.WebRequest; import cc.fascinated.bat.common.WebRequest;
@ -18,8 +17,8 @@ import org.springframework.stereotype.Component;
* @author Nick (okNick) * @author Nick (okNick)
*/ */
@Component @Component
@CommandInfo(name = "fox", description = "Get a random fox image", category = Category.FUN, guildOnly = false) @CommandInfo(name = "fox", description = "Get a random fox image")
public class FoxCommand extends BatCommand { public class FoxSubCommand extends BatSubCommand {
@Override @Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { 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); RandomFoxToken responseEntity = WebRequest.getAsEntity("https://randomfox.ca/floof/", RandomFoxToken.class);

@ -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));
}
}

@ -75,8 +75,9 @@ public class CommandService extends ListenerAdapter {
// Unregister all commands that Discord has but we don't // Unregister all commands that Discord has but we don't
jda.retrieveCommands().complete().forEach(command -> { jda.retrieveCommands().complete().forEach(command -> {
CommandInfo commandInfo = commands.get(command.getName()).getCommandInfo(); if (commands.containsKey(command.getName())
if (commands.containsKey(command.getName()) && (commandInfo.category().isHidden() || commandInfo.botOwnerOnly())) { && (commands.get(command.getName()).getCommandInfo().category().isHidden()
|| commands.get(command.getName()).getCommandInfo().botOwnerOnly())) {
jda.deleteCommandById(command.getId()).complete(); // Unregister the command on Discord jda.deleteCommandById(command.getId()).complete(); // Unregister the command on Discord
log.info("Unregistered hidden command \"{}\" from Discord", command.getName()); log.info("Unregistered hidden command \"{}\" from Discord", command.getName());
return; return;
@ -111,7 +112,7 @@ public class CommandService extends ListenerAdapter {
log.error("Unable to find the admin guild to register hidden commands"); 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);
} }
/** /**