From c7208eef848218b7c0db7d40d165d1ba443cf059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 22 Nov 2022 21:02:48 +0100 Subject: [PATCH] Make botinfo support both slash and message commands --- .../wtf/beatrice/hidekobot/HidekoBot.java | 3 +- .../hidekobot/commands/base/BotInfo.java | 75 ++++++++++++++++ .../commands/message/BotInfoCommand.java | 51 +++++++++++ .../commands/message/CommandsCommand.java | 46 ---------- .../commands/slash/BotInfoCommand.java | 87 ++++--------------- 5 files changed, 144 insertions(+), 118 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/commands/base/BotInfo.java create mode 100644 src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java delete mode 100644 src/main/java/wtf/beatrice/hidekobot/commands/message/CommandsCommand.java diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index bb617d1..85499c0 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -6,7 +6,6 @@ import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.requests.GatewayIntent; import sun.misc.Signal; -import wtf.beatrice.hidekobot.commands.message.CommandsCommand; import wtf.beatrice.hidekobot.commands.message.HelloCommand; import wtf.beatrice.hidekobot.commands.slash.*; import wtf.beatrice.hidekobot.datasource.ConfigurationSource; @@ -115,7 +114,7 @@ public class HidekoBot // register message commands MessageCommandListener messageCommandListener = new MessageCommandListener(); messageCommandListener.registerCommand(new HelloCommand()); - messageCommandListener.registerCommand(new CommandsCommand()); + messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BotInfoCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.CoinFlipCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.ClearCommand()); Cache.setMessageCommandListener(messageCommandListener); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/BotInfo.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/BotInfo.java new file mode 100644 index 0000000..5d15664 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/BotInfo.java @@ -0,0 +1,75 @@ +package wtf.beatrice.hidekobot.commands.base; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.MessageEmbed; +import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.util.FormatUtil; + +import java.lang.management.ManagementFactory; +import java.text.DecimalFormat; +import java.util.List; + +public class BotInfo +{ + public static MessageEmbed generateEmbed(List commandLabels) + { + EmbedBuilder embedBuilder = new EmbedBuilder(); + embedBuilder.setColor(Cache.getBotColor()); + embedBuilder.setTitle(Cache.getBotName()); + + // thumbnail + String botAvatarUrl = HidekoBot.getAPI().getSelfUser().getAvatarUrl(); + if(botAvatarUrl != null) embedBuilder.setThumbnail(botAvatarUrl); + + // help field + long ownerId = Cache.getBotOwnerId(); + embedBuilder.addField("Getting started", + "This instance is run by <@" + ownerId + ">.\nType `/help` for help! ", + false); + + // commands list field + StringBuilder commandsListBuilder = new StringBuilder(); + commandsListBuilder.append(commandLabels.size()).append( " total - "); + for(int i = 0; i < commandLabels.size(); i++) + { + commandsListBuilder.append("`").append(commandLabels.get(i)).append("`"); + + if(i + 1 != commandLabels.size()) // don't add comma in last iteration + { + commandsListBuilder.append(", "); + } + + } + embedBuilder.addField("Commands", commandsListBuilder.toString(), false); + + // version field + embedBuilder.addField("Version", "v" + Cache.getBotVersion(), true); + + // jvm version field + String jvmVersion = ManagementFactory.getRuntimeMXBean().getVmVersion(); + // only keep the important part "v19.0.1" and omit "v19.0.1+10" + jvmVersion = jvmVersion.replaceAll("\\+.*", ""); + embedBuilder.addField("JVM Version", "v" + jvmVersion, true); + + // used ram field + long usedRamBytes = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()); + double usedRamMB = usedRamBytes / 1024.0 / 1024.0; // bytes -> kB -> MB + DecimalFormat ramMBFormatter = new DecimalFormat("#.##"); + embedBuilder.addField("RAM Usage", ramMBFormatter.format(usedRamMB) + " MB", true); + + // developer field + String developerMention = "<@" + Cache.getBotMaintainerId() + ">"; + embedBuilder.addField("Maintainer", developerMention, true); + + // uptime field + embedBuilder.addField("Uptime", FormatUtil.getNiceUptime(), true); + + // issue tracker field + embedBuilder.addField("Support", + "[Issue tracker](https://git.beatrice.wtf/mind-overflow/HidekoBot/issues)", + true); //todo: we should probably make this a final field in the config class + + return embedBuilder.build(); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java new file mode 100644 index 0000000..15272fd --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java @@ -0,0 +1,51 @@ +package wtf.beatrice.hidekobot.commands.message; + +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import org.jetbrains.annotations.Nullable; +import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.commands.base.BotInfo; +import wtf.beatrice.hidekobot.objects.commands.MessageCommand; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class BotInfoCommand implements MessageCommand +{ + + @Override + public LinkedList getCommandLabels() { + return new LinkedList<>(Collections.singletonList("botinfo")); + } + + @Nullable + @Override + public List getPermissions() { + return null; // anyone can use it + } + + @Override + public boolean passRawArgs() { + return false; + } + + @Override + public void runCommand(MessageReceivedEvent event, String label, String[] args) { + + // run in a thread because we might use thread-locking things + new Thread(() -> { + // get a list of message commands + LinkedList messageCommands = Cache.getMessageCommandListener().getRegisteredCommands(); + LinkedList commandNames = new LinkedList<>(); + for (MessageCommand command : messageCommands) { + commandNames.add(command.getCommandLabels().get(0)); + } + + // send the list + MessageEmbed embed = BotInfo.generateEmbed(commandNames); + event.getMessage().replyEmbeds(embed).queue(); + }).start(); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/CommandsCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/CommandsCommand.java deleted file mode 100644 index f2eebfb..0000000 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/CommandsCommand.java +++ /dev/null @@ -1,46 +0,0 @@ -package wtf.beatrice.hidekobot.commands.message; - -import net.dv8tion.jda.api.Permission; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; -import wtf.beatrice.hidekobot.Cache; -import wtf.beatrice.hidekobot.objects.commands.MessageCommand; - -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; - -public class CommandsCommand implements MessageCommand -{ - - @Override - public LinkedList getCommandLabels() { - return new LinkedList<>(Collections.singletonList("commands")); - } - - @Override - public List getPermissions() { return null; } - - @Override - public boolean passRawArgs() { return false; } - - @Override - public void runCommand(MessageReceivedEvent event, String label, String[] args) { - - StringBuilder commandsList = new StringBuilder(); - commandsList.append("Recognized message commands: "); - - LinkedList messageCommands = Cache.getMessageCommandListener().getRegisteredCommands(); - for(int i = 0; i < messageCommands.size(); i++) - { - commandsList.append("`") - .append(messageCommands.get(i).getCommandLabels().get(0)) - .append("`"); - if(i+1 != messageCommands.size()) - { commandsList.append(", "); } - - } - - event.getMessage().reply(commandsList.toString()).queue(); - - } -} 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 82fc6ce..80df263 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java @@ -1,18 +1,16 @@ package wtf.beatrice.hidekobot.commands.slash; -import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.Commands; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Cache; -import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.commands.base.BotInfo; import wtf.beatrice.hidekobot.objects.commands.SlashCommand; import wtf.beatrice.hidekobot.objects.commands.SlashCommandImpl; -import wtf.beatrice.hidekobot.util.FormatUtil; -import java.lang.management.ManagementFactory; -import java.text.DecimalFormat; +import java.util.LinkedList; import java.util.List; public class BotInfoCommand extends SlashCommandImpl @@ -25,74 +23,23 @@ public class BotInfoCommand extends SlashCommandImpl @Override public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { - // defer reply because this might take a moment - event.deferReply().queue(); + // run in a thread because we might use thread-locking things + new Thread(() -> { + // defer reply because this might take a moment + event.deferReply().queue(); - List registeredCommands = Cache.getSlashCommandListener().getRegisteredCommands(); - - EmbedBuilder embedBuilder = new EmbedBuilder(); - - // embed processing - { - embedBuilder.setColor(Cache.getBotColor()); - embedBuilder.setTitle(Cache.getBotName()); - - // thumbnail - String botAvatarUrl = HidekoBot.getAPI().getSelfUser().getAvatarUrl(); - if(botAvatarUrl != null) embedBuilder.setThumbnail(botAvatarUrl); - - // help field - long ownerId = Cache.getBotOwnerId(); - embedBuilder.addField("Getting started", - "This instance is run by <@" + ownerId + ">.\nType `/help` for help! ", - false); - - // commands list field - StringBuilder commandsListBuilder = new StringBuilder(); - commandsListBuilder.append(registeredCommands.size()).append( " total - "); - for(int i = 0; i < registeredCommands.size(); i++) + // get a list of slash commands + List registeredCommands = Cache.getSlashCommandListener().getRegisteredCommands(); + LinkedList registeredCommandNames = new LinkedList<>(); + for(SlashCommand command : registeredCommands) { - SlashCommand cmd = registeredCommands.get(i); - commandsListBuilder.append("`").append(cmd.getCommandName()).append("`"); - - if(i + 1 != registeredCommands.size()) // don't add comma in last iteration - { - commandsListBuilder.append(", "); - } - + // node: adding slash so people realize that this is specific about slash commands. + registeredCommandNames.add("/" + command.getCommandName()); } - embedBuilder.addField("Commands", commandsListBuilder.toString(), false); - - // version field - embedBuilder.addField("Version", "v" + Cache.getBotVersion(), true); - - // jvm version field - String jvmVersion = ManagementFactory.getRuntimeMXBean().getVmVersion(); - // only keep the important part "v19.0.1" and omit "v19.0.1+10" - jvmVersion = jvmVersion.replaceAll("\\+.*", ""); - embedBuilder.addField("JVM Version", "v" + jvmVersion, true); - - // used ram field - long usedRamBytes = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()); - double usedRamMB = usedRamBytes / 1024.0 / 1024.0; // bytes -> kB -> MB - DecimalFormat ramMBFormatter = new DecimalFormat("#.##"); - embedBuilder.addField("RAM Usage", ramMBFormatter.format(usedRamMB) + " MB", true); - - // developer field - String developerMention = "<@" + Cache.getBotMaintainerId() + ">"; - embedBuilder.addField("Maintainer", developerMention, true); - - // uptime field - embedBuilder.addField("Uptime", FormatUtil.getNiceUptime(), true); - - // issue tracker field - embedBuilder.addField("Support", - "[Issue tracker](https://git.beatrice.wtf/mind-overflow/HidekoBot/issues)", - true); //todo: we should probably make this a final field in the config class - } - - event.getHook().editOriginalEmbeds(embedBuilder.build()).queue(); - + // send the list + MessageEmbed embed = BotInfo.generateEmbed(registeredCommandNames); + event.getHook().editOriginalEmbeds(embed).queue(); + }).start(); } }