From acce259acc6a8bbecf996af17f16ac6df1db42d8 Mon Sep 17 00:00:00 2001 From: Lilly Tempest <46890129+rainbowdashlabs@users.noreply.github.com> Date: Sat, 2 Dec 2023 21:11:37 +0100 Subject: [PATCH] Port log analyzer command to bot command (#571) --- .../chojo/repbot/commands/bot/BotAdmin.java | 80 ++++++++++--------- .../commands/bot/handler/log/Analyzer.java | 27 +++++++ .../repbot/commands/log/handler/Analyzer.java | 31 +------ .../commands/log/handler/BaseAnalyzer.java | 47 +++++++++++ .../commands/log/handler/LogFormatter.java | 6 +- 5 files changed, 122 insertions(+), 69 deletions(-) create mode 100644 src/main/java/de/chojo/repbot/commands/bot/handler/log/Analyzer.java create mode 100644 src/main/java/de/chojo/repbot/commands/log/handler/BaseAnalyzer.java diff --git a/src/main/java/de/chojo/repbot/commands/bot/BotAdmin.java b/src/main/java/de/chojo/repbot/commands/bot/BotAdmin.java index 88ab704d4..a5d909fed 100644 --- a/src/main/java/de/chojo/repbot/commands/bot/BotAdmin.java +++ b/src/main/java/de/chojo/repbot/commands/bot/BotAdmin.java @@ -16,6 +16,7 @@ import de.chojo.repbot.commands.bot.handler.Redeploy; import de.chojo.repbot.commands.bot.handler.Search; import de.chojo.repbot.commands.bot.handler.SharedGuilds; +import de.chojo.repbot.commands.bot.handler.log.Analyzer; import de.chojo.repbot.commands.bot.handler.system.Metrics; import de.chojo.repbot.commands.bot.handler.system.Reload; import de.chojo.repbot.commands.bot.handler.system.Restart; @@ -29,42 +30,47 @@ public class BotAdmin extends SlashCommand { public BotAdmin(Guilds guilds, Configuration configuration, Statistic statistics) { super(Slash.of("bot", "Bot admin commands.") - .unlocalized() - .guildOnly() - .adminCommand() - .privateCommand() - .group(Group.of("system", "System management") - .subCommand(SubCommand.of("restart", "Restart bot") - .handler(new Restart(configuration))) - .subCommand(SubCommand.of("upgrade", "Deploy an update") - .handler(new Upgrade(configuration))) - .subCommand(SubCommand.of("shutdown", "Shutdown the bot.") - .handler(new Shudown(configuration))) - .subCommand(SubCommand.of("status", "System status") - .handler(new Status(statistics))) - .subCommand(SubCommand.of("metrics", "System metrics") - .handler(new Metrics(configuration))) - .subCommand(SubCommand.of("reload", "Reload configuration") - .handler(new Reload(configuration)))) - .subCommand(SubCommand.of("debug", "Debug of a guild") - .handler(new Debug(guilds)) - .argument(Argument.text("guild_id", "Id of guild").asRequired()) - .argument(Argument.text("channel_id", "Id of channel"))) - .subCommand(SubCommand.of("shared_guilds", "Shared guilds with a user") - .handler(new SharedGuilds(configuration)) - .argument(Argument.text("user_id", "user id")) - .argument(Argument.user("user", "user"))) - .subCommand(SubCommand.of("redeploy", "Redeploy guild commands") - .handler(new Redeploy()) - .argument(Argument.text("guild_id", "Guild id").asRequired())) - .subCommand(SubCommand.of("search", "Search for guilds") - .handler(new Search()) - .argument(Argument.text("term", "Search term").asRequired())) - .subCommand(SubCommand.of("invalidate_cache", "Invalidates cached data for the guild") - .handler(new InvalidateCache(guilds)) - .argument(Argument.text("guild", "guild id").asRequired())) - .subCommand(SubCommand.of("leave", "Leave a guild") - .handler(new Leave()) - .argument(Argument.text("guild_id", "Guild id").asRequired()))); + .unlocalized() + .guildOnly() + .adminCommand() + .privateCommand() + .group(Group.of("system", "System management") + .subCommand(SubCommand.of("restart", "Restart bot") + .handler(new Restart(configuration))) + .subCommand(SubCommand.of("upgrade", "Deploy an update") + .handler(new Upgrade(configuration))) + .subCommand(SubCommand.of("shutdown", "Shutdown the bot.") + .handler(new Shudown(configuration))) + .subCommand(SubCommand.of("status", "System status") + .handler(new Status(statistics))) + .subCommand(SubCommand.of("metrics", "System metrics") + .handler(new Metrics(configuration))) + .subCommand(SubCommand.of("reload", "Reload configuration") + .handler(new Reload(configuration)))) + .subCommand(SubCommand.of("debug", "Debug of a guild") + .handler(new Debug(guilds)) + .argument(Argument.text("guild_id", "Id of guild").asRequired()) + .argument(Argument.text("channel_id", "Id of channel"))) + .group(Group.of("log", "log access") + .subCommand(SubCommand.of("analyzer", "Analyzer log") + .handler(new Analyzer(guilds)) + .argument(Argument.text("guildid", "Guild id").asRequired()) + .argument(Argument.text("messageid", "Id of message").asRequired()))) + .subCommand(SubCommand.of("shared_guilds", "Shared guilds with a user") + .handler(new SharedGuilds(configuration)) + .argument(Argument.text("user_id", "user id")) + .argument(Argument.user("user", "user"))) + .subCommand(SubCommand.of("redeploy", "Redeploy guild commands") + .handler(new Redeploy()) + .argument(Argument.text("guild_id", "Guild id").asRequired())) + .subCommand(SubCommand.of("search", "Search for guilds") + .handler(new Search()) + .argument(Argument.text("term", "Search term").asRequired())) + .subCommand(SubCommand.of("invalidate_cache", "Invalidates cached data for the guild") + .handler(new InvalidateCache(guilds)) + .argument(Argument.text("guild", "guild id").asRequired())) + .subCommand(SubCommand.of("leave", "Leave a guild") + .handler(new Leave()) + .argument(Argument.text("guild_id", "Guild id").asRequired()))); } } diff --git a/src/main/java/de/chojo/repbot/commands/bot/handler/log/Analyzer.java b/src/main/java/de/chojo/repbot/commands/bot/handler/log/Analyzer.java new file mode 100644 index 000000000..1ca30ed2c --- /dev/null +++ b/src/main/java/de/chojo/repbot/commands/bot/handler/log/Analyzer.java @@ -0,0 +1,27 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * + * Copyright (C) RainbowDashLabs and Contributor + */ +package de.chojo.repbot.commands.bot.handler.log; + +import de.chojo.jdautil.interactions.slash.structure.handler.SlashHandler; +import de.chojo.jdautil.parsing.ValueParser; +import de.chojo.jdautil.wrapper.EventContext; +import de.chojo.repbot.commands.log.handler.BaseAnalyzer; +import de.chojo.repbot.dao.provider.Guilds; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; + +public class Analyzer extends BaseAnalyzer implements SlashHandler { + private final Guilds guilds; + + public Analyzer(Guilds guilds) { + this.guilds = guilds; + } + + @Override + public void onSlashCommand(SlashCommandInteractionEvent event, EventContext context) { + var guild_id = ValueParser.parseLong(event.getOption("guild_id").getAsString()); + onSlashCommand(event, context, guilds.byId(guild_id.get()).reputation()); + } +} diff --git a/src/main/java/de/chojo/repbot/commands/log/handler/Analyzer.java b/src/main/java/de/chojo/repbot/commands/log/handler/Analyzer.java index c7de62fb6..9a4e44dec 100644 --- a/src/main/java/de/chojo/repbot/commands/log/handler/Analyzer.java +++ b/src/main/java/de/chojo/repbot/commands/log/handler/Analyzer.java @@ -16,7 +16,7 @@ import java.util.List; -public class Analyzer implements SlashHandler { +public class Analyzer extends BaseAnalyzer implements SlashHandler { private final Guilds guilds; public Analyzer(Guilds guilds) { @@ -25,33 +25,6 @@ public Analyzer(Guilds guilds) { @Override public void onSlashCommand(SlashCommandInteractionEvent event, EventContext context) { - var optMessageId = ValueParser.parseLong(event.getOption("messageid").getAsString()); - if (optMessageId.isEmpty()) { - event.reply(context.localize("error.invalidMessage")).setEphemeral(true).queue(); - return; - } - var reputation = guilds.guild(event.getGuild()).reputation(); - var resultEntry = reputation.analyzer() - .get(optMessageId.get()); - - if (resultEntry.isEmpty()) { - event.reply(context.localize("command.log.analyzer.notanalyzed")).setEphemeral(true).queue(); - return; - } - - var embed = resultEntry.get().embed(event.getGuild(), context); - - var reputationLogEntries = reputation.log().messageLog(optMessageId.get(), 10); - - var entries = LogFormatter.mapMessageLogEntry(context, reputationLogEntries); - - var builder = new LocalizedEmbedBuilder(context.guildLocalizer()) - .setTitle("command.log.message.message.log", Replacement.create("ID", optMessageId.get())); - - LogFormatter.buildFields(entries, builder); - - embed.add(builder.build()); - - event.replyEmbeds(embed).setEphemeral(true).queue(); + onSlashCommand(event, context, guilds.guild(event.getGuild()).reputation()); } } diff --git a/src/main/java/de/chojo/repbot/commands/log/handler/BaseAnalyzer.java b/src/main/java/de/chojo/repbot/commands/log/handler/BaseAnalyzer.java new file mode 100644 index 000000000..e842bbed2 --- /dev/null +++ b/src/main/java/de/chojo/repbot/commands/log/handler/BaseAnalyzer.java @@ -0,0 +1,47 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * + * Copyright (C) RainbowDashLabs and Contributor + */ +package de.chojo.repbot.commands.log.handler; + +import de.chojo.jdautil.localization.util.LocalizedEmbedBuilder; +import de.chojo.jdautil.localization.util.Replacement; +import de.chojo.jdautil.parsing.ValueParser; +import de.chojo.jdautil.wrapper.EventContext; +import de.chojo.repbot.dao.access.guild.reputation.Reputation; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; + +public class BaseAnalyzer { + public void onSlashCommand(SlashCommandInteractionEvent event, EventContext context, Reputation reputation) { + var optMessageId = ValueParser.parseLong(event.getOption("message_id").getAsString()); + if (optMessageId.isEmpty()) { + event.reply(context.localize("error.invalidMessage")).setEphemeral(true).queue(); + return; + } + + var resultEntry = reputation.analyzer() + .get(optMessageId.get()); + + if (resultEntry.isEmpty()) { + event.reply(context.localize("command.log.analyzer.notanalyzed")).setEphemeral(true).queue(); + return; + } + + var embed = resultEntry.get().embed(event.getGuild(), context); + + var reputationLogEntries = reputation.log().messageLog(optMessageId.get(), 10); + + var entries = LogFormatter.mapMessageLogEntry(context, reputationLogEntries); + + var builder = new LocalizedEmbedBuilder(context.guildLocalizer()) + .setTitle("command.log.message.message.log", Replacement.create("ID", optMessageId.get())); + + LogFormatter.buildFields(entries, builder); + + embed.add(builder.build()); + + event.replyEmbeds(embed).setEphemeral(true).queue(); + + } +} diff --git a/src/main/java/de/chojo/repbot/commands/log/handler/LogFormatter.java b/src/main/java/de/chojo/repbot/commands/log/handler/LogFormatter.java index 744b15998..08484b85a 100644 --- a/src/main/java/de/chojo/repbot/commands/log/handler/LogFormatter.java +++ b/src/main/java/de/chojo/repbot/commands/log/handler/LogFormatter.java @@ -19,7 +19,7 @@ import java.util.StringJoiner; import java.util.function.Function; -final class LogFormatter { +public final class LogFormatter { static final int PAGE_SIZE = 15; private LogFormatter() { @@ -37,7 +37,7 @@ static List mapUserLogEntry(EventContext context, List mapMessageLogEntry(EventContext context, List logEntries) { + public static List mapMessageLogEntry(EventContext context, List logEntries) { if (logEntries.isEmpty()) return Collections.emptyList(); List entries = new ArrayList<>(); @@ -73,7 +73,7 @@ static MessageEmbed userLogEmbed(EventContext context, Member user, String title return builder.build(); } - static void buildFields(List entries, LocalizedEmbedBuilder embedBuilder) { + public static void buildFields(List entries, LocalizedEmbedBuilder embedBuilder) { var joiner = new StringJoiner("\n"); for (var entry : entries) { if (joiner.length() + entry.length() > MessageEmbed.DESCRIPTION_MAX_LENGTH) break;