Make slash commands interface and load them dynamically
All checks were successful
continuous-integration/drone/push Build is passing

Slash commands are now loaded dynamically by implementing a SlashCommand interface and storing them in a loaded commands map.
This commit is contained in:
2022-11-22 14:32:22 +01:00
parent 7ae4790d5c
commit 882c695484
13 changed files with 133 additions and 26 deletions

View File

@@ -6,10 +6,17 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEve
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.objects.SlashCommand;
public class AvatarCommand
public class AvatarCommand implements SlashCommand
{
@Override
public String getCommandName() {
return "avatar";
}
@Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{
// defer reply because this might take a moment

View File

@@ -6,15 +6,22 @@ import net.dv8tion.jda.api.interactions.commands.Command;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.HidekoBot;
import wtf.beatrice.hidekobot.objects.SlashCommand;
import wtf.beatrice.hidekobot.util.FormatUtil;
import java.lang.management.ManagementFactory;
import java.text.DecimalFormat;
import java.util.List;
public class BotInfoCommand
public class BotInfoCommand implements SlashCommand
{
@Override
public String getCommandName() {
return "botinfo";
}
@Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{
// defer reply because this might take a moment

View File

@@ -13,13 +13,21 @@ import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.requests.restaction.WebhookMessageEditAction;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.objects.SlashCommand;
import java.util.ArrayList;
import java.util.List;
public class ClearCommand
public class ClearCommand implements SlashCommand
{
@Override
public String getCommandName() {
return "clear";
}
@Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{

View File

@@ -9,16 +9,23 @@ import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.objects.SlashCommand;
import wtf.beatrice.hidekobot.util.RandomUtil;
import java.util.List;
public class CoinFlipCommand
public class CoinFlipCommand implements SlashCommand
{
@Override
public String getCommandName() {
return "coinflip";
}
private final Button reflipButton = Button.primary("coinflip_reflip", "Flip again")
.withEmoji(Emoji.fromFormatted("\uD83E\uDE99"));
@Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{
// perform coin flip

View File

@@ -4,13 +4,20 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEve
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.HidekoBot;
import wtf.beatrice.hidekobot.objects.SlashCommand;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class DieCommand
public class DieCommand implements SlashCommand
{
@Override
public String getCommandName() {
return "die";
}
@Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{
if(Cache.getBotOwnerId() != event.getUser().getIdLong())

View File

@@ -4,10 +4,17 @@ import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.objects.SlashCommand;
public class HelpCommand
public class HelpCommand implements SlashCommand
{
@Override
public String getCommandName() {
return "help";
}
@Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{
// defer reply because replying might take a while

View File

@@ -11,9 +11,17 @@ import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.HidekoBot;
import wtf.beatrice.hidekobot.objects.SlashCommand;
public class InviteCommand
public class InviteCommand implements SlashCommand
{
@Override
public String getCommandName() {
return "invite";
}
@Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{
// defer reply because this might take a moment

View File

@@ -2,9 +2,16 @@ package wtf.beatrice.hidekobot.commands.slash;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.objects.SlashCommand;
public class PingCommand
public class PingCommand implements SlashCommand
{
@Override
public String getCommandName() {
return "ping";
}
@Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{
event.reply("Pong!").queue();

View File

@@ -4,10 +4,18 @@ import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.objects.SlashCommand;
public class SayCommand
public class SayCommand implements SlashCommand
{
@Override
public String getCommandName() {
return "say";
}
@Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{
MessageChannel channel = event.getChannel();