From b35b962ac6a880ca5c692a3950cab2dee83e5191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 21 Nov 2022 15:37:12 +0100 Subject: [PATCH] Implement basic say command --- .../commands/slash/BotInfoCommand.java | 9 ++--- .../commands/slash/ClearChatCommand.java | 4 +-- .../hidekobot/commands/slash/SayCommand.java | 36 +++++++++++++++++++ .../listeners/SlashCommandListener.java | 1 + .../utils/{TimeUtil.java => FormatUtil.java} | 4 +-- .../hidekobot/utils/SlashCommandUtil.java | 3 ++ 6 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/commands/slash/SayCommand.java rename src/main/java/wtf/beatrice/hidekobot/utils/{TimeUtil.java => FormatUtil.java} (94%) diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java index 21d1285..4ec42f8 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java @@ -6,13 +6,10 @@ import net.dv8tion.jda.api.interactions.commands.Command; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Configuration; import wtf.beatrice.hidekobot.HidekoBot; -import wtf.beatrice.hidekobot.utils.TimeUtil; +import wtf.beatrice.hidekobot.utils.FormatUtil; import java.lang.management.ManagementFactory; import java.text.DecimalFormat; -import java.time.Duration; -import java.time.LocalDateTime; -import java.time.temporal.ChronoUnit; import java.util.List; public class BotInfoCommand @@ -75,12 +72,12 @@ public class BotInfoCommand embedBuilder.addField("Author", authorMention, true); // uptime field - embedBuilder.addField("Uptime", TimeUtil.getNiceUptime(), true); + embedBuilder.addField("Uptime", FormatUtil.getNiceUptime(), true); // issue tracker field embedBuilder.addField("Support", "[Issue tracker](https://git.beatrice.wtf/mind-overflow/HidekoBot/issues)", - true); + true); //todo: we should probably make this a final field in the config class } event.getHook().editOriginalEmbeds(embedBuilder.build()).queue(); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java index 879f60a..fe5fb9d 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java @@ -38,8 +38,8 @@ public class ClearChatCommand /* get the amount from the command args. NULL should not be possible because we specified them as mandatory, but apparently the mobile app doesn't care and still sends the command if you omit the args. */ - OptionMapping amountMapping = event.getOption("amount"); - int toDeleteAmount = amountMapping == null ? 1 : amountMapping.getAsInt(); + OptionMapping amountOption = event.getOption("amount"); + int toDeleteAmount = amountOption == null ? 1 : amountOption.getAsInt(); if(toDeleteAmount <= 0) { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/SayCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/SayCommand.java new file mode 100644 index 0000000..560fc0d --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/SayCommand.java @@ -0,0 +1,36 @@ +package wtf.beatrice.hidekobot.commands.slash; + +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; + +public class SayCommand +{ + + public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) + { + MessageChannel channel = event.getChannel(); + + // get the text to send + OptionMapping textOption = event.getOption("text"); + String messageContent = ""; + if(textOption != null) + { + messageContent = textOption.getAsString(); + } + + if(textOption == null || messageContent.isEmpty()) + { + event.reply("Hey, you have to tell me what to say!") + .setEphemeral(true) + .queue(); + return; + } + + channel.sendMessage(messageContent).queue(); + event.reply("Message sent!") + .setEphemeral(true) + .queue(); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java index 791e069..b0c52c0 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java @@ -20,6 +20,7 @@ public class SlashCommandListener extends ListenerAdapter case "help" -> new HelpCommand().runSlashCommand(event); case "invite" -> new InviteCommand().runSlashCommand(event); case "ping" -> new PingCommand().runSlashCommand(event); + case "say" -> new SayCommand().runSlashCommand(event); } } } diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/TimeUtil.java b/src/main/java/wtf/beatrice/hidekobot/utils/FormatUtil.java similarity index 94% rename from src/main/java/wtf/beatrice/hidekobot/utils/TimeUtil.java rename to src/main/java/wtf/beatrice/hidekobot/utils/FormatUtil.java index a7512d2..b298785 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/TimeUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/utils/FormatUtil.java @@ -6,12 +6,12 @@ import java.time.Duration; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; -public class TimeUtil +public class FormatUtil { /** * Generate a nicely formatted uptime String that omits unnecessary data - * (eg. 0 days, 0 hours, 4 minutes, 32 seconds -> 4m 32s) + * (e.g. 0 days, 0 hours, 4 minutes, 32 seconds -> 4m 32s) * * @return the formatter String */ diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandUtil.java b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandUtil.java index df756f9..80102f7 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandUtil.java @@ -34,6 +34,9 @@ public class SlashCommandUtil add(Commands.slash("help", "Get general help on the bot.")); add(Commands.slash("invite", "Get an invite link for the bot.")); add(Commands.slash("ping", "Test if the bot is responsive.")); + add(Commands.slash("say", "Make the bot say something.") + .addOption(OptionType.STRING, "text", "The message to send.", true, false) + .setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MESSAGE_MANAGE))); }}; public static void updateSlashCommands(boolean force)