Skip to content

Commit

Permalink
Port log analyzer command to bot command (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs authored Dec 2, 2023
1 parent c5ea8cb commit acce259
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 69 deletions.
80 changes: 43 additions & 37 deletions src/main/java/de/chojo/repbot/commands/bot/BotAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())));
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
31 changes: 2 additions & 29 deletions src/main/java/de/chojo/repbot/commands/log/handler/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -37,7 +37,7 @@ static List<String> mapUserLogEntry(EventContext context, List<ReputationLogEntr
return entries;
}

static List<String> mapMessageLogEntry(EventContext context, List<ReputationLogEntry> logEntries) {
public static List<String> mapMessageLogEntry(EventContext context, List<ReputationLogEntry> logEntries) {
if (logEntries.isEmpty()) return Collections.emptyList();

List<String> entries = new ArrayList<>();
Expand Down Expand Up @@ -73,7 +73,7 @@ static MessageEmbed userLogEmbed(EventContext context, Member user, String title
return builder.build();
}

static void buildFields(List<String> entries, LocalizedEmbedBuilder embedBuilder) {
public static void buildFields(List<String> entries, LocalizedEmbedBuilder embedBuilder) {
var joiner = new StringJoiner("\n");
for (var entry : entries) {
if (joiner.length() + entry.length() > MessageEmbed.DESCRIPTION_MAX_LENGTH) break;
Expand Down

0 comments on commit acce259

Please sign in to comment.