From eb273b91e98a57cf01620f4ed01257d3c6abc240 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Fri, 3 Dec 2021 01:23:48 +0200 Subject: [PATCH 01/77] `minor fix` --- .../tjbot/commands/tags/TagManageCommand.java | 64 +++++++++++++++++++ .../tjbot/utils/ModAuditLogWriter.java | 59 +++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 16be87f43c..88da6553fb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -1,6 +1,7 @@ package org.togetherjava.tjbot.commands.tags; import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; @@ -11,12 +12,16 @@ import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.commands.utils.MessageUtils; import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.utils.ModAuditLogWriter; +import java.awt.*; import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.Objects; @@ -52,6 +57,11 @@ public final class TagManageCommand extends SlashCommandAdapter { private final TagSystem tagSystem; private final Predicate hasRequiredRole; + /** + * stolen from {@link org.togetherjava.tjbot.routines.ModAuditLogRoutine} + */ + private static final Color MOD_AUDIT_LOG_COLOR = Color.decode("#4FC3F7"); + /** * Creates a new instance, using the given tag system as base. * @@ -156,6 +166,8 @@ private void rawTag(@NotNull SlashCommandEvent event) { String content = tagSystem.getTag(id).orElseThrow(); event.reply("").addFile(content.getBytes(StandardCharsets.UTF_8), "content.md").queue(); + + logAction(event, id); } private void createTag(@NotNull CommandInteraction event) { @@ -204,6 +216,12 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, idAction.accept(id); sendSuccessMessage(event, id, actionVerb); + + if (event.getOptions().stream().anyMatch(o -> o.getName().equals(CONTENT_OPTION))) + logAction((SlashCommandEvent) event, id, + Objects.requireNonNull(event.getOption(CONTENT_OPTION)).getAsString()); + else + logAction((SlashCommandEvent) event, id); } /** @@ -239,6 +257,8 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, event.getMessageChannel().retrieveMessageById(messageId).queue(message -> { idAndContentAction.accept(tagId, message.getContentRaw()); sendSuccessMessage(event, tagId, actionVerb); + + logAction((SlashCommandEvent) event, tagId, message.getContentRaw()); }, failure -> { if (failure instanceof ErrorResponseException ex && ex.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) { @@ -285,6 +305,50 @@ private boolean isWrongTagStatusAndHandle(@NotNull TagStatus requiredTagStatus, return false; } + private void logAction(@NotNull SlashCommandEvent event, @NotNull String id, + @Nullable String content) { + Guild guild = Objects.requireNonNull(event.getGuild()); + + EmbedBuilder embed = new EmbedBuilder() + .setAuthor(event.getUser().getAsTag(), null, event.getUser().getAvatarUrl()) + .setColor(MOD_AUDIT_LOG_COLOR); + + switch (Subcommand.fromName(event.getSubcommandName())) { + case RAW -> { + ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Raw") + .setDescription(String.format("viewed raw tag **%s**", id))); + } + case CREATE -> { + ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Create") + .setDescription( + String.format("created tag **%s** with content: %n*%s*", id, content))); + } + case CREATE_WITH_MESSAGE -> { + ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Create with message") + .setDescription( + String.format("created tag **%s** with content: %n*%s*", id, content))); + } + case EDIT -> { + ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Edit") + .setDescription( + String.format("edited tag **%s** to content: %n*%s*", id, content))); + } + case EDIT_WITH_MESSAGE -> { + ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Edit with message") + .setDescription( + String.format("edited tag **%s** to content: %n*%s*", id, content))); + } + case DELETE -> { + ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Delete") + .setDescription(String.format("deleted tag **%s**", id))); + } + } + } + + private void logAction(@NotNull SlashCommandEvent event, @NotNull String id) { + logAction(event, id, null); + } + private boolean hasTagManageRole(@NotNull Member member) { return member.getRoles().stream().map(Role::getName).anyMatch(hasRequiredRole); } diff --git a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java new file mode 100644 index 0000000000..dc2369568d --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java @@ -0,0 +1,59 @@ +package org.togetherjava.tjbot.utils; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.config.Config; + +import javax.annotation.Nonnull; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.regex.Pattern; + +public class ModAuditLogWriter { + private static final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class); + + private static final Predicate isAuditLogChannelName = + Pattern.compile(Config.getInstance().getModAuditLogChannelPattern()).asMatchPredicate(); + private static final Predicate isAuditLogChannel = + channel -> isAuditLogChannelName.test(channel.getName()); + + private ModAuditLogWriter() { throw new IllegalStateException("Utility class"); } + + /** + * logs an entry in the mod audit log channel. + * + * @param guild the guild. usually use {@link GenericInteractionCreateEvent#getGuild()} + * + * @param embed the embed that will be sent. + */ + public static void log(@Nonnull Guild guild, @Nonnull EmbedBuilder embed) { + + Optional auditLogChannel = getModAuditLogChannel(guild); + if (auditLogChannel.isEmpty()) { + logger.warn( + "Unable to log moderation events, did not find a mod audit log channel matching the configured pattern '{}' for guild '{}'", + Config.getInstance().getModAuditLogChannelPattern(), guild.getName()); + return; + } + + auditLogChannel.get().sendMessageEmbeds(embed.build()).queue(); + } + + + /** + * stolen from {@link org.togetherjava.tjbot.routines.ModAuditLogRoutine} + */ + private static Optional getModAuditLogChannel(@NotNull Guild guild) { + // Check cache first, then get full list + return guild.getTextChannelCache() + .stream() + .filter(isAuditLogChannel) + .findAny() + .or(() -> guild.getTextChannels().stream().filter(isAuditLogChannel).findAny()); + } +} From b0ce95d0b68a6a87a6e7e3c46362ab50dc3fcd52 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Fri, 3 Dec 2021 03:37:35 +0200 Subject: [PATCH 02/77] fixed auto-checked problems --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 3 +++ .../java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 88da6553fb..205c8296a6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -342,6 +342,9 @@ private void logAction(@NotNull SlashCommandEvent event, @NotNull String id, ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Delete") .setDescription(String.format("deleted tag **%s**", id))); } + default -> { + throw new IllegalArgumentException("Subcommand Enum isn't valid"); + } } } diff --git a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java index dc2369568d..268d1c1887 100644 --- a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java @@ -22,7 +22,9 @@ public class ModAuditLogWriter { private static final Predicate isAuditLogChannel = channel -> isAuditLogChannelName.test(channel.getName()); - private ModAuditLogWriter() { throw new IllegalStateException("Utility class"); } + private ModAuditLogWriter() { + throw new IllegalStateException("Utility class"); + } /** * logs an entry in the mod audit log channel. From e06af671aada4038c49efc9cbc55ab065f3567ae Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Fri, 3 Dec 2021 11:18:54 +0200 Subject: [PATCH 03/77] `minor fix` --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 205c8296a6..bedd1e6a0f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -314,10 +314,6 @@ private void logAction(@NotNull SlashCommandEvent event, @NotNull String id, .setColor(MOD_AUDIT_LOG_COLOR); switch (Subcommand.fromName(event.getSubcommandName())) { - case RAW -> { - ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Raw") - .setDescription(String.format("viewed raw tag **%s**", id))); - } case CREATE -> { ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Create") .setDescription( From 5545a725c8e7d4ed620651d095f9eeb3a58c2907 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Fri, 3 Dec 2021 13:43:53 +0200 Subject: [PATCH 04/77] added `content.md` files to the logs and optimized logging methods to prevent `NullPointerException` --- .../tjbot/commands/tags/TagManageCommand.java | 101 ++++++++++++------ .../tjbot/utils/ModAuditLogWriter.java | 10 +- .../togetherjava/tjbot/utils/VirtualFile.java | 30 ++++++ 3 files changed, 108 insertions(+), 33 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/utils/VirtualFile.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index bedd1e6a0f..99144eb49c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -12,7 +12,6 @@ import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; @@ -20,6 +19,7 @@ import org.togetherjava.tjbot.commands.utils.MessageUtils; import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.utils.ModAuditLogWriter; +import org.togetherjava.tjbot.utils.VirtualFile; import java.awt.*; import java.nio.charset.StandardCharsets; @@ -214,14 +214,23 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, return; } + String oldContent = ""; + if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT + || Subcommand.fromName(event.getSubcommandName()) == Subcommand.DELETE) + oldContent = tagSystem.getTag(id).orElseThrow(); + idAction.accept(id); sendSuccessMessage(event, id, actionVerb); - if (event.getOptions().stream().anyMatch(o -> o.getName().equals(CONTENT_OPTION))) - logAction((SlashCommandEvent) event, id, + if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.CREATE) + logCreateAction(event, id, Objects.requireNonNull(event.getOption(CONTENT_OPTION)).getAsString()); - else - logAction((SlashCommandEvent) event, id); + if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT) + logEditAction(event, id, + Objects.requireNonNull(event.getOption(CONTENT_OPTION)).getAsString(), + oldContent); + if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.DELETE) + logDeleteAction(event, id, oldContent); } /** @@ -255,10 +264,18 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } event.getMessageChannel().retrieveMessageById(messageId).queue(message -> { + String oldContent = ""; + if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT_WITH_MESSAGE) + oldContent = tagSystem.getTag(tagId).orElseThrow(); + idAndContentAction.accept(tagId, message.getContentRaw()); sendSuccessMessage(event, tagId, actionVerb); - logAction((SlashCommandEvent) event, tagId, message.getContentRaw()); + if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.CREATE_WITH_MESSAGE) + logCreateAction(event, tagId, message.getContentRaw()); + if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT_WITH_MESSAGE) + logEditAction(event, tagId, message.getContentRaw(), oldContent); + }, failure -> { if (failure instanceof ErrorResponseException ex && ex.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) { @@ -305,47 +322,71 @@ private boolean isWrongTagStatusAndHandle(@NotNull TagStatus requiredTagStatus, return false; } - private void logAction(@NotNull SlashCommandEvent event, @NotNull String id, - @Nullable String content) { - Guild guild = Objects.requireNonNull(event.getGuild()); - - EmbedBuilder embed = new EmbedBuilder() + /** + * used to get a base {@link EmbedBuilder} for log embeds. + */ + private EmbedBuilder getLogEmbed(@NotNull CommandInteraction event) { + return new EmbedBuilder() .setAuthor(event.getUser().getAsTag(), null, event.getUser().getAvatarUrl()) .setColor(MOD_AUDIT_LOG_COLOR); + } + + private void logCreateAction(@NotNull CommandInteraction event, @NotNull String id, + @NotNull String content) { + Guild guild = Objects.requireNonNull(event.getGuild()); switch (Subcommand.fromName(event.getSubcommandName())) { case CREATE -> { - ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Create") - .setDescription( - String.format("created tag **%s** with content: %n*%s*", id, content))); + ModAuditLogWriter.log(guild, + getLogEmbed(event).setTitle("Tag-Manage Create") + .setDescription(String.format("created tag **%s**", id)), + new VirtualFile("content.md", content)); } case CREATE_WITH_MESSAGE -> { - ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Create with message") - .setDescription( - String.format("created tag **%s** with content: %n*%s*", id, content))); + ModAuditLogWriter.log(guild, + getLogEmbed(event).setTitle("Tag-Manage Create with message") + .setDescription(String.format("created tag **%s**", id)), + new VirtualFile("content.md", content)); } + default -> { + throw new IllegalArgumentException("Subcommand Enum invalid"); + } + } + } + + private void logEditAction(@NotNull CommandInteraction event, @NotNull String id, + @NotNull String newContent, @NotNull String oldContent) { + Guild guild = Objects.requireNonNull(event.getGuild()); + + switch (Subcommand.fromName(event.getSubcommandName())) { case EDIT -> { - ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Edit") - .setDescription( - String.format("edited tag **%s** to content: %n*%s*", id, content))); + ModAuditLogWriter.log(guild, + getLogEmbed(event).setTitle("Tag-Manage Edit") + .setDescription(String.format("edited tag **%s**", id)), + new VirtualFile("new_content.md", newContent), + new VirtualFile("old_content.md", oldContent)); } case EDIT_WITH_MESSAGE -> { - ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Edit with message") - .setDescription( - String.format("edited tag **%s** to content: %n*%s*", id, content))); - } - case DELETE -> { - ModAuditLogWriter.log(guild, embed.setTitle("Tag-Manage Delete") - .setDescription(String.format("deleted tag **%s**", id))); + ModAuditLogWriter.log(guild, + getLogEmbed(event).setTitle("Tag-Manage Edit with message") + .setDescription(String.format("edited tag **%s**", id)), + new VirtualFile("new_content.md", newContent), + new VirtualFile("old_content.md", oldContent)); } default -> { - throw new IllegalArgumentException("Subcommand Enum isn't valid"); + throw new IllegalArgumentException("Subcommand Enum invalid"); } } } - private void logAction(@NotNull SlashCommandEvent event, @NotNull String id) { - logAction(event, id, null); + private void logDeleteAction(@NotNull CommandInteraction event, @NotNull String id, + @NotNull String oldContent) { + Guild guild = Objects.requireNonNull(event.getGuild()); + + ModAuditLogWriter.log(guild, + getLogEmbed(event).setTitle("Tag-Manage Delete") + .setDescription(String.format("deleted tag **%s**", id)), + new VirtualFile("old_content.md", oldContent)); } private boolean hasTagManageRole(@NotNull Member member) { diff --git a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java index 268d1c1887..21d1fb4997 100644 --- a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java @@ -9,7 +9,6 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.config.Config; -import javax.annotation.Nonnull; import java.util.Optional; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -32,9 +31,11 @@ private ModAuditLogWriter() { * @param guild the guild. usually use {@link GenericInteractionCreateEvent#getGuild()} * * @param embed the embed that will be sent. + * + * @param files the files added to the message. */ - public static void log(@Nonnull Guild guild, @Nonnull EmbedBuilder embed) { - + public static void log(@NotNull Guild guild, @NotNull EmbedBuilder embed, + VirtualFile... files) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { logger.warn( @@ -44,6 +45,9 @@ public static void log(@Nonnull Guild guild, @Nonnull EmbedBuilder embed) { } auditLogChannel.get().sendMessageEmbeds(embed.build()).queue(); + for (VirtualFile file : files) { + auditLogChannel.get().sendFile(file.getAsInputStream(), file.getName()).queue(); + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/utils/VirtualFile.java b/application/src/main/java/org/togetherjava/tjbot/utils/VirtualFile.java new file mode 100644 index 0000000000..04da31887e --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/utils/VirtualFile.java @@ -0,0 +1,30 @@ +package org.togetherjava.tjbot.utils; + +import org.jetbrains.annotations.NotNull; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +public class VirtualFile { + private final String name; + private final String content; + + /** + * + * @param name the name of the file, example: {@code "foo.md"} + * @param content the content of the file. + */ + public VirtualFile(@NotNull String name, @NotNull String content) { + this.name = name; + this.content = content; + } + + public String getName() { + return name; + } + + public InputStream getAsInputStream() { + return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)); + } +} From 3c8c59c4f564704ea96d930bad7a0aa6bcc20d77 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Fri, 3 Dec 2021 19:25:43 +0200 Subject: [PATCH 05/77] removed duplicate `getTextChannels()` --- .../java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java index 21d1fb4997..6571714258 100644 --- a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java @@ -55,11 +55,9 @@ public static void log(@NotNull Guild guild, @NotNull EmbedBuilder embed, * stolen from {@link org.togetherjava.tjbot.routines.ModAuditLogRoutine} */ private static Optional getModAuditLogChannel(@NotNull Guild guild) { - // Check cache first, then get full list return guild.getTextChannelCache() .stream() .filter(isAuditLogChannel) - .findAny() - .or(() -> guild.getTextChannels().stream().filter(isAuditLogChannel).findAny()); + .findAny(); } } From 202e11798d5d14aa8bf0779688422588adc14207 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Fri, 3 Dec 2021 23:42:09 +0200 Subject: [PATCH 06/77] made `log()` `synchronized` to prevent interruptions --- .../java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java index 6571714258..a3d6e3c4c6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java @@ -34,7 +34,7 @@ private ModAuditLogWriter() { * * @param files the files added to the message. */ - public static void log(@NotNull Guild guild, @NotNull EmbedBuilder embed, + public static synchronized void log(@NotNull Guild guild, @NotNull EmbedBuilder embed, VirtualFile... files) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { From a4447cf99c1aecee7e82264c98d4b50699fa071a Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Sat, 4 Dec 2021 00:34:49 +0200 Subject: [PATCH 07/77] made `enum Filename` --- .../tjbot/commands/tags/TagManageCommand.java | 74 ++++++++++--------- .../tjbot/utils/ModAuditLogWriter.java | 6 +- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 99144eb49c..c16a48a843 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -336,21 +336,17 @@ private void logCreateAction(@NotNull CommandInteraction event, @NotNull String Guild guild = Objects.requireNonNull(event.getGuild()); switch (Subcommand.fromName(event.getSubcommandName())) { - case CREATE -> { - ModAuditLogWriter.log(guild, - getLogEmbed(event).setTitle("Tag-Manage Create") - .setDescription(String.format("created tag **%s**", id)), - new VirtualFile("content.md", content)); - } - case CREATE_WITH_MESSAGE -> { - ModAuditLogWriter.log(guild, - getLogEmbed(event).setTitle("Tag-Manage Create with message") - .setDescription(String.format("created tag **%s**", id)), - new VirtualFile("content.md", content)); - } - default -> { - throw new IllegalArgumentException("Subcommand Enum invalid"); - } + case CREATE -> ModAuditLogWriter.log(guild, + getLogEmbed(event).setTitle("Tag-Manage Create") + .setDescription(String.format("created tag **%s**", id)), + new VirtualFile(Filename.CONTENT.get(), content)); + + case CREATE_WITH_MESSAGE -> ModAuditLogWriter.log(guild, + getLogEmbed(event).setTitle("Tag-Manage Create with message") + .setDescription(String.format("created tag **%s**", id)), + new VirtualFile(Filename.CONTENT.get(), content)); + + default -> throw new IllegalArgumentException("Subcommand Enum invalid"); } } @@ -359,23 +355,19 @@ private void logEditAction(@NotNull CommandInteraction event, @NotNull String id Guild guild = Objects.requireNonNull(event.getGuild()); switch (Subcommand.fromName(event.getSubcommandName())) { - case EDIT -> { - ModAuditLogWriter.log(guild, - getLogEmbed(event).setTitle("Tag-Manage Edit") - .setDescription(String.format("edited tag **%s**", id)), - new VirtualFile("new_content.md", newContent), - new VirtualFile("old_content.md", oldContent)); - } - case EDIT_WITH_MESSAGE -> { - ModAuditLogWriter.log(guild, - getLogEmbed(event).setTitle("Tag-Manage Edit with message") - .setDescription(String.format("edited tag **%s**", id)), - new VirtualFile("new_content.md", newContent), - new VirtualFile("old_content.md", oldContent)); - } - default -> { - throw new IllegalArgumentException("Subcommand Enum invalid"); - } + case EDIT -> ModAuditLogWriter.log(guild, + getLogEmbed(event).setTitle("Tag-Manage Edit") + .setDescription(String.format("edited tag **%s**", id)), + new VirtualFile(Filename.NEW_CONTENT.get(), newContent), + new VirtualFile(Filename.OLD_CONTENT.get(), oldContent)); + + case EDIT_WITH_MESSAGE -> ModAuditLogWriter.log(guild, + getLogEmbed(event).setTitle("Tag-Manage Edit with message") + .setDescription(String.format("edited tag **%s**", id)), + new VirtualFile(Filename.NEW_CONTENT.get(), newContent), + new VirtualFile(Filename.OLD_CONTENT.get(), oldContent)); + + default -> throw new IllegalArgumentException("Subcommand Enum invalid"); } } @@ -386,7 +378,23 @@ private void logDeleteAction(@NotNull CommandInteraction event, @NotNull String ModAuditLogWriter.log(guild, getLogEmbed(event).setTitle("Tag-Manage Delete") .setDescription(String.format("deleted tag **%s**", id)), - new VirtualFile("old_content.md", oldContent)); + new VirtualFile(Filename.OLD_CONTENT.get(), oldContent)); + } + + private enum Filename { + CONTENT("content.md"), + OLD_CONTENT("old_content.md"), + NEW_CONTENT("new_content.md"); + + private final String name; + + Filename(String name) { + this.name = name; + } + + private String get() { + return name; + } } private boolean hasTagManageRole(@NotNull Member member) { diff --git a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java index a3d6e3c4c6..b6abbe8ba3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java @@ -50,14 +50,10 @@ public static synchronized void log(@NotNull Guild guild, @NotNull EmbedBuilder } } - /** * stolen from {@link org.togetherjava.tjbot.routines.ModAuditLogRoutine} */ private static Optional getModAuditLogChannel(@NotNull Guild guild) { - return guild.getTextChannelCache() - .stream() - .filter(isAuditLogChannel) - .findAny(); + return guild.getTextChannelCache().stream().filter(isAuditLogChannel).findAny(); } } From ccbaecbd6c6f8d1e1bbb415216934477b79d0bda Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Sat, 4 Dec 2021 19:34:53 +0200 Subject: [PATCH 08/77] files are now in the same message as the embed --- .../org/togetherjava/tjbot/utils/ModAuditLogWriter.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java index b6abbe8ba3..916f3cb972 100644 --- a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java @@ -4,6 +4,7 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent; +import net.dv8tion.jda.api.requests.restaction.MessageAction; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,10 +45,11 @@ public static synchronized void log(@NotNull Guild guild, @NotNull EmbedBuilder return; } - auditLogChannel.get().sendMessageEmbeds(embed.build()).queue(); + MessageAction message = auditLogChannel.get().sendMessageEmbeds(embed.build()); for (VirtualFile file : files) { - auditLogChannel.get().sendFile(file.getAsInputStream(), file.getName()).queue(); + message = message.addFile(file.getAsInputStream(), file.getName()); } + message.queue(); } /** From b9f98fa4cd482174fc802eb7dec5c51d384f17dc Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Sat, 4 Dec 2021 21:24:54 +0200 Subject: [PATCH 09/77] changed `oldContent` to `previousContent` --- .../tjbot/commands/tags/TagManageCommand.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index c16a48a843..74514ed546 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -214,10 +214,10 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, return; } - String oldContent = ""; + String previousContent = ""; if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT || Subcommand.fromName(event.getSubcommandName()) == Subcommand.DELETE) - oldContent = tagSystem.getTag(id).orElseThrow(); + previousContent = tagSystem.getTag(id).orElseThrow(); idAction.accept(id); sendSuccessMessage(event, id, actionVerb); @@ -228,9 +228,9 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT) logEditAction(event, id, Objects.requireNonNull(event.getOption(CONTENT_OPTION)).getAsString(), - oldContent); + previousContent); if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.DELETE) - logDeleteAction(event, id, oldContent); + logDeleteAction(event, id, previousContent); } /** @@ -264,9 +264,9 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } event.getMessageChannel().retrieveMessageById(messageId).queue(message -> { - String oldContent = ""; + String previousContent = ""; if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT_WITH_MESSAGE) - oldContent = tagSystem.getTag(tagId).orElseThrow(); + previousContent = tagSystem.getTag(tagId).orElseThrow(); idAndContentAction.accept(tagId, message.getContentRaw()); sendSuccessMessage(event, tagId, actionVerb); @@ -274,7 +274,7 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.CREATE_WITH_MESSAGE) logCreateAction(event, tagId, message.getContentRaw()); if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT_WITH_MESSAGE) - logEditAction(event, tagId, message.getContentRaw(), oldContent); + logEditAction(event, tagId, message.getContentRaw(), previousContent); }, failure -> { if (failure instanceof ErrorResponseException ex @@ -351,7 +351,7 @@ private void logCreateAction(@NotNull CommandInteraction event, @NotNull String } private void logEditAction(@NotNull CommandInteraction event, @NotNull String id, - @NotNull String newContent, @NotNull String oldContent) { + @NotNull String newContent, @NotNull String previousContent) { Guild guild = Objects.requireNonNull(event.getGuild()); switch (Subcommand.fromName(event.getSubcommandName())) { @@ -359,31 +359,31 @@ private void logEditAction(@NotNull CommandInteraction event, @NotNull String id getLogEmbed(event).setTitle("Tag-Manage Edit") .setDescription(String.format("edited tag **%s**", id)), new VirtualFile(Filename.NEW_CONTENT.get(), newContent), - new VirtualFile(Filename.OLD_CONTENT.get(), oldContent)); + new VirtualFile(Filename.OLD_CONTENT.get(), previousContent)); case EDIT_WITH_MESSAGE -> ModAuditLogWriter.log(guild, getLogEmbed(event).setTitle("Tag-Manage Edit with message") .setDescription(String.format("edited tag **%s**", id)), new VirtualFile(Filename.NEW_CONTENT.get(), newContent), - new VirtualFile(Filename.OLD_CONTENT.get(), oldContent)); + new VirtualFile(Filename.OLD_CONTENT.get(), previousContent)); default -> throw new IllegalArgumentException("Subcommand Enum invalid"); } } private void logDeleteAction(@NotNull CommandInteraction event, @NotNull String id, - @NotNull String oldContent) { + @NotNull String previousContent) { Guild guild = Objects.requireNonNull(event.getGuild()); ModAuditLogWriter.log(guild, getLogEmbed(event).setTitle("Tag-Manage Delete") .setDescription(String.format("deleted tag **%s**", id)), - new VirtualFile(Filename.OLD_CONTENT.get(), oldContent)); + new VirtualFile(Filename.OLD_CONTENT.get(), previousContent)); } private enum Filename { CONTENT("content.md"), - OLD_CONTENT("old_content.md"), + OLD_CONTENT("previous_content.md"), NEW_CONTENT("new_content.md"); private final String name; From a78c039af8fa5323bc0728292f1a14361e2feef5 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Sat, 4 Dec 2021 21:50:04 +0200 Subject: [PATCH 10/77] changed `class VirtualFile` to `record Attachment` nested it as an inner class of `ModAuditLogWriter` fixed javadoc --- .../tjbot/commands/tags/TagManageCommand.java | 15 +++++----- .../tjbot/utils/ModAuditLogWriter.java | 29 ++++++++++++++++-- .../togetherjava/tjbot/utils/VirtualFile.java | 30 ------------------- 3 files changed, 33 insertions(+), 41 deletions(-) delete mode 100644 application/src/main/java/org/togetherjava/tjbot/utils/VirtualFile.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 74514ed546..2f8ad6f65f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -19,7 +19,6 @@ import org.togetherjava.tjbot.commands.utils.MessageUtils; import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.utils.ModAuditLogWriter; -import org.togetherjava.tjbot.utils.VirtualFile; import java.awt.*; import java.nio.charset.StandardCharsets; @@ -339,12 +338,12 @@ private void logCreateAction(@NotNull CommandInteraction event, @NotNull String case CREATE -> ModAuditLogWriter.log(guild, getLogEmbed(event).setTitle("Tag-Manage Create") .setDescription(String.format("created tag **%s**", id)), - new VirtualFile(Filename.CONTENT.get(), content)); + new ModAuditLogWriter.Attachment(Filename.CONTENT.get(), content)); case CREATE_WITH_MESSAGE -> ModAuditLogWriter.log(guild, getLogEmbed(event).setTitle("Tag-Manage Create with message") .setDescription(String.format("created tag **%s**", id)), - new VirtualFile(Filename.CONTENT.get(), content)); + new ModAuditLogWriter.Attachment(Filename.CONTENT.get(), content)); default -> throw new IllegalArgumentException("Subcommand Enum invalid"); } @@ -358,14 +357,14 @@ private void logEditAction(@NotNull CommandInteraction event, @NotNull String id case EDIT -> ModAuditLogWriter.log(guild, getLogEmbed(event).setTitle("Tag-Manage Edit") .setDescription(String.format("edited tag **%s**", id)), - new VirtualFile(Filename.NEW_CONTENT.get(), newContent), - new VirtualFile(Filename.OLD_CONTENT.get(), previousContent)); + new ModAuditLogWriter.Attachment(Filename.NEW_CONTENT.get(), newContent), + new ModAuditLogWriter.Attachment(Filename.OLD_CONTENT.get(), previousContent)); case EDIT_WITH_MESSAGE -> ModAuditLogWriter.log(guild, getLogEmbed(event).setTitle("Tag-Manage Edit with message") .setDescription(String.format("edited tag **%s**", id)), - new VirtualFile(Filename.NEW_CONTENT.get(), newContent), - new VirtualFile(Filename.OLD_CONTENT.get(), previousContent)); + new ModAuditLogWriter.Attachment(Filename.NEW_CONTENT.get(), newContent), + new ModAuditLogWriter.Attachment(Filename.OLD_CONTENT.get(), previousContent)); default -> throw new IllegalArgumentException("Subcommand Enum invalid"); } @@ -378,7 +377,7 @@ private void logDeleteAction(@NotNull CommandInteraction event, @NotNull String ModAuditLogWriter.log(guild, getLogEmbed(event).setTitle("Tag-Manage Delete") .setDescription(String.format("deleted tag **%s**", id)), - new VirtualFile(Filename.OLD_CONTENT.get(), previousContent)); + new ModAuditLogWriter.Attachment(Filename.OLD_CONTENT.get(), previousContent)); } private enum Filename { diff --git a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java index 916f3cb972..f2bb295f88 100644 --- a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java @@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.config.Config; +import java.nio.charset.StandardCharsets; import java.util.Optional; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -36,7 +37,7 @@ private ModAuditLogWriter() { * @param files the files added to the message. */ public static synchronized void log(@NotNull Guild guild, @NotNull EmbedBuilder embed, - VirtualFile... files) { + Attachment... files) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { logger.warn( @@ -46,8 +47,8 @@ public static synchronized void log(@NotNull Guild guild, @NotNull EmbedBuilder } MessageAction message = auditLogChannel.get().sendMessageEmbeds(embed.build()); - for (VirtualFile file : files) { - message = message.addFile(file.getAsInputStream(), file.getName()); + for (Attachment file : files) { + message = message.addFile(file.getContent(), file.getName()); } message.queue(); } @@ -58,4 +59,26 @@ public static synchronized void log(@NotNull Guild guild, @NotNull EmbedBuilder private static Optional getModAuditLogChannel(@NotNull Guild guild) { return guild.getTextChannelCache().stream().filter(isAuditLogChannel).findAny(); } + + /** + * used to add a file to a message without having an actual file. + * + * @param name the name of the file, example: {@code "foo.md"} + * @param content the content of the file + */ + public static final record Attachment(@NotNull String name, @NotNull String content) { + /** + * @return the name of the file. used by JDA methods + */ + public @NotNull String getName() { + return name; + } + + /** + * @return the content of the file as a {@code byte[]}. used by JDA methods + */ + public byte[] getContent() { + return content.getBytes(StandardCharsets.UTF_8); + } + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/utils/VirtualFile.java b/application/src/main/java/org/togetherjava/tjbot/utils/VirtualFile.java deleted file mode 100644 index 04da31887e..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/utils/VirtualFile.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.togetherjava.tjbot.utils; - -import org.jetbrains.annotations.NotNull; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; - -public class VirtualFile { - private final String name; - private final String content; - - /** - * - * @param name the name of the file, example: {@code "foo.md"} - * @param content the content of the file. - */ - public VirtualFile(@NotNull String name, @NotNull String content) { - this.name = name; - this.content = content; - } - - public String getName() { - return name; - } - - public InputStream getAsInputStream() { - return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)); - } -} From 40ce619dc4fc68a24bc21e8ad5d72b6bbb3f7fc4 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Sat, 4 Dec 2021 22:11:14 +0200 Subject: [PATCH 11/77] made `class ModAuditLogWriter` an `enum` moved it into `moderation` package completed javadoc --- .../tjbot/commands/tags/TagManageCommand.java | 2 +- .../{utils => moderation}/ModAuditLogWriter.java | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) rename application/src/main/java/org/togetherjava/tjbot/{utils => moderation}/ModAuditLogWriter.java (91%) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 2f8ad6f65f..927ae694e7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -18,7 +18,7 @@ import org.togetherjava.tjbot.commands.SlashCommandVisibility; import org.togetherjava.tjbot.commands.utils.MessageUtils; import org.togetherjava.tjbot.config.Config; -import org.togetherjava.tjbot.utils.ModAuditLogWriter; +import org.togetherjava.tjbot.moderation.ModAuditLogWriter; import java.awt.*; import java.nio.charset.StandardCharsets; diff --git a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java similarity index 91% rename from application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java rename to application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index f2bb295f88..a2fea831c6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/utils/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -1,4 +1,4 @@ -package org.togetherjava.tjbot.utils; +package org.togetherjava.tjbot.moderation; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Guild; @@ -15,7 +15,10 @@ import java.util.function.Predicate; import java.util.regex.Pattern; -public class ModAuditLogWriter { +/** + * used to log activities in the mod audit log channel + */ +public enum ModAuditLogWriter {; private static final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class); private static final Predicate isAuditLogChannelName = @@ -23,10 +26,6 @@ public class ModAuditLogWriter { private static final Predicate isAuditLogChannel = channel -> isAuditLogChannelName.test(channel.getName()); - private ModAuditLogWriter() { - throw new IllegalStateException("Utility class"); - } - /** * logs an entry in the mod audit log channel. * @@ -36,7 +35,7 @@ private ModAuditLogWriter() { * * @param files the files added to the message. */ - public static synchronized void log(@NotNull Guild guild, @NotNull EmbedBuilder embed, + public static void log(@NotNull Guild guild, @NotNull EmbedBuilder embed, Attachment... files) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { From 4bfb896397de7725c18d81313dd15a7b3c7e1584 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Sat, 4 Dec 2021 22:21:07 +0200 Subject: [PATCH 12/77] fixed minor code-style problems --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 927ae694e7..48617d607b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -56,9 +56,6 @@ public final class TagManageCommand extends SlashCommandAdapter { private final TagSystem tagSystem; private final Predicate hasRequiredRole; - /** - * stolen from {@link org.togetherjava.tjbot.routines.ModAuditLogRoutine} - */ private static final Color MOD_AUDIT_LOG_COLOR = Color.decode("#4FC3F7"); /** @@ -264,8 +261,9 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, event.getMessageChannel().retrieveMessageById(messageId).queue(message -> { String previousContent = ""; - if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT_WITH_MESSAGE) + if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT_WITH_MESSAGE) { previousContent = tagSystem.getTag(tagId).orElseThrow(); + } idAndContentAction.accept(tagId, message.getContentRaw()); sendSuccessMessage(event, tagId, actionVerb); From d7e3ccf046d22a93603e21cc724530c8f9e5722b Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Sun, 5 Dec 2021 13:00:55 +0200 Subject: [PATCH 13/77] improved the structure of `ModAuditLogWriter#log()` --- .../tjbot/commands/tags/TagManageCommand.java | 65 +++++++++---------- .../tjbot/moderation/ModAuditLogWriter.java | 56 +++++++++++++--- 2 files changed, 78 insertions(+), 43 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 48617d607b..5102a091ff 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -56,8 +56,6 @@ public final class TagManageCommand extends SlashCommandAdapter { private final TagSystem tagSystem; private final Predicate hasRequiredRole; - private static final Color MOD_AUDIT_LOG_COLOR = Color.decode("#4FC3F7"); - /** * Creates a new instance, using the given tag system as base. * @@ -319,29 +317,20 @@ private boolean isWrongTagStatusAndHandle(@NotNull TagStatus requiredTagStatus, return false; } - /** - * used to get a base {@link EmbedBuilder} for log embeds. - */ - private EmbedBuilder getLogEmbed(@NotNull CommandInteraction event) { - return new EmbedBuilder() - .setAuthor(event.getUser().getAsTag(), null, event.getUser().getAvatarUrl()) - .setColor(MOD_AUDIT_LOG_COLOR); - } - private void logCreateAction(@NotNull CommandInteraction event, @NotNull String id, @NotNull String content) { Guild guild = Objects.requireNonNull(event.getGuild()); switch (Subcommand.fromName(event.getSubcommandName())) { - case CREATE -> ModAuditLogWriter.log(guild, - getLogEmbed(event).setTitle("Tag-Manage Create") - .setDescription(String.format("created tag **%s**", id)), - new ModAuditLogWriter.Attachment(Filename.CONTENT.get(), content)); + case CREATE -> ModAuditLogWriter.log("Tag-Manage Create", + String.format("created tag **%s**", id), event.getUser(), + event.getTimeCreated(), guild, new ModAuditLogWriter.Attachment[] { + new ModAuditLogWriter.Attachment(Filename.CONTENT.get(), content)}); - case CREATE_WITH_MESSAGE -> ModAuditLogWriter.log(guild, - getLogEmbed(event).setTitle("Tag-Manage Create with message") - .setDescription(String.format("created tag **%s**", id)), - new ModAuditLogWriter.Attachment(Filename.CONTENT.get(), content)); + case CREATE_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Create with message", + String.format("created tag **%s**", id), event.getUser(), + event.getTimeCreated(), guild, new ModAuditLogWriter.Attachment[] { + new ModAuditLogWriter.Attachment(Filename.CONTENT.get(), content)}); default -> throw new IllegalArgumentException("Subcommand Enum invalid"); } @@ -352,17 +341,23 @@ private void logEditAction(@NotNull CommandInteraction event, @NotNull String id Guild guild = Objects.requireNonNull(event.getGuild()); switch (Subcommand.fromName(event.getSubcommandName())) { - case EDIT -> ModAuditLogWriter.log(guild, - getLogEmbed(event).setTitle("Tag-Manage Edit") - .setDescription(String.format("edited tag **%s**", id)), - new ModAuditLogWriter.Attachment(Filename.NEW_CONTENT.get(), newContent), - new ModAuditLogWriter.Attachment(Filename.OLD_CONTENT.get(), previousContent)); - - case EDIT_WITH_MESSAGE -> ModAuditLogWriter.log(guild, - getLogEmbed(event).setTitle("Tag-Manage Edit with message") - .setDescription(String.format("edited tag **%s**", id)), - new ModAuditLogWriter.Attachment(Filename.NEW_CONTENT.get(), newContent), - new ModAuditLogWriter.Attachment(Filename.OLD_CONTENT.get(), previousContent)); + case EDIT -> ModAuditLogWriter.log("Tag-Manage Edit", + String.format("edited tag **%s**", id), event.getUser(), event.getTimeCreated(), + guild, + new ModAuditLogWriter.Attachment[] { + new ModAuditLogWriter.Attachment(Filename.NEW_CONTENT.get(), + newContent), + new ModAuditLogWriter.Attachment(Filename.PREVIOUS_CONTENT.get(), + previousContent)}); + + case EDIT_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Edit with message", + String.format("edited tag **%s**", id), event.getUser(), event.getTimeCreated(), + guild, + new ModAuditLogWriter.Attachment[] { + new ModAuditLogWriter.Attachment(Filename.NEW_CONTENT.get(), + newContent), + new ModAuditLogWriter.Attachment(Filename.PREVIOUS_CONTENT.get(), + previousContent)}); default -> throw new IllegalArgumentException("Subcommand Enum invalid"); } @@ -372,15 +367,15 @@ private void logDeleteAction(@NotNull CommandInteraction event, @NotNull String @NotNull String previousContent) { Guild guild = Objects.requireNonNull(event.getGuild()); - ModAuditLogWriter.log(guild, - getLogEmbed(event).setTitle("Tag-Manage Delete") - .setDescription(String.format("deleted tag **%s**", id)), - new ModAuditLogWriter.Attachment(Filename.OLD_CONTENT.get(), previousContent)); + ModAuditLogWriter.log("Tag-Manage Delete", String.format("delete tag **%s**", id), + event.getUser(), event.getTimeCreated(), guild, + new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( + Filename.PREVIOUS_CONTENT.get(), previousContent)}); } private enum Filename { CONTENT("content.md"), - OLD_CONTENT("previous_content.md"), + PREVIOUS_CONTENT("previous_content.md"), NEW_CONTENT("new_content.md"); private final String name; diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index a2fea831c6..18caea0ff7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -3,6 +3,7 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent; import net.dv8tion.jda.api.requests.restaction.MessageAction; import org.jetbrains.annotations.NotNull; @@ -10,7 +11,9 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.config.Config; +import java.awt.*; import java.nio.charset.StandardCharsets; +import java.time.temporal.TemporalAccessor; import java.util.Optional; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -18,7 +21,10 @@ /** * used to log activities in the mod audit log channel */ -public enum ModAuditLogWriter {; +public enum ModAuditLogWriter { + ; + private static final Color EMBED_COLOR = Color.decode("#4FC3F7"); + private static final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class); private static final Predicate isAuditLogChannelName = @@ -28,15 +34,23 @@ public enum ModAuditLogWriter {; /** * logs an entry in the mod audit log channel. - * - * @param guild the guild. usually use {@link GenericInteractionCreateEvent#getGuild()} * - * @param embed the embed that will be sent. + * @param title the title of the log embed + * + * @param description the description of the log embed + * + * @param author the user who triggered the action * - * @param files the files added to the message. + * @param timestamp the timestamp of the action. usually use + * {@link GenericInteractionCreateEvent#getTimeCreated()} + * + * @param guild the guild. usually use {@link GenericInteractionCreateEvent#getGuild()} + * + * @param files the files that'll be added to the message */ - public static void log(@NotNull Guild guild, @NotNull EmbedBuilder embed, - Attachment... files) { + public static void log(@NotNull String title, @NotNull String description, @NotNull User author, + @NotNull TemporalAccessor timestamp, @NotNull Guild guild, + @NotNull Attachment[] files) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { logger.warn( @@ -45,13 +59,39 @@ public static void log(@NotNull Guild guild, @NotNull EmbedBuilder embed, return; } - MessageAction message = auditLogChannel.get().sendMessageEmbeds(embed.build()); + MessageAction message = auditLogChannel.get() + .sendMessageEmbeds(new EmbedBuilder().setTitle(title) + .setDescription(description) + .setAuthor(author.getAsTag(), null, author.getAvatarUrl()) + .setTimestamp(timestamp) + .setColor(EMBED_COLOR) + .build()); + for (Attachment file : files) { message = message.addFile(file.getContent(), file.getName()); } message.queue(); } + /** + * logs an entry in the mod audit log channel. + * + * @param title the title of the log embed + * + * @param description the description of the log embed + * + * @param author the user who triggered the action + * + * @param timestamp the timestamp of the action. usually use + * {@link GenericInteractionCreateEvent#getTimeCreated()} + * + * @param guild the guild. usually use {@link GenericInteractionCreateEvent#getGuild()} + */ + public static void log(@NotNull String title, @NotNull String description, @NotNull User author, + @NotNull TemporalAccessor timestamp, @NotNull Guild guild) { + log(title, description, author, timestamp, guild, new Attachment[] {}); + } + /** * stolen from {@link org.togetherjava.tjbot.routines.ModAuditLogRoutine} */ From 60e328bad0b36eea9f3a0e46911e6eb8485e932f Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Sun, 5 Dec 2021 20:11:19 +0200 Subject: [PATCH 14/77] merged the 3 logging methods in `TagManageCommand` fixed the design of the merged method --- .../tjbot/commands/tags/TagManageCommand.java | 126 +++++++++--------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 5102a091ff..221da9d57c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -4,6 +4,7 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.interactions.Interaction; @@ -12,6 +13,7 @@ import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; @@ -20,10 +22,12 @@ import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.moderation.ModAuditLogWriter; +import java.time.temporal.TemporalAccessor; import java.awt.*; import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.Objects; +import java.util.Optional; import java.util.OptionalLong; import java.util.function.BiConsumer; import java.util.function.Consumer; @@ -167,25 +171,30 @@ private void rawTag(@NotNull SlashCommandEvent event) { private void createTag(@NotNull CommandInteraction event) { String content = Objects.requireNonNull(event.getOption(CONTENT_OPTION)).getAsString(); - handleAction(TagStatus.NOT_EXISTS, id -> tagSystem.putTag(id, content), "created", event); + handleAction(TagStatus.NOT_EXISTS, id -> tagSystem.putTag(id, content), "created", event, + Subcommand.CREATE, content); } private void createTagWithMessage(@NotNull CommandInteraction event) { - handleActionWithMessage(TagStatus.NOT_EXISTS, tagSystem::putTag, "created", event); + handleActionWithMessage(TagStatus.NOT_EXISTS, tagSystem::putTag, "created", event, + Subcommand.CREATE_WITH_MESSAGE); } private void editTag(@NotNull CommandInteraction event) { String content = Objects.requireNonNull(event.getOption(CONTENT_OPTION)).getAsString(); - handleAction(TagStatus.EXISTS, id -> tagSystem.putTag(id, content), "edited", event); + handleAction(TagStatus.EXISTS, id -> tagSystem.putTag(id, content), "edited", event, + Subcommand.EDIT, content); } private void editTagWithMessage(@NotNull CommandInteraction event) { - handleActionWithMessage(TagStatus.EXISTS, tagSystem::putTag, "edited", event); + handleActionWithMessage(TagStatus.EXISTS, tagSystem::putTag, "edited", event, + Subcommand.EDIT_WITH_MESSAGE); } private void deleteTag(@NotNull CommandInteraction event) { - handleAction(TagStatus.EXISTS, tagSystem::deleteTag, "deleted", event); + handleAction(TagStatus.EXISTS, tagSystem::deleteTag, "deleted", event, Subcommand.DELETE, + null); } /** @@ -199,32 +208,31 @@ private void deleteTag(@NotNull CommandInteraction event) { * @param actionVerb the verb describing the executed action, i.e. edited or * created, will be displayed in the message send to the user * @param event the event to send messages with, it must have an {@code id} option set + * @param subcommand the executed subcommand + * @param newContent the new content of the tag (@{@link Nullable}: only if assigning new + * content) */ private void handleAction(@NotNull TagStatus requiredTagStatus, @NotNull Consumer idAction, @NotNull String actionVerb, - @NotNull CommandInteraction event) { + @NotNull CommandInteraction event, @NotNull Subcommand subcommand, + @Nullable String newContent) { + String id = Objects.requireNonNull(event.getOption(ID_OPTION)).getAsString(); if (isWrongTagStatusAndHandle(requiredTagStatus, id, event)) { return; } - String previousContent = ""; - if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT - || Subcommand.fromName(event.getSubcommandName()) == Subcommand.DELETE) + String previousContent = null; + if (subcommand == Subcommand.EDIT || subcommand == Subcommand.DELETE) { previousContent = tagSystem.getTag(id).orElseThrow(); + } idAction.accept(id); sendSuccessMessage(event, id, actionVerb); - if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.CREATE) - logCreateAction(event, id, - Objects.requireNonNull(event.getOption(CONTENT_OPTION)).getAsString()); - if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT) - logEditAction(event, id, - Objects.requireNonNull(event.getOption(CONTENT_OPTION)).getAsString(), - previousContent); - if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.DELETE) - logDeleteAction(event, id, previousContent); + Guild guild = Objects.requireNonNull(event.getGuild()); + logAction(subcommand, guild, event.getUser(), event.getTimeCreated(), id, newContent, + previousContent); } /** @@ -242,10 +250,13 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, * created, will be displayed in the message send to the user * @param event the event to send messages with, it must have an {@code id} and * {@code message-id} option set + * @param subcommand the subcommand executed */ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, @NotNull BiConsumer idAndContentAction, - @NotNull String actionVerb, @NotNull CommandInteraction event) { + @NotNull String actionVerb, @NotNull CommandInteraction event, + @NotNull Subcommand subcommand) { + String tagId = Objects.requireNonNull(event.getOption(ID_OPTION)).getAsString(); OptionalLong messageIdOpt = parseMessageIdAndHandle( Objects.requireNonNull(event.getOption(MESSAGE_ID_OPTION)).getAsString(), event); @@ -258,18 +269,17 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } event.getMessageChannel().retrieveMessageById(messageId).queue(message -> { - String previousContent = ""; - if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT_WITH_MESSAGE) { + String previousContent = null; + if (subcommand == Subcommand.EDIT_WITH_MESSAGE) { previousContent = tagSystem.getTag(tagId).orElseThrow(); } idAndContentAction.accept(tagId, message.getContentRaw()); sendSuccessMessage(event, tagId, actionVerb); - if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.CREATE_WITH_MESSAGE) - logCreateAction(event, tagId, message.getContentRaw()); - if (Subcommand.fromName(event.getSubcommandName()) == Subcommand.EDIT_WITH_MESSAGE) - logEditAction(event, tagId, message.getContentRaw(), previousContent); + Guild guild = Objects.requireNonNull(event.getGuild()); + logAction(subcommand, guild, event.getUser(), event.getTimeCreated(), tagId, + message.getContentRaw(), previousContent); }, failure -> { if (failure instanceof ErrorResponseException ex @@ -317,60 +327,50 @@ private boolean isWrongTagStatusAndHandle(@NotNull TagStatus requiredTagStatus, return false; } - private void logCreateAction(@NotNull CommandInteraction event, @NotNull String id, - @NotNull String content) { - Guild guild = Objects.requireNonNull(event.getGuild()); + private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, + @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull String id, + @Nullable String newContentNullable, @Nullable String previousContentNullable) { - switch (Subcommand.fromName(event.getSubcommandName())) { + Optional newContent = + (newContentNullable == null) ? Optional.empty() : Optional.of(newContentNullable); + Optional previousContent = (previousContentNullable == null) ? Optional.empty() + : Optional.of(previousContentNullable); + + switch (subcommand) { case CREATE -> ModAuditLogWriter.log("Tag-Manage Create", - String.format("created tag **%s**", id), event.getUser(), - event.getTimeCreated(), guild, new ModAuditLogWriter.Attachment[] { - new ModAuditLogWriter.Attachment(Filename.CONTENT.get(), content)}); + String.format("created tag **%s**", id), author, timestamp, guild, + new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( + Filename.CONTENT.get(), newContent.orElseThrow())}); case CREATE_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Create with message", - String.format("created tag **%s**", id), event.getUser(), - event.getTimeCreated(), guild, new ModAuditLogWriter.Attachment[] { - new ModAuditLogWriter.Attachment(Filename.CONTENT.get(), content)}); - - default -> throw new IllegalArgumentException("Subcommand Enum invalid"); - } - } - - private void logEditAction(@NotNull CommandInteraction event, @NotNull String id, - @NotNull String newContent, @NotNull String previousContent) { - Guild guild = Objects.requireNonNull(event.getGuild()); + String.format("created tag **%s**", id), author, timestamp, guild, + new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( + Filename.CONTENT.get(), newContent.orElseThrow())}); - switch (Subcommand.fromName(event.getSubcommandName())) { case EDIT -> ModAuditLogWriter.log("Tag-Manage Edit", - String.format("edited tag **%s**", id), event.getUser(), event.getTimeCreated(), - guild, + String.format("edited tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] { new ModAuditLogWriter.Attachment(Filename.NEW_CONTENT.get(), - newContent), + newContent.orElseThrow()), new ModAuditLogWriter.Attachment(Filename.PREVIOUS_CONTENT.get(), - previousContent)}); + previousContent.orElseThrow())}); case EDIT_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Edit with message", - String.format("edited tag **%s**", id), event.getUser(), event.getTimeCreated(), - guild, + String.format("edited tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] { new ModAuditLogWriter.Attachment(Filename.NEW_CONTENT.get(), - newContent), + newContent.orElseThrow()), new ModAuditLogWriter.Attachment(Filename.PREVIOUS_CONTENT.get(), - previousContent)}); + previousContent.orElseThrow())}); - default -> throw new IllegalArgumentException("Subcommand Enum invalid"); - } - } - - private void logDeleteAction(@NotNull CommandInteraction event, @NotNull String id, - @NotNull String previousContent) { - Guild guild = Objects.requireNonNull(event.getGuild()); + case DELETE -> ModAuditLogWriter.log("Tag-Manage Delete", + String.format("delete tag **%s**", id), author, timestamp, guild, + new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( + Filename.PREVIOUS_CONTENT.get(), previousContent.orElseThrow())}); - ModAuditLogWriter.log("Tag-Manage Delete", String.format("delete tag **%s**", id), - event.getUser(), event.getTimeCreated(), guild, - new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - Filename.PREVIOUS_CONTENT.get(), previousContent)}); + default -> throw new IllegalArgumentException("Shouldn't log subcommand " + subcommand); + // in case the subcommand is "raw" or something else + } } private enum Filename { From 123aa43985dc4b540f3399f0f8bdd3b2ce467b06 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Sun, 5 Dec 2021 20:26:57 +0200 Subject: [PATCH 15/77] now using `Optional#orElseThrow()` instead of `Optional#get()` --- .../org/togetherjava/tjbot/moderation/ModAuditLogWriter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 18caea0ff7..6783b9809c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -59,7 +59,7 @@ public static void log(@NotNull String title, @NotNull String description, @NotN return; } - MessageAction message = auditLogChannel.get() + MessageAction message = auditLogChannel.orElseThrow() .sendMessageEmbeds(new EmbedBuilder().setTitle(title) .setDescription(description) .setAuthor(author.getAsTag(), null, author.getAvatarUrl()) From d5438e6f16653ad36817e066940044e1ed0690d7 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Sun, 5 Dec 2021 22:06:03 +0200 Subject: [PATCH 16/77] changed error message `"Shouldn't log subcommand"` to `"The subcommand '%s' is not intended to be logged to the mod audit channel."` --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 221da9d57c..d6dbf83c53 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -368,8 +368,9 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( Filename.PREVIOUS_CONTENT.get(), previousContent.orElseThrow())}); - default -> throw new IllegalArgumentException("Shouldn't log subcommand " + subcommand); - // in case the subcommand is "raw" or something else + default -> throw new IllegalArgumentException(String.format( + "The subcommand '%s' is not intended to be logged to the mod audit channel.", + subcommand.name())); } } From c4e752b2b939e6b6d56f011989ab3428163581e6 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Sun, 5 Dec 2021 22:26:56 +0200 Subject: [PATCH 17/77] made arrays `@NotNull` --- .../org/togetherjava/tjbot/moderation/ModAuditLogWriter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 6783b9809c..4ad5e6b959 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -50,7 +50,7 @@ public enum ModAuditLogWriter { */ public static void log(@NotNull String title, @NotNull String description, @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, - @NotNull Attachment[] files) { + @NotNull Attachment @NotNull [] files) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { logger.warn( @@ -116,7 +116,7 @@ public static final record Attachment(@NotNull String name, @NotNull String cont /** * @return the content of the file as a {@code byte[]}. used by JDA methods */ - public byte[] getContent() { + public byte @NotNull [] getContent() { return content.getBytes(StandardCharsets.UTF_8); } } From e94a720a574031ac99f662c30aea9d6f427cd456 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Tue, 7 Dec 2021 16:04:19 +0200 Subject: [PATCH 18/77] replaced `Filename` enum with constants --- .../tjbot/commands/tags/TagManageCommand.java | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index d6dbf83c53..933e3ed73f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -57,6 +57,11 @@ public final class TagManageCommand extends SlashCommandAdapter { private static final String CONTENT_DESCRIPTION = "the content of the tag"; static final String MESSAGE_ID_OPTION = "message-id"; private static final String MESSAGE_ID_DESCRIPTION = "the id of the message to refer to"; + + private static final String CONTENT_FILE_NAME = "content.md"; + private static final String NEW_CONTENT_FILE_NAME = "new_content.md"; + private static final String PREVIOUS_CONTENT_FILE_NAME = "previous_content.md"; + private final TagSystem tagSystem; private final Predicate hasRequiredRole; @@ -340,33 +345,33 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, case CREATE -> ModAuditLogWriter.log("Tag-Manage Create", String.format("created tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - Filename.CONTENT.get(), newContent.orElseThrow())}); + CONTENT_FILE_NAME, newContent.orElseThrow())}); case CREATE_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Create with message", String.format("created tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - Filename.CONTENT.get(), newContent.orElseThrow())}); + CONTENT_FILE_NAME, newContent.orElseThrow())}); case EDIT -> ModAuditLogWriter.log("Tag-Manage Edit", String.format("edited tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] { - new ModAuditLogWriter.Attachment(Filename.NEW_CONTENT.get(), + new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, newContent.orElseThrow()), - new ModAuditLogWriter.Attachment(Filename.PREVIOUS_CONTENT.get(), + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, previousContent.orElseThrow())}); case EDIT_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Edit with message", String.format("edited tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] { - new ModAuditLogWriter.Attachment(Filename.NEW_CONTENT.get(), + new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, newContent.orElseThrow()), - new ModAuditLogWriter.Attachment(Filename.PREVIOUS_CONTENT.get(), + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, previousContent.orElseThrow())}); case DELETE -> ModAuditLogWriter.log("Tag-Manage Delete", String.format("delete tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - Filename.PREVIOUS_CONTENT.get(), previousContent.orElseThrow())}); + PREVIOUS_CONTENT_FILE_NAME, previousContent.orElseThrow())}); default -> throw new IllegalArgumentException(String.format( "The subcommand '%s' is not intended to be logged to the mod audit channel.", @@ -374,22 +379,6 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, } } - private enum Filename { - CONTENT("content.md"), - PREVIOUS_CONTENT("previous_content.md"), - NEW_CONTENT("new_content.md"); - - private final String name; - - Filename(String name) { - this.name = name; - } - - private String get() { - return name; - } - } - private boolean hasTagManageRole(@NotNull Member member) { return member.getRoles().stream().map(Role::getName).anyMatch(hasRequiredRole); } From 3c9658e4ad69131a3a233fb333720a0b4eb8bf87 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Tue, 7 Dec 2021 16:51:22 +0200 Subject: [PATCH 19/77] now checking for `null` instead of abusing Optionals --- .../tjbot/commands/tags/TagManageCommand.java | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 933e3ed73f..35654fce66 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -23,6 +23,7 @@ import org.togetherjava.tjbot.moderation.ModAuditLogWriter; import java.time.temporal.TemporalAccessor; +import java.util.List; import java.awt.*; import java.nio.charset.StandardCharsets; import java.time.Instant; @@ -334,44 +335,62 @@ private boolean isWrongTagStatusAndHandle(@NotNull TagStatus requiredTagStatus, private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull String id, - @Nullable String newContentNullable, @Nullable String previousContentNullable) { + @Nullable String newContent, @Nullable String previousContent) { + + if (List.of( + Subcommand.CREATE, + Subcommand.CREATE_WITH_MESSAGE, + Subcommand.EDIT, + Subcommand.EDIT_WITH_MESSAGE + ).contains(subcommand) && newContent == null) { + logger.debug("newContent is null even though the subcommand should supply a value."); + return; + } + + if (List.of( + Subcommand.EDIT, + Subcommand.EDIT_WITH_MESSAGE, + Subcommand.DELETE + ).contains(subcommand) && previousContent == null) { + logger.debug("previousContent is null even though the subcommand should supply a value."); + return; + } - Optional newContent = - (newContentNullable == null) ? Optional.empty() : Optional.of(newContentNullable); - Optional previousContent = (previousContentNullable == null) ? Optional.empty() - : Optional.of(previousContentNullable); + //to suppress warning "Argument '' might be null" + if (newContent == null) newContent = ""; + if (previousContent == null) previousContent = ""; switch (subcommand) { case CREATE -> ModAuditLogWriter.log("Tag-Manage Create", String.format("created tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - CONTENT_FILE_NAME, newContent.orElseThrow())}); + CONTENT_FILE_NAME, newContent)}); case CREATE_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Create with message", String.format("created tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - CONTENT_FILE_NAME, newContent.orElseThrow())}); + CONTENT_FILE_NAME, newContent)}); case EDIT -> ModAuditLogWriter.log("Tag-Manage Edit", String.format("edited tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] { new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, - newContent.orElseThrow()), + newContent), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - previousContent.orElseThrow())}); + previousContent)}); case EDIT_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Edit with message", String.format("edited tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] { new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, - newContent.orElseThrow()), + newContent), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - previousContent.orElseThrow())}); + previousContent)}); case DELETE -> ModAuditLogWriter.log("Tag-Manage Delete", String.format("delete tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - PREVIOUS_CONTENT_FILE_NAME, previousContent.orElseThrow())}); + PREVIOUS_CONTENT_FILE_NAME, previousContent)}); default -> throw new IllegalArgumentException(String.format( "The subcommand '%s' is not intended to be logged to the mod audit channel.", From 81b43b362af541e143e67cae3b79cf95b54b9943 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Tue, 7 Dec 2021 17:05:16 +0200 Subject: [PATCH 20/77] `ModAuditLogWriter#log` now takes the attachments as a List instead of an array --- .../tjbot/commands/tags/TagManageCommand.java | 23 ++++--------- .../tjbot/moderation/ModAuditLogWriter.java | 34 +++++++++++++++---- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 35654fce66..d7e7b9118f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -363,34 +363,25 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, switch (subcommand) { case CREATE -> ModAuditLogWriter.log("Tag-Manage Create", String.format("created tag **%s**", id), author, timestamp, guild, - new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - CONTENT_FILE_NAME, newContent)}); + new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, newContent)); case CREATE_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Create with message", String.format("created tag **%s**", id), author, timestamp, guild, - new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - CONTENT_FILE_NAME, newContent)}); + new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, newContent)); case EDIT -> ModAuditLogWriter.log("Tag-Manage Edit", String.format("edited tag **%s**", id), author, timestamp, guild, - new ModAuditLogWriter.Attachment[] { - new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, - newContent), - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - previousContent)}); + List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, newContent), + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, previousContent))); case EDIT_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Edit with message", String.format("edited tag **%s**", id), author, timestamp, guild, - new ModAuditLogWriter.Attachment[] { - new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, - newContent), - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - previousContent)}); + List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, newContent), + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, previousContent))); case DELETE -> ModAuditLogWriter.log("Tag-Manage Delete", String.format("delete tag **%s**", id), author, timestamp, guild, - new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - PREVIOUS_CONTENT_FILE_NAME, previousContent)}); + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, previousContent)); default -> throw new IllegalArgumentException(String.format( "The subcommand '%s' is not intended to be logged to the mod audit channel.", diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 4ad5e6b959..f8ac7d1ef2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -14,6 +14,7 @@ import java.awt.*; import java.nio.charset.StandardCharsets; import java.time.temporal.TemporalAccessor; +import java.util.List; import java.util.Optional; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -46,11 +47,11 @@ public enum ModAuditLogWriter { * * @param guild the guild. usually use {@link GenericInteractionCreateEvent#getGuild()} * - * @param files the files that'll be added to the message + * @param attachments the attachments that'll be added to the message */ public static void log(@NotNull String title, @NotNull String description, @NotNull User author, - @NotNull TemporalAccessor timestamp, @NotNull Guild guild, - @NotNull Attachment @NotNull [] files) { + @NotNull TemporalAccessor timestamp, @NotNull Guild guild, + @NotNull List<@NotNull Attachment> attachments) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { logger.warn( @@ -67,12 +68,33 @@ public static void log(@NotNull String title, @NotNull String description, @NotN .setColor(EMBED_COLOR) .build()); - for (Attachment file : files) { - message = message.addFile(file.getContent(), file.getName()); + for (Attachment attachment : attachments) { + message = message.addFile(attachment.getContent(), attachment.getName()); } message.queue(); } + /** + * logs an entry in the mod audit log channel. + * + * @param title the title of the log embed + * + * @param description the description of the log embed + * + * @param author the user who triggered the action + * + * @param timestamp the timestamp of the action. usually use + * {@link GenericInteractionCreateEvent#getTimeCreated()} + * + * @param guild the guild. usually use {@link GenericInteractionCreateEvent#getGuild()} + * + * @param attachment an attachment that'll be added to the message + */ + public static void log(@NotNull String title, @NotNull String description, @NotNull User author, + @NotNull TemporalAccessor timestamp, @NotNull Guild guild, @NotNull Attachment attachment) { + log(title, description, author, timestamp, guild, List.of(attachment)); + } + /** * logs an entry in the mod audit log channel. * @@ -89,7 +111,7 @@ public static void log(@NotNull String title, @NotNull String description, @NotN */ public static void log(@NotNull String title, @NotNull String description, @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild) { - log(title, description, author, timestamp, guild, new Attachment[] {}); + log(title, description, author, timestamp, guild, List.of()); } /** From e9e4a25bfdd41783443091ef1e64c925e0541f25 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Tue, 7 Dec 2021 17:10:09 +0200 Subject: [PATCH 21/77] updated a minor javadoc --- .../org/togetherjava/tjbot/commands/tags/TagManageCommand.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index d7e7b9118f..346b5e75c1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -215,8 +215,7 @@ private void deleteTag(@NotNull CommandInteraction event) { * created, will be displayed in the message send to the user * @param event the event to send messages with, it must have an {@code id} option set * @param subcommand the executed subcommand - * @param newContent the new content of the tag (@{@link Nullable}: only if assigning new - * content) + * @param newContent the new content of the tag, or null if content is unchanged */ private void handleAction(@NotNull TagStatus requiredTagStatus, @NotNull Consumer idAction, @NotNull String actionVerb, From 35411af542882bde3acfeab7e164881cd50a5ae3 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Tue, 7 Dec 2021 17:24:13 +0200 Subject: [PATCH 22/77] renamed `ModAuditLogWriter#log()` to `writeModAuditLog()` --- .../tjbot/commands/tags/TagManageCommand.java | 52 ++++++++++--------- .../tjbot/moderation/ModAuditLogWriter.java | 19 +++---- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 346b5e75c1..3a30a80f3a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -336,49 +336,53 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull String id, @Nullable String newContent, @Nullable String previousContent) { - if (List.of( - Subcommand.CREATE, - Subcommand.CREATE_WITH_MESSAGE, - Subcommand.EDIT, - Subcommand.EDIT_WITH_MESSAGE - ).contains(subcommand) && newContent == null) { + if (List + .of(Subcommand.CREATE, Subcommand.CREATE_WITH_MESSAGE, Subcommand.EDIT, + Subcommand.EDIT_WITH_MESSAGE) + .contains(subcommand) && newContent == null) { logger.debug("newContent is null even though the subcommand should supply a value."); return; } - if (List.of( - Subcommand.EDIT, - Subcommand.EDIT_WITH_MESSAGE, - Subcommand.DELETE - ).contains(subcommand) && previousContent == null) { - logger.debug("previousContent is null even though the subcommand should supply a value."); + if (List.of(Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE, Subcommand.DELETE) + .contains(subcommand) && previousContent == null) { + logger + .debug("previousContent is null even though the subcommand should supply a value."); return; } - //to suppress warning "Argument '' might be null" - if (newContent == null) newContent = ""; - if (previousContent == null) previousContent = ""; + // to suppress warning "Argument '' might be null" + if (newContent == null) { + newContent = ""; + } + if (previousContent == null) { + previousContent = ""; + } switch (subcommand) { - case CREATE -> ModAuditLogWriter.log("Tag-Manage Create", + case CREATE -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Create", String.format("created tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, newContent)); - case CREATE_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Create with message", - String.format("created tag **%s**", id), author, timestamp, guild, + case CREATE_WITH_MESSAGE -> ModAuditLogWriter.writeModAuditLog( + "Tag-Manage Create with message", String.format("created tag **%s**", id), + author, timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, newContent)); - case EDIT -> ModAuditLogWriter.log("Tag-Manage Edit", + case EDIT -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Edit", String.format("edited tag **%s**", id), author, timestamp, guild, List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, newContent), - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, previousContent))); + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, + previousContent))); - case EDIT_WITH_MESSAGE -> ModAuditLogWriter.log("Tag-Manage Edit with message", - String.format("edited tag **%s**", id), author, timestamp, guild, + case EDIT_WITH_MESSAGE -> ModAuditLogWriter.writeModAuditLog( + "Tag-Manage Edit with message", String.format("edited tag **%s**", id), author, + timestamp, guild, List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, newContent), - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, previousContent))); + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, + previousContent))); - case DELETE -> ModAuditLogWriter.log("Tag-Manage Delete", + case DELETE -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Delete", String.format("delete tag **%s**", id), author, timestamp, guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, previousContent)); diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index f8ac7d1ef2..002f5e7b09 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -49,9 +49,9 @@ public enum ModAuditLogWriter { * * @param attachments the attachments that'll be added to the message */ - public static void log(@NotNull String title, @NotNull String description, @NotNull User author, - @NotNull TemporalAccessor timestamp, @NotNull Guild guild, - @NotNull List<@NotNull Attachment> attachments) { + public static void writeModAuditLog(@NotNull String title, @NotNull String description, + @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, + @NotNull List<@NotNull Attachment> attachments) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { logger.warn( @@ -90,9 +90,10 @@ public static void log(@NotNull String title, @NotNull String description, @NotN * * @param attachment an attachment that'll be added to the message */ - public static void log(@NotNull String title, @NotNull String description, @NotNull User author, - @NotNull TemporalAccessor timestamp, @NotNull Guild guild, @NotNull Attachment attachment) { - log(title, description, author, timestamp, guild, List.of(attachment)); + public static void writeModAuditLog(@NotNull String title, @NotNull String description, + @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, + @NotNull Attachment attachment) { + writeModAuditLog(title, description, author, timestamp, guild, List.of(attachment)); } /** @@ -109,9 +110,9 @@ public static void log(@NotNull String title, @NotNull String description, @NotN * * @param guild the guild. usually use {@link GenericInteractionCreateEvent#getGuild()} */ - public static void log(@NotNull String title, @NotNull String description, @NotNull User author, - @NotNull TemporalAccessor timestamp, @NotNull Guild guild) { - log(title, description, author, timestamp, guild, List.of()); + public static void writeModAuditLog(@NotNull String title, @NotNull String description, + @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild) { + writeModAuditLog(title, description, author, timestamp, guild, List.of()); } /** From b46205140f50cb0171ccad7457b052b65dbd27a4 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Tue, 7 Dec 2021 18:15:52 +0200 Subject: [PATCH 23/77] changed the embed color from `#4FC3F7` to `#3788AC` --- .../org/togetherjava/tjbot/moderation/ModAuditLogWriter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 002f5e7b09..5528025656 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -24,7 +24,7 @@ */ public enum ModAuditLogWriter { ; - private static final Color EMBED_COLOR = Color.decode("#4FC3F7"); + private static final Color EMBED_COLOR = Color.decode("#3788AC"); private static final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class); From d0f3340d94b18137c3b4286f09f7391a9f98b1ca Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Tue, 7 Dec 2021 18:45:02 +0200 Subject: [PATCH 24/77] removed duplicate `getModAuditLogChannel()` removed from `ModAuditLogRoutine`, made the one in `ModAuditLogWriter` public and completed its javadoc --- .../tjbot/moderation/ModAuditLogWriter.java | 6 ++++-- .../tjbot/routines/ModAuditLogRoutine.java | 12 +++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 5528025656..b2272c35db 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -116,9 +116,11 @@ public static void writeModAuditLog(@NotNull String title, @NotNull String descr } /** - * stolen from {@link org.togetherjava.tjbot.routines.ModAuditLogRoutine} + * used to get the mod audit log channel. + * + * @param guild the current guild */ - private static Optional getModAuditLogChannel(@NotNull Guild guild) { + public static Optional getModAuditLogChannel(@NotNull Guild guild) { return guild.getTextChannelCache().stream().filter(isAuditLogChannel).findAny(); } diff --git a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java index e3beb603dd..3ecde0fde8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java @@ -26,6 +26,7 @@ import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAccessor; import java.util.List; +import java.util.concurrent.Executors; import java.util.*; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -34,6 +35,8 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; +import static org.togetherjava.tjbot.moderation.ModAuditLogWriter.getModAuditLogChannel; + /** * Routine that automatically checks moderator actions on a schedule and logs them to dedicated * channels. @@ -317,15 +320,6 @@ private boolean containsMutedRole(@NotNull AuditLogEntry entry, @NotNull AuditLo .anyMatch(ModerationUtils.getIsMutedRolePredicate(config)); } - private Optional getModAuditLogChannel(@NotNull Guild guild) { - // Check cache first, then get full list - return guild.getTextChannelCache() - .stream() - .filter(isAuditLogChannel) - .findAny() - .or(() -> guild.getTextChannels().stream().filter(isAuditLogChannel).findAny()); - } - private enum Action { BAN("Ban", "banned"), UNBAN("Unban", "unbanned"), From 3e3a7b6fcbdf29eeddd3f90aa0d52c75f42e46d5 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Tue, 7 Dec 2021 18:57:48 +0200 Subject: [PATCH 25/77] now ignores `NoSuchElementException` when retrieving the previous content of a tag --- .../tjbot/commands/tags/TagManageCommand.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 3a30a80f3a..2fdf86803f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -24,6 +24,7 @@ import java.time.temporal.TemporalAccessor; import java.util.List; +import java.util.NoSuchElementException; import java.awt.*; import java.nio.charset.StandardCharsets; import java.time.Instant; @@ -229,7 +230,12 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, String previousContent = null; if (subcommand == Subcommand.EDIT || subcommand == Subcommand.DELETE) { - previousContent = tagSystem.getTag(id).orElseThrow(); + try { + previousContent = tagSystem.getTag(id).orElseThrow(); + } catch (NoSuchElementException exception) { + logger.debug( + "tried to retrieve previous content of tag '%s', but the content doesn't exist."); + } } idAction.accept(id); @@ -276,7 +282,12 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, event.getMessageChannel().retrieveMessageById(messageId).queue(message -> { String previousContent = null; if (subcommand == Subcommand.EDIT_WITH_MESSAGE) { - previousContent = tagSystem.getTag(tagId).orElseThrow(); + try { + previousContent = tagSystem.getTag(tagId).orElseThrow(); + } catch (NoSuchElementException exception) { + logger.debug( + "tried to retrieve previous content of tag '%s', but the content doesn't exist."); + } } idAndContentAction.accept(tagId, message.getContentRaw()); From 70c680c9916aac23e3c0c847fbfda5222bbc44f2 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Tue, 7 Dec 2021 20:55:15 +0200 Subject: [PATCH 26/77] `actionVerb` is now a field in `Subcommand` enum instead of a parameter in the subcommand methods --- .../tjbot/commands/tags/TagManageCommand.java | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 2fdf86803f..94292329a3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -178,30 +178,29 @@ private void rawTag(@NotNull SlashCommandEvent event) { private void createTag(@NotNull CommandInteraction event) { String content = Objects.requireNonNull(event.getOption(CONTENT_OPTION)).getAsString(); - handleAction(TagStatus.NOT_EXISTS, id -> tagSystem.putTag(id, content), "created", event, + handleAction(TagStatus.NOT_EXISTS, id -> tagSystem.putTag(id, content), event, Subcommand.CREATE, content); } private void createTagWithMessage(@NotNull CommandInteraction event) { - handleActionWithMessage(TagStatus.NOT_EXISTS, tagSystem::putTag, "created", event, + handleActionWithMessage(TagStatus.NOT_EXISTS, tagSystem::putTag, event, Subcommand.CREATE_WITH_MESSAGE); } private void editTag(@NotNull CommandInteraction event) { String content = Objects.requireNonNull(event.getOption(CONTENT_OPTION)).getAsString(); - handleAction(TagStatus.EXISTS, id -> tagSystem.putTag(id, content), "edited", event, - Subcommand.EDIT, content); + handleAction(TagStatus.EXISTS, id -> tagSystem.putTag(id, content), event, Subcommand.EDIT, + content); } private void editTagWithMessage(@NotNull CommandInteraction event) { - handleActionWithMessage(TagStatus.EXISTS, tagSystem::putTag, "edited", event, + handleActionWithMessage(TagStatus.EXISTS, tagSystem::putTag, event, Subcommand.EDIT_WITH_MESSAGE); } private void deleteTag(@NotNull CommandInteraction event) { - handleAction(TagStatus.EXISTS, tagSystem::deleteTag, "deleted", event, Subcommand.DELETE, - null); + handleAction(TagStatus.EXISTS, tagSystem::deleteTag, event, Subcommand.DELETE, null); } /** @@ -212,16 +211,13 @@ private void deleteTag(@NotNull CommandInteraction event) { * * @param requiredTagStatus the required status of the tag * @param idAction the action to perform on the id - * @param actionVerb the verb describing the executed action, i.e. edited or - * created, will be displayed in the message send to the user * @param event the event to send messages with, it must have an {@code id} option set * @param subcommand the executed subcommand * @param newContent the new content of the tag, or null if content is unchanged */ private void handleAction(@NotNull TagStatus requiredTagStatus, - @NotNull Consumer idAction, @NotNull String actionVerb, - @NotNull CommandInteraction event, @NotNull Subcommand subcommand, - @Nullable String newContent) { + @NotNull Consumer idAction, @NotNull CommandInteraction event, + @NotNull Subcommand subcommand, @Nullable String newContent) { String id = Objects.requireNonNull(event.getOption(ID_OPTION)).getAsString(); if (isWrongTagStatusAndHandle(requiredTagStatus, id, event)) { @@ -239,7 +235,7 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, } idAction.accept(id); - sendSuccessMessage(event, id, actionVerb); + sendSuccessMessage(event, id, subcommand.getActionVerb()); Guild guild = Objects.requireNonNull(event.getGuild()); logAction(subcommand, guild, event.getUser(), event.getTimeCreated(), id, newContent, @@ -257,16 +253,13 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, * * @param requiredTagStatus the required status of the tag * @param idAndContentAction the action to perform on the id and content - * @param actionVerb the verb describing the executed action, i.e. edited or - * created, will be displayed in the message send to the user * @param event the event to send messages with, it must have an {@code id} and * {@code message-id} option set * @param subcommand the subcommand executed */ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, @NotNull BiConsumer idAndContentAction, - @NotNull String actionVerb, @NotNull CommandInteraction event, - @NotNull Subcommand subcommand) { + @NotNull CommandInteraction event, @NotNull Subcommand subcommand) { String tagId = Objects.requireNonNull(event.getOption(ID_OPTION)).getAsString(); OptionalLong messageIdOpt = parseMessageIdAndHandle( @@ -291,7 +284,7 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } idAndContentAction.accept(tagId, message.getContentRaw()); - sendSuccessMessage(event, tagId, actionVerb); + sendSuccessMessage(event, tagId, subcommand.getActionVerb()); Guild guild = Objects.requireNonNull(event.getGuild()); logAction(subcommand, guild, event.getUser(), event.getTimeCreated(), tagId, @@ -415,16 +408,23 @@ private enum TagStatus { enum Subcommand { RAW("raw"), - CREATE("create"), - CREATE_WITH_MESSAGE("create-with-message"), - EDIT("edit"), - EDIT_WITH_MESSAGE("edit-with-message"), - DELETE("delete"); + CREATE("create", "created"), + CREATE_WITH_MESSAGE("create-with-message", "created"), + EDIT("edit", "edited"), + EDIT_WITH_MESSAGE("edit-with-message", "edited"), + DELETE("delete", "deleted"); private final String name; + private final String actionVerb; Subcommand(@NotNull String name) { this.name = name; + this.actionVerb = ""; + } + + Subcommand(String name, String actionVerb) { + this.name = name; + this.actionVerb = actionVerb; } @NotNull @@ -441,5 +441,9 @@ static Subcommand fromName(@NotNull String name) { throw new IllegalArgumentException( "Subcommand with name '%s' is unknown".formatted(name)); } + + String getActionVerb() { + return this.actionVerb; + } } } From 64aaab4a5b0ac97b7ab6994438d8fa377605ec8b Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Tue, 7 Dec 2021 21:32:53 +0200 Subject: [PATCH 27/77] removed unused field and var that are left from https://github.com/Together-Java/TJ-Bot/pull/296/commits/29483e8d3da0fccf50b40d8bdc575e2d1c2f9c86 --- .../tjbot/routines/ModAuditLogRoutine.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java index 3ecde0fde8..53b8ebc819 100644 --- a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java @@ -26,8 +26,9 @@ import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAccessor; import java.util.List; -import java.util.concurrent.Executors; import java.util.*; +import java.util.List; +import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.function.BiFunction; @@ -52,8 +53,6 @@ public final class ModAuditLogRoutine implements Routine { private static final int HOURS_OF_DAY = 24; private static final Color AMBIENT_COLOR = Color.decode("#4FC3F7"); - private final String modAuditLogChannelPattern; - private final Predicate isAuditLogChannel; private final Database database; private final Config config; @@ -64,11 +63,6 @@ public final class ModAuditLogRoutine implements Routine { * @param config the config to use for this */ public ModAuditLogRoutine(@NotNull Database database, @NotNull Config config) { - modAuditLogChannelPattern = config.getModAuditLogChannelPattern(); - Predicate isAuditLogChannelName = - Pattern.compile(modAuditLogChannelPattern).asMatchPredicate(); - isAuditLogChannel = channel -> isAuditLogChannelName.test(channel.getName()); - this.config = config; this.database = database; } From 91a968db50914dbb40d88f386e447587fd9a68d0 Mon Sep 17 00:00:00 2001 From: MaiTheLord Date: Tue, 7 Dec 2021 21:43:42 +0200 Subject: [PATCH 28/77] moved duplicate debug message into the common method that causes it --- .../tjbot/moderation/ModAuditLogWriter.java | 12 ++++++++---- .../tjbot/routines/ModAuditLogRoutine.java | 3 --- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index b2272c35db..85fc6b6901 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -54,9 +54,6 @@ public static void writeModAuditLog(@NotNull String title, @NotNull String descr @NotNull List<@NotNull Attachment> attachments) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { - logger.warn( - "Unable to log moderation events, did not find a mod audit log channel matching the configured pattern '{}' for guild '{}'", - Config.getInstance().getModAuditLogChannelPattern(), guild.getName()); return; } @@ -121,7 +118,14 @@ public static void writeModAuditLog(@NotNull String title, @NotNull String descr * @param guild the current guild */ public static Optional getModAuditLogChannel(@NotNull Guild guild) { - return guild.getTextChannelCache().stream().filter(isAuditLogChannel).findAny(); + Optional channel = + guild.getTextChannelCache().stream().filter(isAuditLogChannel).findAny(); + if (channel.isEmpty()) { + logger.warn( + "Unable to log moderation events, did not find a mod audit log channel matching the configured pattern '{}' for guild '{}'", + Config.getInstance().getModAuditLogChannelPattern(), guild.getName()); + } + return channel; } /** diff --git a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java index 53b8ebc819..77f0f160a4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java @@ -220,9 +220,6 @@ private void checkAuditLogsRoutine(@NotNull JDA jda) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { - logger.warn( - "Unable to log moderation events, did not find a mod audit log channel matching the configured pattern '{}' for guild '{}'", - modAuditLogChannelPattern, guild.getName()); return; } From 6f6a342ed583e914ce745be139221885ffba169e Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 24 Dec 2021 07:04:52 +0200 Subject: [PATCH 29/77] removed static import --- .../org/togetherjava/tjbot/routines/ModAuditLogRoutine.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java index 77f0f160a4..5c21fd9671 100644 --- a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java @@ -36,7 +36,7 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; -import static org.togetherjava.tjbot.moderation.ModAuditLogWriter.getModAuditLogChannel; +import org.togetherjava.tjbot.moderation.ModAuditLogWriter; /** * Routine that automatically checks moderator actions on a schedule and logs them to dedicated @@ -218,7 +218,7 @@ private void checkAuditLogsRoutine(@NotNull JDA jda) { return; } - Optional auditLogChannel = getModAuditLogChannel(guild); + Optional auditLogChannel = ModAuditLogWriter.getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { return; } From ae123c596491973cf4cbfa3b39490d127ea52cd4 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Thu, 20 Jan 2022 20:01:20 +0200 Subject: [PATCH 30/77] Improved JavaDoc. --- .../tjbot/commands/tags/TagManageCommand.java | 4 +- .../tjbot/moderation/ModAuditLogWriter.java | 41 +++++++++---------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 94292329a3..f2c21e929e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -212,7 +212,7 @@ private void deleteTag(@NotNull CommandInteraction event) { * @param requiredTagStatus the required status of the tag * @param idAction the action to perform on the id * @param event the event to send messages with, it must have an {@code id} option set - * @param subcommand the executed subcommand + * @param subcommand the subcommand to be executed * @param newContent the new content of the tag, or null if content is unchanged */ private void handleAction(@NotNull TagStatus requiredTagStatus, @@ -255,7 +255,7 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, * @param idAndContentAction the action to perform on the id and content * @param event the event to send messages with, it must have an {@code id} and * {@code message-id} option set - * @param subcommand the subcommand executed + * @param subcommand the subcommand to be executed */ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, @NotNull BiConsumer idAndContentAction, diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 85fc6b6901..075277e9f3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -20,7 +20,8 @@ import java.util.regex.Pattern; /** - * used to log activities in the mod audit log channel + * Utility class that allows you to easily log an entry on the mod audit log channel. + * Thread-Safe. */ public enum ModAuditLogWriter { ; @@ -34,18 +35,17 @@ public enum ModAuditLogWriter { channel -> isAuditLogChannelName.test(channel.getName()); /** - * logs an entry in the mod audit log channel. + * Sends a log embed on the mod audit log channel. * * @param title the title of the log embed * * @param description the description of the log embed * - * @param author the user who triggered the action + * @param author the user to be added to the embed * - * @param timestamp the timestamp of the action. usually use - * {@link GenericInteractionCreateEvent#getTimeCreated()} + * @param timestamp the timestamp to be added to the embed * - * @param guild the guild. usually use {@link GenericInteractionCreateEvent#getGuild()} + * @param guild the guild to write this log to * * @param attachments the attachments that'll be added to the message */ @@ -72,18 +72,17 @@ public static void writeModAuditLog(@NotNull String title, @NotNull String descr } /** - * logs an entry in the mod audit log channel. + * Sends a log embed on the mod audit log channel. * * @param title the title of the log embed * * @param description the description of the log embed * - * @param author the user who triggered the action + * @param author the user to be added to the embed * - * @param timestamp the timestamp of the action. usually use - * {@link GenericInteractionCreateEvent#getTimeCreated()} + * @param timestamp the timestamp to be added to the embed * - * @param guild the guild. usually use {@link GenericInteractionCreateEvent#getGuild()} + * @param guild the guild to write this log to * * @param attachment an attachment that'll be added to the message */ @@ -94,18 +93,17 @@ public static void writeModAuditLog(@NotNull String title, @NotNull String descr } /** - * logs an entry in the mod audit log channel. + * Sends a log embed on the mod audit log channel. * * @param title the title of the log embed * * @param description the description of the log embed * - * @param author the user who triggered the action + * @param author the user to be added to the embed * - * @param timestamp the timestamp of the action. usually use - * {@link GenericInteractionCreateEvent#getTimeCreated()} + * @param timestamp the timestamp to be added to the embed * - * @param guild the guild. usually use {@link GenericInteractionCreateEvent#getGuild()} + * @param guild the guild to write this log to */ public static void writeModAuditLog(@NotNull String title, @NotNull String description, @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild) { @@ -113,9 +111,10 @@ public static void writeModAuditLog(@NotNull String title, @NotNull String descr } /** - * used to get the mod audit log channel. + * Gets the channel used for moderation audit logs, if present. + * If the channel doesn't exist, this method will return an empty optional, and a warning message will be written. * - * @param guild the current guild + * @param guild the guild to look for the channel in */ public static Optional getModAuditLogChannel(@NotNull Guild guild) { Optional channel = @@ -129,10 +128,10 @@ public static Optional getModAuditLogChannel(@NotNull Guild guild) } /** - * used to add a file to a message without having an actual file. + * Represents attachment to messages, as for example used by {@link MessageAction#addFile(File, String, AttachmentOption...)}. * - * @param name the name of the file, example: {@code "foo.md"} - * @param content the content of the file + * @param name the name of the attachment, example: {@code "foo.md"} + * @param content the content of the attachment */ public static final record Attachment(@NotNull String name, @NotNull String content) { /** From b23e7f556336136dc726f335d0bf35591f6f0940 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 16:50:56 +0200 Subject: [PATCH 31/77] Added missing imports --- .../tjbot/moderation/ModAuditLogWriter.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 075277e9f3..473ba83617 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -4,14 +4,15 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent; import net.dv8tion.jda.api.requests.restaction.MessageAction; +import net.dv8tion.jda.api.utils.AttachmentOption; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.config.Config; import java.awt.*; +import java.io.File; import java.nio.charset.StandardCharsets; import java.time.temporal.TemporalAccessor; import java.util.List; @@ -20,8 +21,7 @@ import java.util.regex.Pattern; /** - * Utility class that allows you to easily log an entry on the mod audit log channel. - * Thread-Safe. + * Utility class that allows you to easily log an entry on the mod audit log channel. Thread-Safe. */ public enum ModAuditLogWriter { ; @@ -111,8 +111,8 @@ public static void writeModAuditLog(@NotNull String title, @NotNull String descr } /** - * Gets the channel used for moderation audit logs, if present. - * If the channel doesn't exist, this method will return an empty optional, and a warning message will be written. + * Gets the channel used for moderation audit logs, if present. If the channel doesn't exist, + * this method will return an empty optional, and a warning message will be written. * * @param guild the guild to look for the channel in */ @@ -128,7 +128,8 @@ public static Optional getModAuditLogChannel(@NotNull Guild guild) } /** - * Represents attachment to messages, as for example used by {@link MessageAction#addFile(File, String, AttachmentOption...)}. + * Represents attachment to messages, as for example used by + * {@link MessageAction#addFile(File, String, AttachmentOption...)}. * * @param name the name of the attachment, example: {@code "foo.md"} * @param content the content of the attachment From 54c9dafddce819b7aacc4b6423172879823bef49 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 16:52:17 +0200 Subject: [PATCH 32/77] removed `static final` from `record` --- .../org/togetherjava/tjbot/moderation/ModAuditLogWriter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 473ba83617..72b52ee75d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -134,7 +134,7 @@ public static Optional getModAuditLogChannel(@NotNull Guild guild) * @param name the name of the attachment, example: {@code "foo.md"} * @param content the content of the attachment */ - public static final record Attachment(@NotNull String name, @NotNull String content) { + public record Attachment(@NotNull String name, @NotNull String content) { /** * @return the name of the file. used by JDA methods */ From 071fc9ba277bbba30ad750c03b2260190401cba4 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 17:06:24 +0200 Subject: [PATCH 33/77] Slightly updated previous content retrieval code --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index f2c21e929e..0473f6ad72 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -224,11 +224,12 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, return; } - String previousContent = null; + String previousContent = "Unable to retrieve previous content"; if (subcommand == Subcommand.EDIT || subcommand == Subcommand.DELETE) { try { previousContent = tagSystem.getTag(id).orElseThrow(); - } catch (NoSuchElementException exception) { + } catch (NoSuchElementException e) { + //NOTE Rare race condition, for example if another thread deleted the tag in the meantime logger.debug( "tried to retrieve previous content of tag '%s', but the content doesn't exist."); } From b494b75f7ba64afbdfc4152103515c1453745af6 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 17:11:04 +0200 Subject: [PATCH 34/77] now using `EnumSet` instead of `List` --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 0473f6ad72..1e57913518 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -23,6 +23,7 @@ import org.togetherjava.tjbot.moderation.ModAuditLogWriter; import java.time.temporal.TemporalAccessor; +import java.util.*; import java.util.List; import java.util.NoSuchElementException; import java.awt.*; @@ -341,7 +342,7 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull String id, @Nullable String newContent, @Nullable String previousContent) { - if (List + if (EnumSet .of(Subcommand.CREATE, Subcommand.CREATE_WITH_MESSAGE, Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE) .contains(subcommand) && newContent == null) { @@ -349,7 +350,7 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, return; } - if (List.of(Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE, Subcommand.DELETE) + if (EnumSet.of(Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE, Subcommand.DELETE) .contains(subcommand) && previousContent == null) { logger .debug("previousContent is null even though the subcommand should supply a value."); From 5bc61ffb4d81feddf6814058c92a5b166cca5134 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 17:16:08 +0200 Subject: [PATCH 35/77] made `logAction()` fail-fast --- .../tjbot/commands/tags/TagManageCommand.java | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 1e57913518..6d9428bb92 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -346,51 +346,40 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, .of(Subcommand.CREATE, Subcommand.CREATE_WITH_MESSAGE, Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE) .contains(subcommand) && newContent == null) { - logger.debug("newContent is null even though the subcommand should supply a value."); - return; + throw new IllegalArgumentException("newContent is null even though the subcommand should supply a value."); } if (EnumSet.of(Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE, Subcommand.DELETE) .contains(subcommand) && previousContent == null) { - logger - .debug("previousContent is null even though the subcommand should supply a value."); - return; - } - - // to suppress warning "Argument '' might be null" - if (newContent == null) { - newContent = ""; - } - if (previousContent == null) { - previousContent = ""; + throw new IllegalArgumentException("previousContent is null even though the subcommand should supply a value."); } switch (subcommand) { case CREATE -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Create", String.format("created tag **%s**", id), author, timestamp, guild, - new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, newContent)); + new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); case CREATE_WITH_MESSAGE -> ModAuditLogWriter.writeModAuditLog( "Tag-Manage Create with message", String.format("created tag **%s**", id), author, timestamp, guild, - new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, newContent)); + new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); case EDIT -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Edit", String.format("edited tag **%s**", id), author, timestamp, guild, - List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, newContent), + List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - previousContent))); + Objects.requireNonNull(previousContent)))); case EDIT_WITH_MESSAGE -> ModAuditLogWriter.writeModAuditLog( "Tag-Manage Edit with message", String.format("edited tag **%s**", id), author, timestamp, guild, - List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, newContent), + List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - previousContent))); + Objects.requireNonNull(previousContent)))); case DELETE -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Delete", String.format("delete tag **%s**", id), author, timestamp, guild, - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, previousContent)); + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent))); default -> throw new IllegalArgumentException(String.format( "The subcommand '%s' is not intended to be logged to the mod audit channel.", From 358057c95b528ec8155b9b1f0ce37f4bde8d7ef9 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 17:18:48 +0200 Subject: [PATCH 36/77] removed unnecessary constructor --- .../tjbot/commands/tags/TagManageCommand.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 6d9428bb92..0a441921b5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -398,7 +398,7 @@ private enum TagStatus { enum Subcommand { - RAW("raw"), + RAW("raw", ""), CREATE("create", "created"), CREATE_WITH_MESSAGE("create-with-message", "created"), EDIT("edit", "edited"), @@ -408,12 +408,7 @@ enum Subcommand { private final String name; private final String actionVerb; - Subcommand(@NotNull String name) { - this.name = name; - this.actionVerb = ""; - } - - Subcommand(String name, String actionVerb) { + Subcommand(@NotNull String name, @NotNull String actionVerb) { this.name = name; this.actionVerb = actionVerb; } From 4377d8971d7ee8e44eb8bb92a301b10c78a1395e Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 17:23:15 +0200 Subject: [PATCH 37/77] made `Subcommand` enum methods `@NotNull` --- .../org/togetherjava/tjbot/commands/tags/TagManageCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 0a441921b5..6e1f036162 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -428,7 +428,7 @@ static Subcommand fromName(@NotNull String name) { "Subcommand with name '%s' is unknown".formatted(name)); } - String getActionVerb() { + @NotNull String getActionVerb() { return this.actionVerb; } } From 6eb5898a9057134e4c2dc2b0a847b81637efe4ae Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 17:34:32 +0200 Subject: [PATCH 38/77] moved predicates directly into the variable declaration --- .../tjbot/moderation/ModAuditLogWriter.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 72b52ee75d..ca4418b75f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -29,11 +29,6 @@ public enum ModAuditLogWriter { private static final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class); - private static final Predicate isAuditLogChannelName = - Pattern.compile(Config.getInstance().getModAuditLogChannelPattern()).asMatchPredicate(); - private static final Predicate isAuditLogChannel = - channel -> isAuditLogChannelName.test(channel.getName()); - /** * Sends a log embed on the mod audit log channel. * @@ -117,8 +112,13 @@ public static void writeModAuditLog(@NotNull String title, @NotNull String descr * @param guild the guild to look for the channel in */ public static Optional getModAuditLogChannel(@NotNull Guild guild) { - Optional channel = - guild.getTextChannelCache().stream().filter(isAuditLogChannel).findAny(); + Optional channel = guild.getTextChannelCache() + .stream() + .filter(c -> Pattern.compile(Config.getInstance().getModAuditLogChannelPattern()) + .asMatchPredicate() + .test(c.getName())) + .findAny(); + if (channel.isEmpty()) { logger.warn( "Unable to log moderation events, did not find a mod audit log channel matching the configured pattern '{}' for guild '{}'", From e8a7f0bfb3e9d571a8a8b232fbb5ef15cc2d6d38 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 17:45:00 +0200 Subject: [PATCH 39/77] updated `Attachment` `record` --- .../tjbot/moderation/ModAuditLogWriter.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index ca4418b75f..48f82e948a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -61,7 +61,7 @@ public static void writeModAuditLog(@NotNull String title, @NotNull String descr .build()); for (Attachment attachment : attachments) { - message = message.addFile(attachment.getContent(), attachment.getName()); + message = message.addFile(attachment.getContentRaw(), attachment.name()); } message.queue(); } @@ -136,16 +136,11 @@ public static Optional getModAuditLogChannel(@NotNull Guild guild) */ public record Attachment(@NotNull String name, @NotNull String content) { /** - * @return the name of the file. used by JDA methods + * Gets the content raw, interpreted as UTF-8. + * + * @return the raw content of the file as a {@code byte[]}. */ - public @NotNull String getName() { - return name; - } - - /** - * @return the content of the file as a {@code byte[]}. used by JDA methods - */ - public byte @NotNull [] getContent() { + public byte @NotNull [] getContentRaw() { return content.getBytes(StandardCharsets.UTF_8); } } From 7c6823d3a0bf7a4e85342282526c9b434cabd090 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 18:07:24 +0200 Subject: [PATCH 40/77] moved the code that gets previous content into a helper method, getTagPreviousContent() --- .../tjbot/commands/tags/TagManageCommand.java | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 6e1f036162..c9b338c0b7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -225,16 +225,7 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, return; } - String previousContent = "Unable to retrieve previous content"; - if (subcommand == Subcommand.EDIT || subcommand == Subcommand.DELETE) { - try { - previousContent = tagSystem.getTag(id).orElseThrow(); - } catch (NoSuchElementException e) { - //NOTE Rare race condition, for example if another thread deleted the tag in the meantime - logger.debug( - "tried to retrieve previous content of tag '%s', but the content doesn't exist."); - } - } + String previousContent = getTagPreviousContent(subcommand, id); idAction.accept(id); sendSuccessMessage(event, id, subcommand.getActionVerb()); @@ -275,15 +266,7 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } event.getMessageChannel().retrieveMessageById(messageId).queue(message -> { - String previousContent = null; - if (subcommand == Subcommand.EDIT_WITH_MESSAGE) { - try { - previousContent = tagSystem.getTag(tagId).orElseThrow(); - } catch (NoSuchElementException exception) { - logger.debug( - "tried to retrieve previous content of tag '%s', but the content doesn't exist."); - } - } + String previousContent = getTagPreviousContent(subcommand, tagId); idAndContentAction.accept(tagId, message.getContentRaw()); sendSuccessMessage(event, tagId, subcommand.getActionVerb()); @@ -311,6 +294,25 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, }); } + /** + * Gets the previous content of a tag, or {@code "Unable to retrieve previous content"} if was unable to. + * @param subcommand the subcommand to be executed + * @param id the id of the tag to get its previous content + * @return the previous content of the tag, or {@code "Unable to retrieve previous content"} if was unable to + */ + private String getTagPreviousContent(Subcommand subcommand, String id) { + if (EnumSet.of(Subcommand.DELETE, Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE).contains(subcommand)) { + try { + return tagSystem.getTag(id).orElseThrow(); + } catch (NoSuchElementException e) { + //NOTE Rare race condition, for example if another thread deleted the tag in the meantime + logger.warn( + String.format("tried to retrieve previous content of tag '%s', but the content doesn't exist.", id)); + } + } + return "Unable to retrieve previous content"; + } + /** * Returns whether the status of the given tag is not equal to the required status. *

From a22143696cf88eb3d523e5ffe6a0085e91fdc5b1 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 18:11:38 +0200 Subject: [PATCH 41/77] now using `actionVerb` as the verb in the message --- .../tjbot/commands/tags/TagManageCommand.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index c9b338c0b7..9ef2fad001 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -358,29 +358,29 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, switch (subcommand) { case CREATE -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Create", - String.format("created tag **%s**", id), author, timestamp, guild, + String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); case CREATE_WITH_MESSAGE -> ModAuditLogWriter.writeModAuditLog( - "Tag-Manage Create with message", String.format("created tag **%s**", id), + "Tag-Manage Create with message", String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); case EDIT -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Edit", - String.format("edited tag **%s**", id), author, timestamp, guild, + String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent)))); case EDIT_WITH_MESSAGE -> ModAuditLogWriter.writeModAuditLog( - "Tag-Manage Edit with message", String.format("edited tag **%s**", id), author, + "Tag-Manage Edit with message", String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent)))); case DELETE -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Delete", - String.format("delete tag **%s**", id), author, timestamp, guild, + String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent))); default -> throw new IllegalArgumentException(String.format( From 24def3467c974d8b18ea463c865fb69fd9bd42f6 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 18:19:17 +0200 Subject: [PATCH 42/77] renamed `writeModAuditLog()` to `write` --- .../tjbot/commands/tags/TagManageCommand.java | 10 ++++----- .../tjbot/moderation/ModAuditLogWriter.java | 21 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 9ef2fad001..2127bec465 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -357,29 +357,29 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, } switch (subcommand) { - case CREATE -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Create", + case CREATE -> ModAuditLogWriter.write("Tag-Manage Create", String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); - case CREATE_WITH_MESSAGE -> ModAuditLogWriter.writeModAuditLog( + case CREATE_WITH_MESSAGE -> ModAuditLogWriter.write( "Tag-Manage Create with message", String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); - case EDIT -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Edit", + case EDIT -> ModAuditLogWriter.write("Tag-Manage Edit", String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent)))); - case EDIT_WITH_MESSAGE -> ModAuditLogWriter.writeModAuditLog( + case EDIT_WITH_MESSAGE -> ModAuditLogWriter.write( "Tag-Manage Edit with message", String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent)))); - case DELETE -> ModAuditLogWriter.writeModAuditLog("Tag-Manage Delete", + case DELETE -> ModAuditLogWriter.write("Tag-Manage Delete", String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent))); diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 48f82e948a..e542d87713 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -17,7 +17,6 @@ import java.time.temporal.TemporalAccessor; import java.util.List; import java.util.Optional; -import java.util.function.Predicate; import java.util.regex.Pattern; /** @@ -44,9 +43,9 @@ public enum ModAuditLogWriter { * * @param attachments the attachments that'll be added to the message */ - public static void writeModAuditLog(@NotNull String title, @NotNull String description, - @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, - @NotNull List<@NotNull Attachment> attachments) { + public static void write(@NotNull String title, @NotNull String description, + @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, + @NotNull List<@NotNull Attachment> attachments) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { return; @@ -81,10 +80,10 @@ public static void writeModAuditLog(@NotNull String title, @NotNull String descr * * @param attachment an attachment that'll be added to the message */ - public static void writeModAuditLog(@NotNull String title, @NotNull String description, - @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, - @NotNull Attachment attachment) { - writeModAuditLog(title, description, author, timestamp, guild, List.of(attachment)); + public static void write(@NotNull String title, @NotNull String description, + @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, + @NotNull Attachment attachment) { + write(title, description, author, timestamp, guild, List.of(attachment)); } /** @@ -100,9 +99,9 @@ public static void writeModAuditLog(@NotNull String title, @NotNull String descr * * @param guild the guild to write this log to */ - public static void writeModAuditLog(@NotNull String title, @NotNull String description, - @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild) { - writeModAuditLog(title, description, author, timestamp, guild, List.of()); + public static void write(@NotNull String title, @NotNull String description, + @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild) { + write(title, description, author, timestamp, guild, List.of()); } /** From 7c49c73fa7151d60da5d2b673d2d14b4280bd012 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 18:23:01 +0200 Subject: [PATCH 43/77] removed `write()` overloads and now getting attachments as a vararg --- .../tjbot/commands/tags/TagManageCommand.java | 8 ++-- .../tjbot/moderation/ModAuditLogWriter.java | 43 +------------------ 2 files changed, 6 insertions(+), 45 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 2127bec465..be6b6e6faa 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -368,16 +368,16 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, case EDIT -> ModAuditLogWriter.write("Tag-Manage Edit", String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, - List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), + new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - Objects.requireNonNull(previousContent)))); + Objects.requireNonNull(previousContent))); case EDIT_WITH_MESSAGE -> ModAuditLogWriter.write( "Tag-Manage Edit with message", String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, - List.of(new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), + new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - Objects.requireNonNull(previousContent)))); + Objects.requireNonNull(previousContent))); case DELETE -> ModAuditLogWriter.write("Tag-Manage Delete", String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index e542d87713..0142d62166 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -41,11 +41,11 @@ public enum ModAuditLogWriter { * * @param guild the guild to write this log to * - * @param attachments the attachments that'll be added to the message + * @param attachments attachments that'll be added to the message */ public static void write(@NotNull String title, @NotNull String description, @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, - @NotNull List<@NotNull Attachment> attachments) { + @NotNull Attachment... attachments) { Optional auditLogChannel = getModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { return; @@ -65,45 +65,6 @@ public static void write(@NotNull String title, @NotNull String description, message.queue(); } - /** - * Sends a log embed on the mod audit log channel. - * - * @param title the title of the log embed - * - * @param description the description of the log embed - * - * @param author the user to be added to the embed - * - * @param timestamp the timestamp to be added to the embed - * - * @param guild the guild to write this log to - * - * @param attachment an attachment that'll be added to the message - */ - public static void write(@NotNull String title, @NotNull String description, - @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, - @NotNull Attachment attachment) { - write(title, description, author, timestamp, guild, List.of(attachment)); - } - - /** - * Sends a log embed on the mod audit log channel. - * - * @param title the title of the log embed - * - * @param description the description of the log embed - * - * @param author the user to be added to the embed - * - * @param timestamp the timestamp to be added to the embed - * - * @param guild the guild to write this log to - */ - public static void write(@NotNull String title, @NotNull String description, - @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild) { - write(title, description, author, timestamp, guild, List.of()); - } - /** * Gets the channel used for moderation audit logs, if present. If the channel doesn't exist, * this method will return an empty optional, and a warning message will be written. From a9a43392d82a480e9e6777a4d79bf715269a482d Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 18:28:13 +0200 Subject: [PATCH 44/77] renamed `getModAuditLogChannel()` to `getAndHandleModAuditLogChannel()` --- .../org/togetherjava/tjbot/moderation/ModAuditLogWriter.java | 4 ++-- .../org/togetherjava/tjbot/routines/ModAuditLogRoutine.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 0142d62166..c9d469aa27 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -46,7 +46,7 @@ public enum ModAuditLogWriter { public static void write(@NotNull String title, @NotNull String description, @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, @NotNull Attachment... attachments) { - Optional auditLogChannel = getModAuditLogChannel(guild); + Optional auditLogChannel = getAndHandleModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { return; } @@ -71,7 +71,7 @@ public static void write(@NotNull String title, @NotNull String description, * * @param guild the guild to look for the channel in */ - public static Optional getModAuditLogChannel(@NotNull Guild guild) { + public static Optional getAndHandleModAuditLogChannel(@NotNull Guild guild) { Optional channel = guild.getTextChannelCache() .stream() .filter(c -> Pattern.compile(Config.getInstance().getModAuditLogChannelPattern()) diff --git a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java index 5c21fd9671..84e5e095f5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java @@ -218,7 +218,7 @@ private void checkAuditLogsRoutine(@NotNull JDA jda) { return; } - Optional auditLogChannel = ModAuditLogWriter.getModAuditLogChannel(guild); + Optional auditLogChannel = ModAuditLogWriter.getAndHandleModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { return; } From 81482cd6bad3998378d3a8eff6583ebb71bafa06 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 21 Jan 2022 18:32:56 +0200 Subject: [PATCH 45/77] fixed code-style. --- .../tjbot/commands/tags/TagManageCommand.java | 66 +++++++++++-------- .../tjbot/moderation/ModAuditLogWriter.java | 4 +- .../tjbot/routines/ModAuditLogRoutine.java | 3 +- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index be6b6e6faa..96581e46ec 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -295,19 +295,25 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } /** - * Gets the previous content of a tag, or {@code "Unable to retrieve previous content"} if was unable to. + * Gets the previous content of a tag, or {@code "Unable to retrieve previous content"} if was + * unable to. + * * @param subcommand the subcommand to be executed * @param id the id of the tag to get its previous content - * @return the previous content of the tag, or {@code "Unable to retrieve previous content"} if was unable to + * @return the previous content of the tag, or {@code "Unable to retrieve previous content"} if + * was unable to */ private String getTagPreviousContent(Subcommand subcommand, String id) { - if (EnumSet.of(Subcommand.DELETE, Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE).contains(subcommand)) { + if (EnumSet.of(Subcommand.DELETE, Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE) + .contains(subcommand)) { try { return tagSystem.getTag(id).orElseThrow(); } catch (NoSuchElementException e) { - //NOTE Rare race condition, for example if another thread deleted the tag in the meantime - logger.warn( - String.format("tried to retrieve previous content of tag '%s', but the content doesn't exist.", id)); + // NOTE Rare race condition, for example if another thread deleted the tag in the + // meantime + logger.warn(String.format( + "tried to retrieve previous content of tag '%s', but the content doesn't exist.", + id)); } } return "Unable to retrieve previous content"; @@ -348,40 +354,47 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, .of(Subcommand.CREATE, Subcommand.CREATE_WITH_MESSAGE, Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE) .contains(subcommand) && newContent == null) { - throw new IllegalArgumentException("newContent is null even though the subcommand should supply a value."); + throw new IllegalArgumentException( + "newContent is null even though the subcommand should supply a value."); } if (EnumSet.of(Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE, Subcommand.DELETE) .contains(subcommand) && previousContent == null) { - throw new IllegalArgumentException("previousContent is null even though the subcommand should supply a value."); + throw new IllegalArgumentException( + "previousContent is null even though the subcommand should supply a value."); } switch (subcommand) { case CREATE -> ModAuditLogWriter.write("Tag-Manage Create", - String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, - new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); + String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, + timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, + Objects.requireNonNull(newContent))); - case CREATE_WITH_MESSAGE -> ModAuditLogWriter.write( - "Tag-Manage Create with message", String.format("%s tag **%s**", subcommand.getActionVerb(), id), - author, timestamp, guild, - new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); + case CREATE_WITH_MESSAGE -> ModAuditLogWriter.write("Tag-Manage Create with message", + String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, + timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, + Objects.requireNonNull(newContent))); case EDIT -> ModAuditLogWriter.write("Tag-Manage Edit", - String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, - new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - Objects.requireNonNull(previousContent))); + String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, + timestamp, guild, + new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, + Objects.requireNonNull(newContent)), + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, + Objects.requireNonNull(previousContent))); - case EDIT_WITH_MESSAGE -> ModAuditLogWriter.write( - "Tag-Manage Edit with message", String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, + case EDIT_WITH_MESSAGE -> ModAuditLogWriter.write("Tag-Manage Edit with message", + String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, - new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - Objects.requireNonNull(previousContent))); + new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, + Objects.requireNonNull(newContent)), + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, + Objects.requireNonNull(previousContent))); case DELETE -> ModAuditLogWriter.write("Tag-Manage Delete", - String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, timestamp, guild, - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent))); + String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, + timestamp, guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, + Objects.requireNonNull(previousContent))); default -> throw new IllegalArgumentException(String.format( "The subcommand '%s' is not intended to be logged to the mod audit channel.", @@ -430,7 +443,8 @@ static Subcommand fromName(@NotNull String name) { "Subcommand with name '%s' is unknown".formatted(name)); } - @NotNull String getActionVerb() { + @NotNull + String getActionVerb() { return this.actionVerb; } } diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index c9d469aa27..84ade13cae 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -44,8 +44,8 @@ public enum ModAuditLogWriter { * @param attachments attachments that'll be added to the message */ public static void write(@NotNull String title, @NotNull String description, - @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, - @NotNull Attachment... attachments) { + @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, + @NotNull Attachment... attachments) { Optional auditLogChannel = getAndHandleModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { return; diff --git a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java index 84e5e095f5..dfb2b710f3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java @@ -218,7 +218,8 @@ private void checkAuditLogsRoutine(@NotNull JDA jda) { return; } - Optional auditLogChannel = ModAuditLogWriter.getAndHandleModAuditLogChannel(guild); + Optional auditLogChannel = + ModAuditLogWriter.getAndHandleModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { return; } From 1a8b799c4258f3e623565008ee55171c971721a4 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Wed, 26 Jan 2022 13:21:40 +0200 Subject: [PATCH 46/77] fixed a merge issue --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 6 ------ .../togetherjava/tjbot/moderation/ModAuditLogWriter.java | 1 - .../org/togetherjava/tjbot/routines/ModAuditLogRoutine.java | 2 -- 3 files changed, 9 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 96581e46ec..80b02d740a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -18,19 +18,15 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; -import org.togetherjava.tjbot.commands.utils.MessageUtils; import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.moderation.ModAuditLogWriter; import java.time.temporal.TemporalAccessor; import java.util.*; -import java.util.List; import java.util.NoSuchElementException; -import java.awt.*; import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.Objects; -import java.util.Optional; import java.util.OptionalLong; import java.util.function.BiConsumer; import java.util.function.Consumer; @@ -172,8 +168,6 @@ private void rawTag(@NotNull SlashCommandEvent event) { String content = tagSystem.getTag(id).orElseThrow(); event.reply("").addFile(content.getBytes(StandardCharsets.UTF_8), "content.md").queue(); - - logAction(event, id); } private void createTag(@NotNull CommandInteraction event) { diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 84ade13cae..2b01d0ca99 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -15,7 +15,6 @@ import java.io.File; import java.nio.charset.StandardCharsets; import java.time.temporal.TemporalAccessor; -import java.util.List; import java.util.Optional; import java.util.regex.Pattern; diff --git a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java index dfb2b710f3..1eca2f03ac 100644 --- a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java @@ -32,8 +32,6 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.function.BiFunction; -import java.util.function.Predicate; -import java.util.regex.Pattern; import java.util.stream.Collectors; import org.togetherjava.tjbot.moderation.ModAuditLogWriter; From 429d9b7cf7a3946c4b6f6363ac15da316b5c2e4c Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Thu, 27 Jan 2022 21:03:09 +0200 Subject: [PATCH 47/77] using constant `CONTENT_FILE_NAME` instead of `"content.md"` --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 80b02d740a..76e929fdc2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -167,7 +167,9 @@ private void rawTag(@NotNull SlashCommandEvent event) { } String content = tagSystem.getTag(id).orElseThrow(); - event.reply("").addFile(content.getBytes(StandardCharsets.UTF_8), "content.md").queue(); + event.reply("") + .addFile(content.getBytes(StandardCharsets.UTF_8), CONTENT_FILE_NAME) + .queue(); } private void createTag(@NotNull CommandInteraction event) { From d011b349ddad425de3eeb9bfa3707ad03b031d0a Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Thu, 27 Jan 2022 21:07:06 +0200 Subject: [PATCH 48/77] Now using a constant for the log embed description --- .../tjbot/commands/tags/TagManageCommand.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 76e929fdc2..7b74c1db9f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -57,6 +57,7 @@ public final class TagManageCommand extends SlashCommandAdapter { static final String MESSAGE_ID_OPTION = "message-id"; private static final String MESSAGE_ID_DESCRIPTION = "the id of the message to refer to"; + private static final String LOG_EMBED_DESCRIPTION = "%s tag **%s**"; //%s is formatted to the action verb. private static final String CONTENT_FILE_NAME = "content.md"; private static final String NEW_CONTENT_FILE_NAME = "new_content.md"; private static final String PREVIOUS_CONTENT_FILE_NAME = "previous_content.md"; @@ -362,17 +363,17 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, switch (subcommand) { case CREATE -> ModAuditLogWriter.write("Tag-Manage Create", - String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, + String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); case CREATE_WITH_MESSAGE -> ModAuditLogWriter.write("Tag-Manage Create with message", - String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, + String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); case EDIT -> ModAuditLogWriter.write("Tag-Manage Edit", - String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, + String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, timestamp, guild, new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), @@ -380,7 +381,7 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, Objects.requireNonNull(previousContent))); case EDIT_WITH_MESSAGE -> ModAuditLogWriter.write("Tag-Manage Edit with message", - String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, + String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, timestamp, guild, new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), @@ -388,7 +389,7 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, Objects.requireNonNull(previousContent))); case DELETE -> ModAuditLogWriter.write("Tag-Manage Delete", - String.format("%s tag **%s**", subcommand.getActionVerb(), id), author, + String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, timestamp, guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent))); From 3b9b8b99577afc8914e1999cf07f0c84fb64e81e Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Thu, 27 Jan 2022 21:08:46 +0200 Subject: [PATCH 49/77] code-style quick fix --- .../org/togetherjava/tjbot/commands/tags/TagManageCommand.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 7b74c1db9f..c69537ad56 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -57,7 +57,8 @@ public final class TagManageCommand extends SlashCommandAdapter { static final String MESSAGE_ID_OPTION = "message-id"; private static final String MESSAGE_ID_DESCRIPTION = "the id of the message to refer to"; - private static final String LOG_EMBED_DESCRIPTION = "%s tag **%s**"; //%s is formatted to the action verb. + private static final String LOG_EMBED_DESCRIPTION = "%s tag **%s**"; // %s is formatted to the + // action verb. private static final String CONTENT_FILE_NAME = "content.md"; private static final String NEW_CONTENT_FILE_NAME = "new_content.md"; private static final String PREVIOUS_CONTENT_FILE_NAME = "previous_content.md"; From 749db7b8ac92577f15fc68f3e9be6af00ea69d96 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Thu, 27 Jan 2022 21:35:10 +0200 Subject: [PATCH 50/77] moved comment to be above the constant, instead of in front of it. Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com> --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index c69537ad56..f5a390dde4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -57,8 +57,9 @@ public final class TagManageCommand extends SlashCommandAdapter { static final String MESSAGE_ID_OPTION = "message-id"; private static final String MESSAGE_ID_DESCRIPTION = "the id of the message to refer to"; - private static final String LOG_EMBED_DESCRIPTION = "%s tag **%s**"; // %s is formatted to the - // action verb. + // %s is formatted to the action verb. + private static final String LOG_EMBED_DESCRIPTION = "%s tag **%s**"; + private static final String CONTENT_FILE_NAME = "content.md"; private static final String NEW_CONTENT_FILE_NAME = "new_content.md"; private static final String PREVIOUS_CONTENT_FILE_NAME = "previous_content.md"; From 7d25c29adc98c906b32fbd05f2137e9e8f66cc24 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Thu, 27 Jan 2022 22:04:06 +0200 Subject: [PATCH 51/77] Put audit log channel name pattern in a field to prevent it from recompiling every time the method gets called. --- .../togetherjava/tjbot/moderation/ModAuditLogWriter.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 2b01d0ca99..4625aa8ac4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -27,6 +27,8 @@ public enum ModAuditLogWriter { private static final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class); + private static Pattern AUDIT_LOG_CHANNEL_NAME_PATTERN = null; + /** * Sends a log embed on the mod audit log channel. * @@ -71,9 +73,13 @@ public static void write(@NotNull String title, @NotNull String description, * @param guild the guild to look for the channel in */ public static Optional getAndHandleModAuditLogChannel(@NotNull Guild guild) { + if (AUDIT_LOG_CHANNEL_NAME_PATTERN == null) { + AUDIT_LOG_CHANNEL_NAME_PATTERN = Pattern.compile(Config.getInstance().getModAuditLogChannelPattern()); + } + Optional channel = guild.getTextChannelCache() .stream() - .filter(c -> Pattern.compile(Config.getInstance().getModAuditLogChannelPattern()) + .filter(c -> AUDIT_LOG_CHANNEL_NAME_PATTERN .asMatchPredicate() .test(c.getName())) .findAny(); From 97c4f322109fdb91115c94b96be95244ad97eb64 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Thu, 27 Jan 2022 22:20:43 +0200 Subject: [PATCH 52/77] renamed field. --- .../tjbot/moderation/ModAuditLogWriter.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 4625aa8ac4..cf32392889 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -27,7 +27,7 @@ public enum ModAuditLogWriter { private static final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class); - private static Pattern AUDIT_LOG_CHANNEL_NAME_PATTERN = null; + private static Pattern auditLogChannelNamePattern = null; /** * Sends a log embed on the mod audit log channel. @@ -73,15 +73,14 @@ public static void write(@NotNull String title, @NotNull String description, * @param guild the guild to look for the channel in */ public static Optional getAndHandleModAuditLogChannel(@NotNull Guild guild) { - if (AUDIT_LOG_CHANNEL_NAME_PATTERN == null) { - AUDIT_LOG_CHANNEL_NAME_PATTERN = Pattern.compile(Config.getInstance().getModAuditLogChannelPattern()); + if (auditLogChannelNamePattern == null) { + auditLogChannelNamePattern = + Pattern.compile(Config.getInstance().getModAuditLogChannelPattern()); } Optional channel = guild.getTextChannelCache() .stream() - .filter(c -> AUDIT_LOG_CHANNEL_NAME_PATTERN - .asMatchPredicate() - .test(c.getName())) + .filter(c -> auditLogChannelNamePattern.asMatchPredicate().test(c.getName())) .findAny(); if (channel.isEmpty()) { From 1cf7627a10afc198b47c52db0c0f29291034aa01 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 28 Jan 2022 06:19:45 +0200 Subject: [PATCH 53/77] spotless applied. --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index f5a390dde4..b64de8100b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -58,8 +58,8 @@ public final class TagManageCommand extends SlashCommandAdapter { private static final String MESSAGE_ID_DESCRIPTION = "the id of the message to refer to"; // %s is formatted to the action verb. - private static final String LOG_EMBED_DESCRIPTION = "%s tag **%s**"; - + private static final String LOG_EMBED_DESCRIPTION = "%s tag **%s**"; + private static final String CONTENT_FILE_NAME = "content.md"; private static final String NEW_CONTENT_FILE_NAME = "new_content.md"; private static final String PREVIOUS_CONTENT_FILE_NAME = "previous_content.md"; From ccac11275088d27e802119900576d9f1bff98d38 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Wed, 9 Feb 2022 21:55:19 +0200 Subject: [PATCH 54/77] Removed useless imports. --- .../org/togetherjava/tjbot/routines/ModAuditLogRoutine.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java index 1eca2f03ac..e729bfccac 100644 --- a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java @@ -27,8 +27,6 @@ import java.time.temporal.TemporalAccessor; import java.util.List; import java.util.*; -import java.util.List; -import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.function.BiFunction; From f00590a31c11d8ab141d2e408bbd948b64a1fa58 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Wed, 2 Mar 2022 13:05:49 +0200 Subject: [PATCH 55/77] Updated `getTagContent()` --- .../tjbot/commands/tags/TagManageCommand.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index b64de8100b..dbe4d9a5e6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -224,7 +224,7 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, return; } - String previousContent = getTagPreviousContent(subcommand, id); + String previousContent = getTagContent(subcommand, id); idAction.accept(id); sendSuccessMessage(event, id, subcommand.getActionVerb()); @@ -265,7 +265,7 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } event.getMessageChannel().retrieveMessageById(messageId).queue(message -> { - String previousContent = getTagPreviousContent(subcommand, tagId); + String previousContent = getTagContent(subcommand, tagId); idAndContentAction.accept(tagId, message.getContentRaw()); sendSuccessMessage(event, tagId, subcommand.getActionVerb()); @@ -294,17 +294,17 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } /** - * Gets the previous content of a tag, or {@code "Unable to retrieve previous content"} if was - * unable to. + * Gets the content of a tag, or {@code "Unable to retrieve previous content"} if was unable to. * * @param subcommand the subcommand to be executed - * @param id the id of the tag to get its previous content - * @return the previous content of the tag, or {@code "Unable to retrieve previous content"} if - * was unable to + * @param id the id of the tag to get its content + * @return the content of the tag, or {@code "Unable to retrieve previous content"} if was + * unable to */ - private String getTagPreviousContent(Subcommand subcommand, String id) { - if (EnumSet.of(Subcommand.DELETE, Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE) - .contains(subcommand)) { + private @NotNull String getTagContent(@NotNull Subcommand subcommand, @NotNull String id) { + Set subcommandsWithContent = + EnumSet.of(Subcommand.DELETE, Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE); + if (subcommandsWithContent.contains(subcommand)) { try { return tagSystem.getTag(id).orElseThrow(); } catch (NoSuchElementException e) { @@ -315,6 +315,7 @@ private String getTagPreviousContent(Subcommand subcommand, String id) { id)); } } + return "Unable to retrieve previous content"; } From e848ac084d0693ea95ea4264cc59b03f583499e1 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 07:07:17 +0200 Subject: [PATCH 56/77] Some code changes --- .../tjbot/commands/tags/TagManageCommand.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index dbe4d9a5e6..8b056c3025 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -57,13 +57,16 @@ public final class TagManageCommand extends SlashCommandAdapter { static final String MESSAGE_ID_OPTION = "message-id"; private static final String MESSAGE_ID_DESCRIPTION = "the id of the message to refer to"; - // %s is formatted to the action verb. + // "Edited tag **ask**" private static final String LOG_EMBED_DESCRIPTION = "%s tag **%s**"; private static final String CONTENT_FILE_NAME = "content.md"; private static final String NEW_CONTENT_FILE_NAME = "new_content.md"; private static final String PREVIOUS_CONTENT_FILE_NAME = "previous_content.md"; + private static final EnumSet SUBCOMMANDS_WITH_PREVIOUS_CONTENT = + EnumSet.of(Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE, Subcommand.DELETE); + private final TagSystem tagSystem; private final Predicate hasRequiredRole; @@ -302,9 +305,7 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, * unable to */ private @NotNull String getTagContent(@NotNull Subcommand subcommand, @NotNull String id) { - Set subcommandsWithContent = - EnumSet.of(Subcommand.DELETE, Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE); - if (subcommandsWithContent.contains(subcommand)) { + if (SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand)) { try { return tagSystem.getTag(id).orElseThrow(); } catch (NoSuchElementException e) { @@ -347,7 +348,7 @@ private boolean isWrongTagStatusAndHandle(@NotNull TagStatus requiredTagStatus, } private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, - @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull String id, + @NotNull User author, @NotNull TemporalAccessor triggeredAt, @NotNull String id, @Nullable String newContent, @Nullable String previousContent) { if (EnumSet @@ -358,8 +359,7 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, "newContent is null even though the subcommand should supply a value."); } - if (EnumSet.of(Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE, Subcommand.DELETE) - .contains(subcommand) && previousContent == null) { + if (SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand) && previousContent == null) { throw new IllegalArgumentException( "previousContent is null even though the subcommand should supply a value."); } @@ -367,17 +367,17 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, switch (subcommand) { case CREATE -> ModAuditLogWriter.write("Tag-Manage Create", String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, - timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, + triggeredAt, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); case CREATE_WITH_MESSAGE -> ModAuditLogWriter.write("Tag-Manage Create with message", String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, - timestamp, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, + triggeredAt, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); case EDIT -> ModAuditLogWriter.write("Tag-Manage Edit", String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, - timestamp, guild, + triggeredAt, guild, new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, @@ -385,7 +385,7 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, case EDIT_WITH_MESSAGE -> ModAuditLogWriter.write("Tag-Manage Edit with message", String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, - timestamp, guild, + triggeredAt, guild, new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, Objects.requireNonNull(newContent)), new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, @@ -393,7 +393,7 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, case DELETE -> ModAuditLogWriter.write("Tag-Manage Delete", String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, - timestamp, guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, + triggeredAt, guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent))); default -> throw new IllegalArgumentException(String.format( From 4d1f09a756103e1240231c57afe280a0dc644360 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 07:22:24 +0200 Subject: [PATCH 57/77] Updated `ModAuditLogWriter`'s javadoc --- .../tjbot/moderation/ModAuditLogWriter.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index cf32392889..c2dfebef7f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -20,6 +20,8 @@ /** * Utility class that allows you to easily log an entry on the mod audit log channel. Thread-Safe. + *

+ * Use {@link ModAuditLogWriter#write(String, String, User, TemporalAccessor, Guild, Attachment...)} to log an entry. */ public enum ModAuditLogWriter { ; @@ -30,19 +32,14 @@ public enum ModAuditLogWriter { private static Pattern auditLogChannelNamePattern = null; /** - * Sends a log embed on the mod audit log channel. + * Sends a log on the mod audit log channel. * * @param title the title of the log embed - * * @param description the description of the log embed - * - * @param author the user to be added to the embed - * - * @param timestamp the timestamp to be added to the embed - * + * @param author the author of the log message + * @param timestamp the timestamp of the log message * @param guild the guild to write this log to - * - * @param attachments attachments that'll be added to the message + * @param attachments attachments that will be added to the message. none or many. */ public static void write(@NotNull String title, @NotNull String description, @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, @@ -102,7 +99,7 @@ public record Attachment(@NotNull String name, @NotNull String content) { /** * Gets the content raw, interpreted as UTF-8. * - * @return the raw content of the file as a {@code byte[]}. + * @return the raw content of the attachment */ public byte @NotNull [] getContentRaw() { return content.getBytes(StandardCharsets.UTF_8); From 4d55f1f7afff195b6eeb7585f0b1d2d8a0375627 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 07:23:17 +0200 Subject: [PATCH 58/77] changed variable name --- .../togetherjava/tjbot/moderation/ModAuditLogWriter.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index c2dfebef7f..2660d79d63 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -75,17 +75,17 @@ public static Optional getAndHandleModAuditLogChannel(@NotNull Guil Pattern.compile(Config.getInstance().getModAuditLogChannelPattern()); } - Optional channel = guild.getTextChannelCache() + Optional auditLogChannel = guild.getTextChannelCache() .stream() - .filter(c -> auditLogChannelNamePattern.asMatchPredicate().test(c.getName())) + .filter(channel -> auditLogChannelNamePattern.asMatchPredicate().test(channel.getName())) .findAny(); - if (channel.isEmpty()) { + if (auditLogChannel.isEmpty()) { logger.warn( "Unable to log moderation events, did not find a mod audit log channel matching the configured pattern '{}' for guild '{}'", Config.getInstance().getModAuditLogChannelPattern(), guild.getName()); } - return channel; + return auditLogChannel; } /** From f8f66083c6160d8b766d212216121b00d0308d69 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 07:27:00 +0200 Subject: [PATCH 59/77] updated `getTagContent()` --- .../tjbot/commands/tags/TagManageCommand.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 8b056c3025..44a6ff2643 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -297,11 +297,11 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } /** - * Gets the content of a tag, or {@code "Unable to retrieve previous content"} if was unable to. + * Gets the content of a tag, or {@code "Unable to retrieve content"} if was unable to. * * @param subcommand the subcommand to be executed * @param id the id of the tag to get its content - * @return the content of the tag, or {@code "Unable to retrieve previous content"} if was + * @return the content of the tag, or {@code "Unable to retrieve content"} if was * unable to */ private @NotNull String getTagContent(@NotNull Subcommand subcommand, @NotNull String id) { @@ -312,12 +312,12 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, // NOTE Rare race condition, for example if another thread deleted the tag in the // meantime logger.warn(String.format( - "tried to retrieve previous content of tag '%s', but the content doesn't exist.", + "tried to retrieve content of tag '%s', but the content doesn't exist.", id)); } } - return "Unable to retrieve previous content"; + return "Unable to retrieve content"; } /** From 86b93e5da43486c5307a615b917b494ea4e3defc Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 07:29:53 +0200 Subject: [PATCH 60/77] Moved enum set into a variable. --- .../tjbot/commands/tags/TagManageCommand.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 44a6ff2643..c633514490 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -301,8 +301,7 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, * * @param subcommand the subcommand to be executed * @param id the id of the tag to get its content - * @return the content of the tag, or {@code "Unable to retrieve content"} if was - * unable to + * @return the content of the tag, or {@code "Unable to retrieve content"} if was unable to */ private @NotNull String getTagContent(@NotNull Subcommand subcommand, @NotNull String id) { if (SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand)) { @@ -351,10 +350,10 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, @NotNull User author, @NotNull TemporalAccessor triggeredAt, @NotNull String id, @Nullable String newContent, @Nullable String previousContent) { - if (EnumSet - .of(Subcommand.CREATE, Subcommand.CREATE_WITH_MESSAGE, Subcommand.EDIT, - Subcommand.EDIT_WITH_MESSAGE) - .contains(subcommand) && newContent == null) { + EnumSet subcommandsWithNewContent = EnumSet.of(Subcommand.CREATE, + Subcommand.CREATE_WITH_MESSAGE, Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE); + + if (subcommandsWithNewContent.contains(subcommand) && newContent == null) { throw new IllegalArgumentException( "newContent is null even though the subcommand should supply a value."); } From 2521bcb187cb15a90d8076eb0c6d6717c9471302 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 07:30:55 +0200 Subject: [PATCH 61/77] code reformat --- .../tjbot/commands/tags/TagManageCommand.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index c633514490..f2c2331109 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -301,7 +301,8 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, * * @param subcommand the subcommand to be executed * @param id the id of the tag to get its content - * @return the content of the tag, or {@code "Unable to retrieve content"} if was unable to + * @return the content of the tag, or {@code "Unable to retrieve content"} if was + * unable to */ private @NotNull String getTagContent(@NotNull Subcommand subcommand, @NotNull String id) { if (SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand)) { @@ -350,10 +351,12 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, @NotNull User author, @NotNull TemporalAccessor triggeredAt, @NotNull String id, @Nullable String newContent, @Nullable String previousContent) { - EnumSet subcommandsWithNewContent = EnumSet.of(Subcommand.CREATE, - Subcommand.CREATE_WITH_MESSAGE, Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE); + EnumSet subcommandsWithNewContent = EnumSet.of( + Subcommand.CREATE, Subcommand.CREATE_WITH_MESSAGE, Subcommand.EDIT, + Subcommand.EDIT_WITH_MESSAGE); - if (subcommandsWithNewContent.contains(subcommand) && newContent == null) { + if (subcommandsWithNewContent + .contains(subcommand) && newContent == null) { throw new IllegalArgumentException( "newContent is null even though the subcommand should supply a value."); } From 1a67ae9c513a741497694bfcd3f96f9db38d9113 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 07:30:55 +0200 Subject: [PATCH 62/77] code reformat --- .../togetherjava/tjbot/moderation/ModAuditLogWriter.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 2660d79d63..41f5d830e4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -20,8 +20,10 @@ /** * Utility class that allows you to easily log an entry on the mod audit log channel. Thread-Safe. - *

- * Use {@link ModAuditLogWriter#write(String, String, User, TemporalAccessor, Guild, Attachment...)} to log an entry. + *
+ *
+ * Use {@link ModAuditLogWriter#write(String, String, User, TemporalAccessor, Guild, Attachment...)} + * to log an entry. */ public enum ModAuditLogWriter { ; @@ -77,7 +79,8 @@ public static Optional getAndHandleModAuditLogChannel(@NotNull Guil Optional auditLogChannel = guild.getTextChannelCache() .stream() - .filter(channel -> auditLogChannelNamePattern.asMatchPredicate().test(channel.getName())) + .filter(channel -> auditLogChannelNamePattern.asMatchPredicate() + .test(channel.getName())) .findAny(); if (auditLogChannel.isEmpty()) { From f03607744b53a5d07d5c2fe59d42ee31326f0361 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 07:43:10 +0200 Subject: [PATCH 63/77] moved field and var into `Subcommand` --- .../tjbot/commands/tags/TagManageCommand.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index f2c2331109..58b9a1b43c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -64,9 +64,6 @@ public final class TagManageCommand extends SlashCommandAdapter { private static final String NEW_CONTENT_FILE_NAME = "new_content.md"; private static final String PREVIOUS_CONTENT_FILE_NAME = "previous_content.md"; - private static final EnumSet SUBCOMMANDS_WITH_PREVIOUS_CONTENT = - EnumSet.of(Subcommand.EDIT, Subcommand.EDIT_WITH_MESSAGE, Subcommand.DELETE); - private final TagSystem tagSystem; private final Predicate hasRequiredRole; @@ -301,11 +298,10 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, * * @param subcommand the subcommand to be executed * @param id the id of the tag to get its content - * @return the content of the tag, or {@code "Unable to retrieve content"} if was - * unable to + * @return the content of the tag, or {@code "Unable to retrieve content"} if was unable to */ private @NotNull String getTagContent(@NotNull Subcommand subcommand, @NotNull String id) { - if (SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand)) { + if (Subcommand.SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand)) { try { return tagSystem.getTag(id).orElseThrow(); } catch (NoSuchElementException e) { @@ -351,17 +347,13 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, @NotNull User author, @NotNull TemporalAccessor triggeredAt, @NotNull String id, @Nullable String newContent, @Nullable String previousContent) { - EnumSet subcommandsWithNewContent = EnumSet.of( - Subcommand.CREATE, Subcommand.CREATE_WITH_MESSAGE, Subcommand.EDIT, - Subcommand.EDIT_WITH_MESSAGE); - - if (subcommandsWithNewContent - .contains(subcommand) && newContent == null) { + if (Subcommand.SUBCOMMANDS_WITH_NEW_CONTENT.contains(subcommand) && newContent == null) { throw new IllegalArgumentException( "newContent is null even though the subcommand should supply a value."); } - if (SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand) && previousContent == null) { + if (Subcommand.SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand) + && previousContent == null) { throw new IllegalArgumentException( "previousContent is null even though the subcommand should supply a value."); } @@ -422,6 +414,12 @@ enum Subcommand { EDIT_WITH_MESSAGE("edit-with-message", "edited"), DELETE("delete", "deleted"); + public static final Set SUBCOMMANDS_WITH_NEW_CONTENT = + EnumSet.of(CREATE, CREATE_WITH_MESSAGE, EDIT, EDIT_WITH_MESSAGE); + public static final Set SUBCOMMANDS_WITH_PREVIOUS_CONTENT = + EnumSet.of(EDIT, EDIT_WITH_MESSAGE, DELETE); + + private final String name; private final String actionVerb; From 7b101c634f1af4f3b8fe847e53fc05fc1b8f6bca Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 14:14:11 +0200 Subject: [PATCH 64/77] made fields `private` --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 58b9a1b43c..40df03f164 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -414,9 +414,9 @@ enum Subcommand { EDIT_WITH_MESSAGE("edit-with-message", "edited"), DELETE("delete", "deleted"); - public static final Set SUBCOMMANDS_WITH_NEW_CONTENT = + private static final Set SUBCOMMANDS_WITH_NEW_CONTENT = EnumSet.of(CREATE, CREATE_WITH_MESSAGE, EDIT, EDIT_WITH_MESSAGE); - public static final Set SUBCOMMANDS_WITH_PREVIOUS_CONTENT = + private static final Set SUBCOMMANDS_WITH_PREVIOUS_CONTENT = EnumSet.of(EDIT, EDIT_WITH_MESSAGE, DELETE); From 4b20e3c66f95eefd26e40a31223673063bd233b5 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 14:49:44 +0200 Subject: [PATCH 65/77] removed unnecessary `this.` --- .../org/togetherjava/tjbot/commands/tags/TagManageCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 40df03f164..5730876745 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -445,7 +445,7 @@ static Subcommand fromName(@NotNull String name) { @NotNull String getActionVerb() { - return this.actionVerb; + return actionVerb; } } } From da0b17773b4ca0adb90cefcde97506e17941e63e Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 15:26:01 +0200 Subject: [PATCH 66/77] Rebased and made `ModAuditLogWriter` singleton --- .../togetherjava/tjbot/commands/Features.java | 6 ++-- .../tjbot/commands/tags/TagManageCommand.java | 18 ++++++++---- .../tjbot/moderation/ModAuditLogWriter.java | 29 ++++++++++++------- .../tjbot/routines/ModAuditLogRoutine.java | 8 +++-- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java index 49105e33da..904c831439 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -19,6 +19,7 @@ import org.togetherjava.tjbot.commands.tophelper.TopHelpersPurgeMessagesRoutine; import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.db.Database; +import org.togetherjava.tjbot.moderation.ModAuditLogWriter; import org.togetherjava.tjbot.routines.ModAuditLogRoutine; import java.util.ArrayList; @@ -50,6 +51,7 @@ public enum Features { @NotNull Database database, @NotNull Config config) { TagSystem tagSystem = new TagSystem(database); ModerationActionsStore actionsStore = new ModerationActionsStore(database); + ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config); // NOTE The system can add special system relevant commands also by itself, // hence this list may not necessarily represent the full list of all commands actually @@ -57,7 +59,7 @@ public enum Features { Collection features = new ArrayList<>(); // Routines - features.add(new ModAuditLogRoutine(database, config)); + features.add(new ModAuditLogRoutine(database, config, modAuditLogWriter)); features.add(new TemporaryModerationRoutine(jda, actionsStore, config)); features.add(new TopHelpersPurgeMessagesRoutine(database)); features.add(new RemindRoutine(database)); @@ -73,7 +75,7 @@ public enum Features { features.add(new PingCommand()); features.add(new TeXCommand()); features.add(new TagCommand(tagSystem)); - features.add(new TagManageCommand(tagSystem, config)); + features.add(new TagManageCommand(tagSystem, config, modAuditLogWriter)); features.add(new TagsCommand(tagSystem)); features.add(new VcActivityCommand()); features.add(new WarnCommand(actionsStore, config)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 5730876745..c328bb360e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -67,18 +67,24 @@ public final class TagManageCommand extends SlashCommandAdapter { private final TagSystem tagSystem; private final Predicate hasRequiredRole; + private final ModAuditLogWriter modAuditLogWriter; + /** * Creates a new instance, using the given tag system as base. * * @param tagSystem the system providing the actual tag data * @param config the config to use for this + * @param modAuditLogWriter the mod audit log writer to use for this */ - public TagManageCommand(TagSystem tagSystem, @NotNull Config config) { + public TagManageCommand(TagSystem tagSystem, @NotNull Config config, + @NotNull ModAuditLogWriter modAuditLogWriter) { super("tag-manage", "Provides commands to manage all tags", SlashCommandVisibility.GUILD); this.tagSystem = tagSystem; hasRequiredRole = Pattern.compile(config.getTagManageRolePattern()).asMatchPredicate(); + this.modAuditLogWriter = modAuditLogWriter; + // TODO Think about adding a "Are you sure"-dialog to 'edit', 'edit-with-message' and // 'delete' getData().addSubcommands(new SubcommandData(Subcommand.RAW.name, @@ -359,17 +365,17 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, } switch (subcommand) { - case CREATE -> ModAuditLogWriter.write("Tag-Manage Create", + case CREATE -> modAuditLogWriter.write("Tag-Manage Create", String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, triggeredAt, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); - case CREATE_WITH_MESSAGE -> ModAuditLogWriter.write("Tag-Manage Create with message", + case CREATE_WITH_MESSAGE -> modAuditLogWriter.write("Tag-Manage Create with message", String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, triggeredAt, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, Objects.requireNonNull(newContent))); - case EDIT -> ModAuditLogWriter.write("Tag-Manage Edit", + case EDIT -> modAuditLogWriter.write("Tag-Manage Edit", String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, triggeredAt, guild, new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, @@ -377,7 +383,7 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent))); - case EDIT_WITH_MESSAGE -> ModAuditLogWriter.write("Tag-Manage Edit with message", + case EDIT_WITH_MESSAGE -> modAuditLogWriter.write("Tag-Manage Edit with message", String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, triggeredAt, guild, new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, @@ -385,7 +391,7 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent))); - case DELETE -> ModAuditLogWriter.write("Tag-Manage Delete", + case DELETE -> modAuditLogWriter.write("Tag-Manage Delete", String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, triggeredAt, guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, Objects.requireNonNull(previousContent))); diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 41f5d830e4..910bd4cdb9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -25,13 +25,23 @@ * Use {@link ModAuditLogWriter#write(String, String, User, TemporalAccessor, Guild, Attachment...)} * to log an entry. */ -public enum ModAuditLogWriter { - ; +public final class ModAuditLogWriter { private static final Color EMBED_COLOR = Color.decode("#3788AC"); - private static final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class); + private final Config config; - private static Pattern auditLogChannelNamePattern = null; + private final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class); + + private Pattern auditLogChannelNamePattern = null; + + /** + * Creates a new instance. + * + * @param config the config to use for this + */ + public ModAuditLogWriter(@NotNull Config config) { + this.config = config; + } /** * Sends a log on the mod audit log channel. @@ -43,8 +53,8 @@ public enum ModAuditLogWriter { * @param guild the guild to write this log to * @param attachments attachments that will be added to the message. none or many. */ - public static void write(@NotNull String title, @NotNull String description, - @NotNull User author, @NotNull TemporalAccessor timestamp, @NotNull Guild guild, + public void write(@NotNull String title, @NotNull String description, @NotNull User author, + @NotNull TemporalAccessor timestamp, @NotNull Guild guild, @NotNull Attachment... attachments) { Optional auditLogChannel = getAndHandleModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { @@ -71,10 +81,9 @@ public static void write(@NotNull String title, @NotNull String description, * * @param guild the guild to look for the channel in */ - public static Optional getAndHandleModAuditLogChannel(@NotNull Guild guild) { + public Optional getAndHandleModAuditLogChannel(@NotNull Guild guild) { if (auditLogChannelNamePattern == null) { - auditLogChannelNamePattern = - Pattern.compile(Config.getInstance().getModAuditLogChannelPattern()); + auditLogChannelNamePattern = Pattern.compile(config.getModAuditLogChannelPattern()); } Optional auditLogChannel = guild.getTextChannelCache() @@ -86,7 +95,7 @@ public static Optional getAndHandleModAuditLogChannel(@NotNull Guil if (auditLogChannel.isEmpty()) { logger.warn( "Unable to log moderation events, did not find a mod audit log channel matching the configured pattern '{}' for guild '{}'", - Config.getInstance().getModAuditLogChannelPattern(), guild.getName()); + config.getModAuditLogChannelPattern(), guild.getName()); } return auditLogChannel; } diff --git a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java index e729bfccac..a194faa4db 100644 --- a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java @@ -51,16 +51,20 @@ public final class ModAuditLogRoutine implements Routine { private final Database database; private final Config config; + private final ModAuditLogWriter modAuditLogWriter; /** * Creates a new instance. * * @param database the database for memorizing audit log dates * @param config the config to use for this + * @param modAuditLogWriter the mod audit log writer to use for this */ - public ModAuditLogRoutine(@NotNull Database database, @NotNull Config config) { + public ModAuditLogRoutine(@NotNull Database database, @NotNull Config config, + @NotNull ModAuditLogWriter modAuditLogWriter) { this.config = config; this.database = database; + this.modAuditLogWriter = modAuditLogWriter; } private static @NotNull RestAction handleAction(@NotNull Action action, @@ -215,7 +219,7 @@ private void checkAuditLogsRoutine(@NotNull JDA jda) { } Optional auditLogChannel = - ModAuditLogWriter.getAndHandleModAuditLogChannel(guild); + modAuditLogWriter.getAndHandleModAuditLogChannel(guild); if (auditLogChannel.isEmpty()) { return; } From ddc1b887de06e5174b187ead68a6b239264c6bc3 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 15:50:40 +0200 Subject: [PATCH 67/77] Using a `BiConsumer` to minimize code duplication. --- .../tjbot/commands/tags/TagManageCommand.java | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index c328bb360e..a0b565b05a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -364,37 +364,39 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, "previousContent is null even though the subcommand should supply a value."); } + BiConsumer writeLog = + (title, attachments) -> modAuditLogWriter.write(title, + String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), + author, triggeredAt, guild, attachments); + switch (subcommand) { - case CREATE -> modAuditLogWriter.write("Tag-Manage Create", - String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, - triggeredAt, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, - Objects.requireNonNull(newContent))); - - case CREATE_WITH_MESSAGE -> modAuditLogWriter.write("Tag-Manage Create with message", - String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, - triggeredAt, guild, new ModAuditLogWriter.Attachment(CONTENT_FILE_NAME, - Objects.requireNonNull(newContent))); - - case EDIT -> modAuditLogWriter.write("Tag-Manage Edit", - String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, - triggeredAt, guild, - new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, - Objects.requireNonNull(newContent)), - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - Objects.requireNonNull(previousContent))); - - case EDIT_WITH_MESSAGE -> modAuditLogWriter.write("Tag-Manage Edit with message", - String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, - triggeredAt, guild, - new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, - Objects.requireNonNull(newContent)), - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - Objects.requireNonNull(previousContent))); - - case DELETE -> modAuditLogWriter.write("Tag-Manage Delete", - String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), author, - triggeredAt, guild, new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - Objects.requireNonNull(previousContent))); + case CREATE -> writeLog.accept("Tag-Manage Create", + new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( + CONTENT_FILE_NAME, Objects.requireNonNull(newContent))}); + + case CREATE_WITH_MESSAGE -> writeLog.accept("Tag-Manage Create with message", + new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( + CONTENT_FILE_NAME, Objects.requireNonNull(newContent))}); + + case EDIT -> writeLog.accept("Tag-Manage Edit", + new ModAuditLogWriter.Attachment[] { + new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, + Objects.requireNonNull(newContent)), + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, + Objects.requireNonNull(previousContent))}); + + case EDIT_WITH_MESSAGE -> writeLog.accept("Tag-Manage Edit with message", + new ModAuditLogWriter.Attachment[] { + new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, + Objects.requireNonNull(newContent)), + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, + Objects.requireNonNull(previousContent))}); + + case DELETE -> writeLog + .accept("Tag-Manage Delete", + new ModAuditLogWriter.Attachment[] { + new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, + Objects.requireNonNull(previousContent))}); default -> throw new IllegalArgumentException(String.format( "The subcommand '%s' is not intended to be logged to the mod audit channel.", From 2661399f500c3048894d09cb83ca6efb3c7db9da Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 22:38:22 +0200 Subject: [PATCH 68/77] `getTagContent()` is now returning `null` in case it shouldn't return a value --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index a0b565b05a..f295ecb39c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -306,7 +306,7 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, * @param id the id of the tag to get its content * @return the content of the tag, or {@code "Unable to retrieve content"} if was unable to */ - private @NotNull String getTagContent(@NotNull Subcommand subcommand, @NotNull String id) { + private @Nullable String getTagContent(@NotNull Subcommand subcommand, @NotNull String id) { if (Subcommand.SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand)) { try { return tagSystem.getTag(id).orElseThrow(); @@ -319,7 +319,7 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } } - return "Unable to retrieve content"; + return null; } /** From f6ad12064184606a6e7a4fff81c54dca6c22722f Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 22:40:13 +0200 Subject: [PATCH 69/77] Made test compile again --- .../tjbot/commands/tags/TagManageCommandTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java index 591b85db89..bc188f8498 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java @@ -18,6 +18,7 @@ import org.togetherjava.tjbot.db.Database; import org.togetherjava.tjbot.db.generated.tables.Tags; import org.togetherjava.tjbot.jda.JdaTester; +import org.togetherjava.tjbot.moderation.ModAuditLogWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -33,6 +34,7 @@ final class TagManageCommandTest { private JdaTester jdaTester; private SlashCommand command; private Member moderator; + private ModAuditLogWriter modAuditLogWriter; private static @NotNull MessageEmbed getResponse(@NotNull SlashCommandEvent event) { ArgumentCaptor responseCaptor = ArgumentCaptor.forClass(MessageEmbed.class); @@ -45,11 +47,12 @@ void setUp() { Config config = mock(Config.class); String moderatorRoleName = "Moderator"; when(config.getTagManageRolePattern()).thenReturn(moderatorRoleName); + modAuditLogWriter = mock(ModAuditLogWriter.class); Database database = Database.createMemoryDatabase(Tags.TAGS); system = spy(new TagSystem(database)); jdaTester = new JdaTester(); - command = new TagManageCommand(system, config); + command = new TagManageCommand(system, config, modAuditLogWriter); moderator = jdaTester.createMemberSpy(1); Role moderatorRole = mock(Role.class); From 1f493f3928d6073c6dd1f1b131a7af081e84f67f Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 11 Mar 2022 23:28:10 +0200 Subject: [PATCH 70/77] now verifying wanted results in tests --- .../commands/tags/TagManageCommandTest.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java index bc188f8498..70ba13ed88 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tags/TagManageCommandTest.java @@ -160,6 +160,7 @@ void commandCanNotBeUsedWithoutRoles() { // THEN the command can not be used since the user lacks roles verify(event).reply("Tags can only be managed by users with a corresponding role."); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -171,6 +172,7 @@ void rawTagCanNotFindUnknownTag() { // THEN the command can not find the tag and responds accordingly verify(event).reply(startsWith("Could not find any tag")); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -185,6 +187,7 @@ void rawTagShowsContentIfFound() { // THEN the command responds with its content as an attachment verify(jdaTester.getReplyActionMock()) .addFile(aryEq("bar".getBytes(StandardCharsets.UTF_8)), anyString()); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -200,6 +203,7 @@ void createTagThatAlreadyExistsFails() { verify(event).reply("The tag with id 'foo' already exists."); assertTrue(system.hasTag("foo")); assertEquals("old", system.getTag("foo").orElseThrow()); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -213,6 +217,9 @@ void createNewTagWorks() { assertEquals("Success", getResponse(event).getTitle()); assertTrue(system.hasTag("foo")); assertEquals("bar", system.getTag("foo").orElseThrow()); + verify(modAuditLogWriter).write("Tag-Manage Create", "created tag **foo**", event.getUser(), + event.getTimeCreated(), event.getGuild(), + new ModAuditLogWriter.Attachment("content.md", "bar")); } @Test @@ -225,6 +232,7 @@ void editUnknownTagFails() { // THEN the command fails and responds accordingly, the tag was not created verify(event).reply(startsWith("Could not find any tag with id")); assertFalse(system.hasTag("foo")); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -239,6 +247,10 @@ void editExistingTagWorks() { // THEN the command succeeds and the content of the tag was changed assertEquals("Success", getResponse(event).getTitle()); assertEquals("new", system.getTag("foo").orElseThrow()); + verify(modAuditLogWriter).write("Tag-Manage Edit", "edited tag **foo**", event.getUser(), + event.getTimeCreated(), event.getGuild(), + new ModAuditLogWriter.Attachment("new_content.md", "new"), + new ModAuditLogWriter.Attachment("previous_content.md", "old")); } @Test @@ -250,6 +262,7 @@ void deleteUnknownTagFails() { // THEN the command fails and responds accordingly verify(event).reply(startsWith("Could not find any tag with id")); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -264,6 +277,9 @@ void deleteExistingTagWorks() { // THEN the command succeeds and the tag was deleted assertEquals("Success", getResponse(event).getTitle()); assertFalse(system.hasTag("foo")); + verify(modAuditLogWriter).write("Tag-Manage Delete", "deleted tag **foo**", event.getUser(), + event.getTimeCreated(), event.getGuild(), + new ModAuditLogWriter.Attachment("previous_content.md", "bar")); } @Test @@ -276,6 +292,7 @@ void createWithMessageFailsForInvalidMessageId() { // THEN the command fails and responds accordingly, the tag was not created verify(event).reply("The given message id 'bar' is invalid, expected a number."); assertFalse(system.hasTag("foo")); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -292,6 +309,7 @@ void createWithMessageTagThatAlreadyExistsFails() { verify(event).reply("The tag with id 'foo' already exists."); assertTrue(system.hasTag("foo")); assertEquals("old", system.getTag("foo").orElseThrow()); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -307,6 +325,9 @@ void createWithMessageNewTagWorks() { assertEquals("Success", getResponse(event).getTitle()); assertTrue(system.hasTag("foo")); assertEquals("bar", system.getTag("foo").orElseThrow()); + verify(modAuditLogWriter).write("Tag-Manage Create with message", "created tag **foo**", + event.getUser(), event.getTimeCreated(), event.getGuild(), + new ModAuditLogWriter.Attachment("content.md", "bar")); } @Test @@ -322,6 +343,7 @@ void createWithMessageUnknownMessageFails() { // THEN the command fails and responds accordingly, the tag was not created verify(event).reply("The message with id '1' does not exist."); assertFalse(system.hasTag("foo")); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -336,6 +358,7 @@ void createWithMessageGenericErrorFails() { // THEN the command fails and responds accordingly, the tag was not created verify(event).reply(startsWith("Something unexpected went wrong")); assertFalse(system.hasTag("foo")); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -350,6 +373,7 @@ void editWithMessageFailsForInvalidMessageId() { // THEN the command fails and responds accordingly, the tags content was not changed verify(event).reply("The given message id 'bar' is invalid, expected a number."); assertEquals("old", system.getTag("foo").orElseThrow()); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -364,6 +388,7 @@ void editWithMessageUnknownTagFails() { // THEN the command fails and responds accordingly, the tag was not created verify(event).reply(startsWith("Could not find any tag with id")); assertFalse(system.hasTag("foo")); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -379,6 +404,10 @@ void editWithMessageExistingTagWorks() { // THEN the command succeeds and the content of the tag was changed assertEquals("Success", getResponse(event).getTitle()); assertEquals("new", system.getTag("foo").orElseThrow()); + verify(modAuditLogWriter).write("Tag-Manage Edit with message", "edited tag **foo**", + event.getUser(), event.getTimeCreated(), event.getGuild(), + new ModAuditLogWriter.Attachment("new_content.md", "new"), + new ModAuditLogWriter.Attachment("previous_content.md", "old")); } @Test @@ -396,6 +425,7 @@ void editWithMessageUnknownMessageFails() { verify(event).reply("The message with id '1' does not exist."); assertTrue(system.hasTag("foo")); assertEquals("old", system.getTag("foo").orElseThrow()); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } @Test @@ -412,5 +442,6 @@ void editWithMessageGenericErrorFails() { verify(event).reply(startsWith("Something unexpected went wrong")); assertTrue(system.hasTag("foo")); assertEquals("old", system.getTag("foo").orElseThrow()); + verify(modAuditLogWriter, never()).write(any(), any(), any(), any(), any(), any()); } } From a3692badfa4b28630113502cd0e985284c689d0e Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Sat, 12 Mar 2022 10:29:01 +0200 Subject: [PATCH 71/77] quick patch --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 4 ++-- .../org/togetherjava/tjbot/moderation/ModAuditLogWriter.java | 4 ++-- .../org/togetherjava/tjbot/routines/ModAuditLogRoutine.java | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index f295ecb39c..3db67792e3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -74,9 +74,9 @@ public final class TagManageCommand extends SlashCommandAdapter { * * @param tagSystem the system providing the actual tag data * @param config the config to use for this - * @param modAuditLogWriter the mod audit log writer to use for this + * @param modAuditLogWriter to log tag changes for audition */ - public TagManageCommand(TagSystem tagSystem, @NotNull Config config, + public TagManageCommand(@NotNull TagSystem tagSystem, @NotNull Config config, @NotNull ModAuditLogWriter modAuditLogWriter) { super("tag-manage", "Provides commands to manage all tags", SlashCommandVisibility.GUILD); diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 910bd4cdb9..93224b42ee 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -28,9 +28,9 @@ public final class ModAuditLogWriter { private static final Color EMBED_COLOR = Color.decode("#3788AC"); - private final Config config; + private static final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class); - private final Logger logger = LoggerFactory.getLogger(ModAuditLogWriter.class); + private final Config config; private Pattern auditLogChannelNamePattern = null; diff --git a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java index a194faa4db..1cf837c809 100644 --- a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java @@ -58,7 +58,7 @@ public final class ModAuditLogRoutine implements Routine { * * @param database the database for memorizing audit log dates * @param config the config to use for this - * @param modAuditLogWriter the mod audit log writer to use for this + * @param modAuditLogWriter to log tag changes for audition */ public ModAuditLogRoutine(@NotNull Database database, @NotNull Config config, @NotNull ModAuditLogWriter modAuditLogWriter) { From 7a812b783ce9d6569deed2c485fe38650360755e Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Sat, 12 Mar 2022 10:39:24 +0200 Subject: [PATCH 72/77] Updated `Unable to retrieve content` handling --- .../tjbot/commands/tags/TagManageCommand.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 3db67792e3..0ad9722337 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -64,6 +64,8 @@ public final class TagManageCommand extends SlashCommandAdapter { private static final String NEW_CONTENT_FILE_NAME = "new_content.md"; private static final String PREVIOUS_CONTENT_FILE_NAME = "previous_content.md"; + private static final String UNABLE_TO_GET_CONTENT_MESSAGE = "Was unable to retrieve content"; + private final TagSystem tagSystem; private final Predicate hasRequiredRole; @@ -230,7 +232,8 @@ private void handleAction(@NotNull TagStatus requiredTagStatus, return; } - String previousContent = getTagContent(subcommand, id); + String previousContent = + getTagContent(subcommand, id).orElse(UNABLE_TO_GET_CONTENT_MESSAGE); idAction.accept(id); sendSuccessMessage(event, id, subcommand.getActionVerb()); @@ -271,7 +274,8 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } event.getMessageChannel().retrieveMessageById(messageId).queue(message -> { - String previousContent = getTagContent(subcommand, tagId); + String previousContent = + getTagContent(subcommand, tagId).orElse(UNABLE_TO_GET_CONTENT_MESSAGE); idAndContentAction.accept(tagId, message.getContentRaw()); sendSuccessMessage(event, tagId, subcommand.getActionVerb()); @@ -306,10 +310,11 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, * @param id the id of the tag to get its content * @return the content of the tag, or {@code "Unable to retrieve content"} if was unable to */ - private @Nullable String getTagContent(@NotNull Subcommand subcommand, @NotNull String id) { + private @NotNull Optional getTagContent(@NotNull Subcommand subcommand, + @NotNull String id) { if (Subcommand.SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand)) { try { - return tagSystem.getTag(id).orElseThrow(); + return tagSystem.getTag(id); } catch (NoSuchElementException e) { // NOTE Rare race condition, for example if another thread deleted the tag in the // meantime @@ -319,7 +324,7 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } } - return null; + return Optional.empty(); } /** From 4a1900487ef529a169e2437c4c5d76f7925fe6d9 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Sat, 12 Mar 2022 10:59:01 +0200 Subject: [PATCH 73/77] made `auditLogChannelNamePattern` compile in constructor --- .../togetherjava/tjbot/moderation/ModAuditLogWriter.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 93224b42ee..636ff4ac0b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -32,7 +32,7 @@ public final class ModAuditLogWriter { private final Config config; - private Pattern auditLogChannelNamePattern = null; + private final Pattern auditLogChannelNamePattern; /** * Creates a new instance. @@ -41,6 +41,7 @@ public final class ModAuditLogWriter { */ public ModAuditLogWriter(@NotNull Config config) { this.config = config; + auditLogChannelNamePattern = Pattern.compile(config.getModAuditLogChannelPattern()); } /** @@ -82,10 +83,6 @@ public void write(@NotNull String title, @NotNull String description, @NotNull U * @param guild the guild to look for the channel in */ public Optional getAndHandleModAuditLogChannel(@NotNull Guild guild) { - if (auditLogChannelNamePattern == null) { - auditLogChannelNamePattern = Pattern.compile(config.getModAuditLogChannelPattern()); - } - Optional auditLogChannel = guild.getTextChannelCache() .stream() .filter(channel -> auditLogChannelNamePattern.asMatchPredicate() From 279d3aa68f6aca0068c673beeef94b2dd25083e2 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Sat, 12 Mar 2022 18:34:30 +0200 Subject: [PATCH 74/77] changed `logAction()` logic --- .../tjbot/commands/tags/TagManageCommand.java | 80 ++++++++----------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 0ad9722337..147e1f3092 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -358,55 +358,45 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, @NotNull User author, @NotNull TemporalAccessor triggeredAt, @NotNull String id, @Nullable String newContent, @Nullable String previousContent) { - if (Subcommand.SUBCOMMANDS_WITH_NEW_CONTENT.contains(subcommand) && newContent == null) { - throw new IllegalArgumentException( - "newContent is null even though the subcommand should supply a value."); - } + ArrayList attachments = new ArrayList<>(); + + if (Subcommand.SUBCOMMANDS_WITH_NEW_CONTENT.contains(subcommand)) { + if (newContent == null) { + throw new IllegalArgumentException( + "newContent is null even though the subcommand should supply a value."); + } + + String fileName = (subcommand == Subcommand.CREATE + || subcommand == Subcommand.CREATE_WITH_MESSAGE) ? CONTENT_FILE_NAME + : NEW_CONTENT_FILE_NAME; + + attachments.add(new ModAuditLogWriter.Attachment(fileName, newContent)); - if (Subcommand.SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand) - && previousContent == null) { - throw new IllegalArgumentException( - "previousContent is null even though the subcommand should supply a value."); } - BiConsumer writeLog = - (title, attachments) -> modAuditLogWriter.write(title, - String.format(LOG_EMBED_DESCRIPTION, subcommand.getActionVerb(), id), - author, triggeredAt, guild, attachments); - - switch (subcommand) { - case CREATE -> writeLog.accept("Tag-Manage Create", - new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - CONTENT_FILE_NAME, Objects.requireNonNull(newContent))}); - - case CREATE_WITH_MESSAGE -> writeLog.accept("Tag-Manage Create with message", - new ModAuditLogWriter.Attachment[] {new ModAuditLogWriter.Attachment( - CONTENT_FILE_NAME, Objects.requireNonNull(newContent))}); - - case EDIT -> writeLog.accept("Tag-Manage Edit", - new ModAuditLogWriter.Attachment[] { - new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, - Objects.requireNonNull(newContent)), - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - Objects.requireNonNull(previousContent))}); - - case EDIT_WITH_MESSAGE -> writeLog.accept("Tag-Manage Edit with message", - new ModAuditLogWriter.Attachment[] { - new ModAuditLogWriter.Attachment(NEW_CONTENT_FILE_NAME, - Objects.requireNonNull(newContent)), - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - Objects.requireNonNull(previousContent))}); - - case DELETE -> writeLog - .accept("Tag-Manage Delete", - new ModAuditLogWriter.Attachment[] { - new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, - Objects.requireNonNull(previousContent))}); - - default -> throw new IllegalArgumentException(String.format( - "The subcommand '%s' is not intended to be logged to the mod audit channel.", - subcommand.name())); + if (Subcommand.SUBCOMMANDS_WITH_PREVIOUS_CONTENT.contains(subcommand)) { + if (previousContent == null) { + throw new IllegalArgumentException( + "previousContent is null even though the subcommand should supply a value."); + } + + attachments + .add(new ModAuditLogWriter.Attachment(PREVIOUS_CONTENT_FILE_NAME, previousContent)); } + + String title = switch (subcommand) { + case CREATE -> "Tag-Manage Create"; + case CREATE_WITH_MESSAGE -> "Tag-Manage Create with message"; + case EDIT -> "Tag-Manage Edit"; + case EDIT_WITH_MESSAGE -> "Tag-Manage Edit with message"; + case DELETE -> "Tag-Manage Delete"; + default -> throw new IllegalArgumentException( + "The subcommand '%s' is not intended to be logged to the mod audit channel."); + }; + + modAuditLogWriter.write(title, + LOG_EMBED_DESCRIPTION.formatted(subcommand.getActionVerb(), id), author, + triggeredAt, guild, attachments.toArray(ModAuditLogWriter.Attachment[]::new)); } private boolean hasTagManageRole(@NotNull Member member) { From 7639f7111837799c82952cc5d631e020697264cc Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Sat, 12 Mar 2022 20:32:34 +0200 Subject: [PATCH 75/77] Updated `getTagContent()` javadoc --- .../togetherjava/tjbot/commands/tags/TagManageCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index 147e1f3092..a16ea17524 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -304,11 +304,11 @@ private void handleActionWithMessage(@NotNull TagStatus requiredTagStatus, } /** - * Gets the content of a tag, or {@code "Unable to retrieve content"} if was unable to. - * + * Gets the content of a tag. + * * @param subcommand the subcommand to be executed * @param id the id of the tag to get its content - * @return the content of the tag, or {@code "Unable to retrieve content"} if was unable to + * @return the content of the tag, if present */ private @NotNull Optional getTagContent(@NotNull Subcommand subcommand, @NotNull String id) { From 2111dc87748680eed40aac3f07bef0ebc28e7477 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Sat, 12 Mar 2022 20:35:43 +0200 Subject: [PATCH 76/77] replaced `ArrayList` with `List` --- .../org/togetherjava/tjbot/commands/tags/TagManageCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java index a16ea17524..e2bae097b8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java @@ -358,7 +358,7 @@ private void logAction(@NotNull Subcommand subcommand, @NotNull Guild guild, @NotNull User author, @NotNull TemporalAccessor triggeredAt, @NotNull String id, @Nullable String newContent, @Nullable String previousContent) { - ArrayList attachments = new ArrayList<>(); + List attachments = new ArrayList<>(); if (Subcommand.SUBCOMMANDS_WITH_NEW_CONTENT.contains(subcommand)) { if (newContent == null) { From cf98c4a805230f5825eef32b516f4211b896fdd8 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Sun, 13 Mar 2022 14:31:57 +0200 Subject: [PATCH 77/77] put predicate in the field instead of the pattern --- .../togetherjava/tjbot/moderation/ModAuditLogWriter.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java index 636ff4ac0b..b7a1f9fa39 100644 --- a/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java +++ b/application/src/main/java/org/togetherjava/tjbot/moderation/ModAuditLogWriter.java @@ -16,6 +16,7 @@ import java.nio.charset.StandardCharsets; import java.time.temporal.TemporalAccessor; import java.util.Optional; +import java.util.function.Predicate; import java.util.regex.Pattern; /** @@ -32,7 +33,7 @@ public final class ModAuditLogWriter { private final Config config; - private final Pattern auditLogChannelNamePattern; + private final Predicate auditLogChannelNamePredicate; /** * Creates a new instance. @@ -41,7 +42,8 @@ public final class ModAuditLogWriter { */ public ModAuditLogWriter(@NotNull Config config) { this.config = config; - auditLogChannelNamePattern = Pattern.compile(config.getModAuditLogChannelPattern()); + auditLogChannelNamePredicate = + Pattern.compile(config.getModAuditLogChannelPattern()).asMatchPredicate(); } /** @@ -85,8 +87,7 @@ public void write(@NotNull String title, @NotNull String description, @NotNull U public Optional getAndHandleModAuditLogChannel(@NotNull Guild guild) { Optional auditLogChannel = guild.getTextChannelCache() .stream() - .filter(channel -> auditLogChannelNamePattern.asMatchPredicate() - .test(channel.getName())) + .filter(channel -> auditLogChannelNamePredicate.test(channel.getName())) .findAny(); if (auditLogChannel.isEmpty()) {