From 913e8e023a32072844c5af271de9ccde354690c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 20 Nov 2022 18:56:57 +0100 Subject: [PATCH] Refactor command packages --- .../wtf/beatrice/hidekobot/HidekoBot.java | 2 +- .../commands/completer/AvatarCompleter.java | 33 +++++++++++++++++++ .../slash}/AvatarCommand.java | 2 +- .../slash}/ClearChatCommand.java | 2 +- .../slash}/CoinFlipCommand.java | 2 +- .../slash}/DieCommand.java | 2 +- .../slash}/InviteCommand.java | 2 +- .../slash}/PingCommand.java | 2 +- .../listeners/SlashCommandCompleter.java | 26 ++------------- .../listeners/SlashCommandListener.java | 2 +- 10 files changed, 44 insertions(+), 31 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/commands/completer/AvatarCompleter.java rename src/main/java/wtf/beatrice/hidekobot/{slashcommands => commands/slash}/AvatarCommand.java (98%) rename src/main/java/wtf/beatrice/hidekobot/{slashcommands => commands/slash}/ClearChatCommand.java (99%) rename src/main/java/wtf/beatrice/hidekobot/{slashcommands => commands/slash}/CoinFlipCommand.java (92%) rename src/main/java/wtf/beatrice/hidekobot/{slashcommands => commands/slash}/DieCommand.java (94%) rename src/main/java/wtf/beatrice/hidekobot/{slashcommands => commands/slash}/InviteCommand.java (93%) rename src/main/java/wtf/beatrice/hidekobot/{slashcommands => commands/slash}/PingCommand.java (85%) diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 9936aa6..9238532 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -65,9 +65,9 @@ public class HidekoBot String botUserId = jda.getSelfUser().getId(); Configuration.setBotApplicationId(botUserId); + // store if we have to force refresh commands despite no apparent changes. boolean forceUpdateCommands = false; - // if there is more than 1 arg, then iterate through them because we have additional things to do. // we are doing this at the end because we might need the API to be already initialized for some things. if(args.length > 1) { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/completer/AvatarCompleter.java b/src/main/java/wtf/beatrice/hidekobot/commands/completer/AvatarCompleter.java new file mode 100644 index 0000000..134561c --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/completer/AvatarCompleter.java @@ -0,0 +1,33 @@ +package wtf.beatrice.hidekobot.commands.completer; + +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.Command; +import org.jetbrains.annotations.NotNull; +import wtf.beatrice.hidekobot.commands.slash.AvatarCommand; + +import java.util.ArrayList; +import java.util.List; + +public class AvatarCompleter +{ + + public AvatarCompleter(@NotNull CommandAutoCompleteInteractionEvent event) + { + if(event.getFocusedOption().getName().equals("size")) + { + + List options = new ArrayList<>(); + + for(int res : AvatarCommand.acceptedSizes) + { + String resString = String.valueOf(res); + String userInput = event.getFocusedOption().getValue(); + + if(resString.startsWith(userInput)) + options.add(new Command.Choice(resString, res)); + } + + event.replyChoices(options).queue(); + } + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java similarity index 98% rename from src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java rename to src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java index 49916b8..2e9d420 100644 --- a/src/main/java/wtf/beatrice/hidekobot/slashcommands/AvatarCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java @@ -1,4 +1,4 @@ -package wtf.beatrice.hidekobot.slashcommands; +package wtf.beatrice.hidekobot.commands.slash; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.User; diff --git a/src/main/java/wtf/beatrice/hidekobot/slashcommands/ClearChatCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java similarity index 99% rename from src/main/java/wtf/beatrice/hidekobot/slashcommands/ClearChatCommand.java rename to src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java index 4f7141a..b33a04e 100644 --- a/src/main/java/wtf/beatrice/hidekobot/slashcommands/ClearChatCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java @@ -1,4 +1,4 @@ -package wtf.beatrice.hidekobot.slashcommands; +package wtf.beatrice.hidekobot.commands.slash; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.MessageHistory; diff --git a/src/main/java/wtf/beatrice/hidekobot/slashcommands/CoinFlipCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java similarity index 92% rename from src/main/java/wtf/beatrice/hidekobot/slashcommands/CoinFlipCommand.java rename to src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java index ef048e7..a2c3edf 100644 --- a/src/main/java/wtf/beatrice/hidekobot/slashcommands/CoinFlipCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java @@ -1,4 +1,4 @@ -package wtf.beatrice.hidekobot.slashcommands; +package wtf.beatrice.hidekobot.commands.slash; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/wtf/beatrice/hidekobot/slashcommands/DieCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java similarity index 94% rename from src/main/java/wtf/beatrice/hidekobot/slashcommands/DieCommand.java rename to src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java index eccbad7..6a394e4 100644 --- a/src/main/java/wtf/beatrice/hidekobot/slashcommands/DieCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java @@ -1,4 +1,4 @@ -package wtf.beatrice.hidekobot.slashcommands; +package wtf.beatrice.hidekobot.commands.slash; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/wtf/beatrice/hidekobot/slashcommands/InviteCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java similarity index 93% rename from src/main/java/wtf/beatrice/hidekobot/slashcommands/InviteCommand.java rename to src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java index 9d24fd5..3cd92e5 100644 --- a/src/main/java/wtf/beatrice/hidekobot/slashcommands/InviteCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java @@ -1,4 +1,4 @@ -package wtf.beatrice.hidekobot.slashcommands; +package wtf.beatrice.hidekobot.commands.slash; import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; diff --git a/src/main/java/wtf/beatrice/hidekobot/slashcommands/PingCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/PingCommand.java similarity index 85% rename from src/main/java/wtf/beatrice/hidekobot/slashcommands/PingCommand.java rename to src/main/java/wtf/beatrice/hidekobot/commands/slash/PingCommand.java index 414f4e2..dc1933f 100644 --- a/src/main/java/wtf/beatrice/hidekobot/slashcommands/PingCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/PingCommand.java @@ -1,4 +1,4 @@ -package wtf.beatrice.hidekobot.slashcommands; +package wtf.beatrice.hidekobot.commands.slash; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java index daa8fdf..0eec987 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java @@ -2,11 +2,7 @@ package wtf.beatrice.hidekobot.listeners; import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; -import net.dv8tion.jda.api.interactions.commands.Command; -import wtf.beatrice.hidekobot.slashcommands.AvatarCommand; - -import java.util.ArrayList; -import java.util.List; +import wtf.beatrice.hidekobot.commands.completer.AvatarCompleter; public class SlashCommandCompleter extends ListenerAdapter { @@ -14,24 +10,8 @@ public class SlashCommandCompleter extends ListenerAdapter @Override public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) { - if(event.getName().equals("avatar")) - { - if(event.getFocusedOption().getName().equals("size")) - { - - List options = new ArrayList<>(); - - for(int res : AvatarCommand.acceptedSizes) - { - String resString = String.valueOf(res); - String userInput = event.getFocusedOption().getValue(); - - if(resString.startsWith(userInput)) - options.add(new Command.Choice(resString, res)); - } - - event.replyChoices(options).queue(); - } + switch (event.getName().toLowerCase()) { + case "avatar" -> new AvatarCompleter(event); } } } diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java index 605ff1d..8dfd996 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java @@ -3,7 +3,7 @@ package wtf.beatrice.hidekobot.listeners; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; -import wtf.beatrice.hidekobot.slashcommands.*; +import wtf.beatrice.hidekobot.commands.slash.*; public class SlashCommandListener extends ListenerAdapter {