From 217b284f14885cc46c99a6c0a2598569964391c9 Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 7 Jul 2024 01:34:27 +0100 Subject: [PATCH] move lookup to a sub command and add mem usage to the bot stats command --- .../cc/fascinated/bat/common/StringUtils.java | 14 ++++++++++ .../bat/features/base/BaseFeature.java | 4 ++- .../commands/general/BotStatsCommand.java | 8 +++--- .../utility/lookup/LookupCommand.java | 28 +++++++++++++++++++ .../lookup/UserSubCommand.java} | 10 +++---- 5 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/features/base/commands/utility/lookup/LookupCommand.java rename src/main/java/cc/fascinated/bat/features/base/commands/{general/LookupUserCommand.java => utility/lookup/UserSubCommand.java} (93%) diff --git a/src/main/java/cc/fascinated/bat/common/StringUtils.java b/src/main/java/cc/fascinated/bat/common/StringUtils.java index a2b84fc..a2cfa5d 100644 --- a/src/main/java/cc/fascinated/bat/common/StringUtils.java +++ b/src/main/java/cc/fascinated/bat/common/StringUtils.java @@ -37,4 +37,18 @@ public class StringUtils { } return inputString; } + + /** + * Formats bytes into a human-readable format + * + * @param bytes the bytes + * @return the formatted bytes + */ + public static String formatBytes(long bytes) { + int unit = 1024; + if (bytes < unit) return bytes + " B"; + int exp = (int) (Math.log(bytes) / Math.log(unit)); + char pre = "KMGTPE".charAt(exp-1); + return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); + } } diff --git a/src/main/java/cc/fascinated/bat/features/base/BaseFeature.java b/src/main/java/cc/fascinated/bat/features/base/BaseFeature.java index e11e9dc..ef95d16 100644 --- a/src/main/java/cc/fascinated/bat/features/base/BaseFeature.java +++ b/src/main/java/cc/fascinated/bat/features/base/BaseFeature.java @@ -9,6 +9,8 @@ import cc.fascinated.bat.features.base.commands.fun.image.ImageCommand; import cc.fascinated.bat.features.base.commands.general.*; import cc.fascinated.bat.features.base.commands.general.avatar.AvatarCommand; import cc.fascinated.bat.features.base.commands.general.banner.BannerCommand; +import cc.fascinated.bat.features.base.commands.utility.lookup.LookupCommand; +import cc.fascinated.bat.features.base.commands.utility.lookup.UserSubCommand; import cc.fascinated.bat.features.base.commands.server.MemberCountCommand; import cc.fascinated.bat.features.base.commands.server.PremiumCommand; import cc.fascinated.bat.features.base.commands.server.channel.ChannelCommand; @@ -43,7 +45,7 @@ public class BaseFeature extends Feature { super.registerCommand(commandService, context.getBean(ImageCommand.class)); super.registerCommand(commandService, context.getBean(FeatureCommand.class)); super.registerCommand(commandService, context.getBean(EightBallCommand.class)); - super.registerCommand(commandService, context.getBean(LookupUserCommand.class)); + super.registerCommand(commandService, context.getBean(LookupCommand.class)); super.registerCommand(commandService, context.getBean(PPSizeCommand.class)); super.registerCommand(commandService, context.getBean(PastebinCommand.class)); } diff --git a/src/main/java/cc/fascinated/bat/features/base/commands/general/BotStatsCommand.java b/src/main/java/cc/fascinated/bat/features/base/commands/general/BotStatsCommand.java index cdd9b7d..6df47dc 100644 --- a/src/main/java/cc/fascinated/bat/features/base/commands/general/BotStatsCommand.java +++ b/src/main/java/cc/fascinated/bat/features/base/commands/general/BotStatsCommand.java @@ -3,10 +3,7 @@ package cc.fascinated.bat.features.base.commands.general; import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.Category; import cc.fascinated.bat.command.CommandInfo; -import cc.fascinated.bat.common.EmbedDescriptionBuilder; -import cc.fascinated.bat.common.EmbedUtils; -import cc.fascinated.bat.common.NumberFormatter; -import cc.fascinated.bat.common.TimeUtils; +import cc.fascinated.bat.common.*; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.service.DiscordService; @@ -32,6 +29,7 @@ public class BotStatsCommand extends BatCommand { private final GuildService guildService; private final UserService userService; private final RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean(); + private final Runtime runtime = Runtime.getRuntime(); @Autowired public BotStatsCommand(@NonNull GuildService guildService, @NonNull UserService userService) { @@ -42,12 +40,14 @@ public class BotStatsCommand extends BatCommand { @Override public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction event) { JDA jda = DiscordService.JDA; + long memoryUsed = (runtime.totalMemory() - runtime.freeMemory()); event.replyEmbeds(EmbedUtils.genericEmbed().setDescription( new EmbedDescriptionBuilder("Bat Statistics") .appendLine("Guilds: **%s**".formatted(NumberFormatter.format(jda.getGuilds().size())), true) .appendLine("Users: **%s**".formatted(NumberFormatter.format(jda.getUsers().size())), true) .appendLine("Gateway Ping: **%sms**".formatted(jda.getGatewayPing()), true) + .appendLine("Memory Usage: **%s**".formatted(StringUtils.formatBytes(memoryUsed)), true) .emptyLine() .appendSubtitle("Bot Statistics") .appendLine("Uptime: **%s**".formatted(TimeUtils.format(bean.getUptime())), true) diff --git a/src/main/java/cc/fascinated/bat/features/base/commands/utility/lookup/LookupCommand.java b/src/main/java/cc/fascinated/bat/features/base/commands/utility/lookup/LookupCommand.java new file mode 100644 index 0000000..a030b5f --- /dev/null +++ b/src/main/java/cc/fascinated/bat/features/base/commands/utility/lookup/LookupCommand.java @@ -0,0 +1,28 @@ +package cc.fascinated.bat.features.base.commands.utility.lookup; + +import cc.fascinated.bat.command.BatCommand; +import cc.fascinated.bat.command.Category; +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 Nick (okNick) + */ +@Component +@CommandInfo( + name = "lookup", + description = "View the banner of the guild or a user", + userInstall = true, + category = Category.UTILITY +) +public class LookupCommand extends BatCommand { + @Autowired + public LookupCommand(@NonNull ApplicationContext context) { + super.addSubCommands( + context.getBean(UserSubCommand.class) + ); + } +} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/bat/features/base/commands/general/LookupUserCommand.java b/src/main/java/cc/fascinated/bat/features/base/commands/utility/lookup/UserSubCommand.java similarity index 93% rename from src/main/java/cc/fascinated/bat/features/base/commands/general/LookupUserCommand.java rename to src/main/java/cc/fascinated/bat/features/base/commands/utility/lookup/UserSubCommand.java index a4c864c..e00539d 100644 --- a/src/main/java/cc/fascinated/bat/features/base/commands/general/LookupUserCommand.java +++ b/src/main/java/cc/fascinated/bat/features/base/commands/utility/lookup/UserSubCommand.java @@ -1,4 +1,4 @@ -package cc.fascinated.bat.features.base.commands.general; +package cc.fascinated.bat.features.base.commands.utility.lookup; import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.Category; @@ -22,10 +22,10 @@ import org.springframework.stereotype.Component; /** * @author Fascinated (fascinated7) */ -@Component -@CommandInfo(name = "lookupuser", description = "Lookup a user", userInstall = true, category = Category.GENERAL) -public class LookupUserCommand extends BatCommand { - public LookupUserCommand() { +@Component("lookup.user:sub") +@CommandInfo(name = "user", description = "Lookup a user") +public class UserSubCommand extends BatCommand { + public UserSubCommand() { super.addOptions(new OptionData(OptionType.STRING, "id", "The id of the user", true)); }