From 0822e1ed6efefc0c88f221815362573b91fb36d1 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 17 Jun 2022 16:39:36 +0200 Subject: [PATCH 1/6] Adding /set-log-level command --- .../togetherjava/tjbot/commands/Features.java | 2 + .../commands/system/LogLevelCommand.java | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java 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 7a3da10a64..08a63930e4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Features.java @@ -17,6 +17,7 @@ import org.togetherjava.tjbot.commands.reminder.RemindCommand; import org.togetherjava.tjbot.commands.reminder.RemindRoutine; import org.togetherjava.tjbot.commands.system.BotCore; +import org.togetherjava.tjbot.commands.system.LogLevelCommand; import org.togetherjava.tjbot.commands.tags.TagCommand; import org.togetherjava.tjbot.commands.tags.TagManageCommand; import org.togetherjava.tjbot.commands.tags.TagSystem; @@ -85,6 +86,7 @@ public enum Features { features.add(new RejoinModerationRoleListener(actionsStore, config)); // Slash commands + features.add(new LogLevelCommand()); features.add(new PingCommand()); features.add(new TeXCommand()); features.add(new TagCommand(tagSystem)); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java new file mode 100644 index 0000000000..32a66f41b4 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/LogLevelCommand.java @@ -0,0 +1,61 @@ +package org.togetherjava.tjbot.commands.system; + +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.config.Configurator; +import org.jetbrains.annotations.NotNull; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.commands.SlashCommandVisibility; + +import java.util.stream.Stream; + +/** + * Implements the '/set-log-level' command which can be used to change the log level used by the + * bot, while it is running. + *

+ * Example usage: + * + *

+ * {@code
+ * /set-log-level level: INFO
+ * }
+ * 
+ */ +public final class LogLevelCommand extends SlashCommandAdapter { + private static final String LOG_LEVEL_OPTION = "level"; + + /** + * Creates a new instance. + */ + public LogLevelCommand() { + super("set-log-level", "Changes the log level of the bot while it is running.", + SlashCommandVisibility.GUILD); + + OptionData option = + new OptionData(OptionType.STRING, LOG_LEVEL_OPTION, "the log level to set", true); + Stream.of(Level.values()).map(Level::name).forEach(level -> option.addChoice(level, level)); + + getData().addOptions(option); + } + + // Security warning about changing log configs. We only change the level, that is safe. + @SuppressWarnings("squid:S4792") + @Override + public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { + String levelText = event.getOption(LOG_LEVEL_OPTION).getAsString(); + Level level = Level.getLevel(levelText); + + if (level == null) { + event.reply("The selected log level '%s' is unknown.".formatted(levelText)) + .setEphemeral(true) + .queue(); + return; + } + + Configurator.setAllLevels(LogManager.getRootLogger().getName(), level); + event.reply("Set the log level to '%s'.".formatted(levelText)).queue(); + } +} From 7692e2927d8b2af5c8af07d8d2740237cc6c8bdf Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 17 Jun 2022 16:09:35 +0200 Subject: [PATCH 2/6] Deactivated logviewer --- settings.gradle | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/settings.gradle b/settings.gradle index 1dd56827f2..811b17ba0f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -3,4 +3,6 @@ rootProject.name = 'TJ-Bot' include 'application' include 'database' include 'formatter' -include 'logviewer' +// NOTE The logviewer does not properly work as of today. +// But it is causing major build slowdowns, so we exclude it for the time being. +// include 'logviewer' From f8ae44e3f33ca02ca4090ae0c42801bfab01ad18 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 17 Jun 2022 16:09:54 +0200 Subject: [PATCH 3/6] Removed unused "helpChannelPattern" in config file --- application/config.json.template | 1 - .../java/org/togetherjava/tjbot/config/Config.java | 13 ------------- 2 files changed, 14 deletions(-) diff --git a/application/config.json.template b/application/config.json.template index 0d3b1af6df..8238dba370 100644 --- a/application/config.json.template +++ b/application/config.json.template @@ -8,7 +8,6 @@ "heavyModerationRolePattern": "Moderator", "softModerationRolePattern": "Moderator|Staff Assistant", "tagManageRolePattern": "Moderator|Staff Assistant|Top Helpers .+", - "helpChannelPattern": "([a-zA-Z_]+_)?help(_\\d+)?", "suggestions": { "channelPattern": "tj_suggestions", "upVoteEmoteName": "peepo_yes", diff --git a/application/src/main/java/org/togetherjava/tjbot/config/Config.java b/application/src/main/java/org/togetherjava/tjbot/config/Config.java index 73534bb24e..fb4ef623b8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/config/Config.java +++ b/application/src/main/java/org/togetherjava/tjbot/config/Config.java @@ -22,7 +22,6 @@ public final class Config { private final String heavyModerationRolePattern; private final String softModerationRolePattern; private final String tagManageRolePattern; - private final String helpChannelPattern; private final SuggestionsConfig suggestions; private final String quarantinedRolePattern; private final ScamBlockerConfig scamBlocker; @@ -40,7 +39,6 @@ private Config(@JsonProperty("token") String token, @JsonProperty("heavyModerationRolePattern") String heavyModerationRolePattern, @JsonProperty("softModerationRolePattern") String softModerationRolePattern, @JsonProperty("tagManageRolePattern") String tagManageRolePattern, - @JsonProperty("helpChannelPattern") String helpChannelPattern, @JsonProperty("suggestions") SuggestionsConfig suggestions, @JsonProperty("quarantinedRolePattern") String quarantinedRolePattern, @JsonProperty("scamBlocker") ScamBlockerConfig scamBlocker, @@ -55,7 +53,6 @@ private Config(@JsonProperty("token") String token, this.heavyModerationRolePattern = heavyModerationRolePattern; this.softModerationRolePattern = softModerationRolePattern; this.tagManageRolePattern = tagManageRolePattern; - this.helpChannelPattern = helpChannelPattern; this.suggestions = suggestions; this.quarantinedRolePattern = quarantinedRolePattern; this.scamBlocker = scamBlocker; @@ -160,16 +157,6 @@ public String getTagManageRolePattern() { return tagManageRolePattern; } - /** - * Gets the REGEX pattern used to identify channels that are used for helping people with their - * questions. - * - * @return the channel name pattern - */ - public @NotNull String getHelpChannelPattern() { - return helpChannelPattern; - } - /** * Gets the config for the suggestion system. * From 3fdf3a138a507d30b642fb0120bb5ebfbb6fccd5 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 17 Jun 2022 16:10:49 +0200 Subject: [PATCH 4/6] Fixed minor typos --- .../java/org/togetherjava/tjbot/commands/help/AskCommand.java | 2 +- .../org/togetherjava/tjbot/commands/help/HelpSystemHelper.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java index 2b67bc6c85..95b69fbcae 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java @@ -109,7 +109,7 @@ private boolean handleIsValidTitle(@NotNull CharSequence title, @NotNull IReplyC } event.reply( - "Sorry, but the titel length (after removal of special characters) has to be between %d and %d." + "Sorry, but the title length (after removal of special characters) has to be between %d and %d." .formatted(TITLE_COMPACT_LENGTH_MIN, TITLE_COMPACT_LENGTH_MAX)) .setEphemeral(true) .queue(); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index 41fa901cc3..d30c8c8000 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -85,7 +85,7 @@ RestAction sendExplanationMessage(@NotNull MessageChannel threadChannel If nobody is calling back, that usually means that your question was **not well asked** and \ hence nobody feels confident enough answering. Try to use your time to elaborate, \ **provide details**, context, more code, examples and maybe some screenshots. \ - With enough info, someone knows the answer for sure .""")); + With enough info, someone knows the answer for sure.""")); MessageAction action = threadChannel.sendMessage(message); if (useCodeSyntaxExampleImage) { From 806ff683138618c1a621bf6774738858b22608ce Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 17 Jun 2022 16:24:45 +0200 Subject: [PATCH 5/6] Adding question limit --- .../tjbot/commands/help/HelpThreadOverviewUpdater.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java index ee947e2f56..eb4e465d1b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java @@ -32,6 +32,7 @@ public final class HelpThreadOverviewUpdater extends MessageReceiverAdapter impl private static final Logger logger = LoggerFactory.getLogger(HelpThreadOverviewUpdater.class); private static final String STATUS_TITLE = "Active questions"; + private static final int OVERVIEW_QUESTION_LIMIT = 150; private final HelpSystemHelper helper; private final List allCategories; @@ -148,6 +149,7 @@ private void updateOverview(@NotNull IThreadContainer stagingChannel, return activeThreads.stream() .sorted(Comparator.comparing(ThreadChannel::getTimeCreated).reversed()) + .limit(OVERVIEW_QUESTION_LIMIT) .collect(Collectors .groupingBy(thread -> helper.getCategoryOfChannel(thread).orElse("Uncategorized"))) .entrySet() From 61488237c78b51f98fefddc4a8c5432e0ce150a5 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 17 Jun 2022 16:28:29 +0200 Subject: [PATCH 6/6] Added more logging --- .../tjbot/commands/help/BotMessageCleanup.java | 3 +++ .../help/HelpThreadOverviewUpdater.java | 5 +++++ .../tjbot/commands/system/BotCore.java | 17 +++++++++++------ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/BotMessageCleanup.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/BotMessageCleanup.java index d3c90980b4..5678072773 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/BotMessageCleanup.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/BotMessageCleanup.java @@ -108,11 +108,14 @@ private static boolean shouldMessageBeCleanedUp(@NotNull Message message) { private static @NotNull RestAction cleanupBotMessages( @NotNull GuildMessageChannel channel, @NotNull Collection messages) { + logger.debug("Cleaning up old bot messages in the staging channel"); List messageIdsToDelete = messages.stream() .filter(BotMessageCleanup::shouldMessageBeCleanedUp) .map(Message::getId) .toList(); + logger.debug("Found {} messages to delete", messageIdsToDelete.size()); + if (messageIdsToDelete.isEmpty()) { return new CompletedRestAction<>(channel.getJDA(), null, null); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java index eb4e465d1b..3ffe0b6396 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadOverviewUpdater.java @@ -122,17 +122,22 @@ private void updateOverviewForGuild(@NotNull Guild guild) { private void updateOverview(@NotNull IThreadContainer stagingChannel, @NotNull MessageChannel overviewChannel) { + logger.debug("Updating overview of active questions"); + List activeThreads = stagingChannel.getThreadChannels() .stream() .filter(Predicate.not(ThreadChannel::isArchived)) .toList(); + logger.debug("Found {} active questions", activeThreads.size()); + MessageEmbed embed = new EmbedBuilder().setTitle(STATUS_TITLE) .setDescription(createDescription(activeThreads)) .setColor(HelpSystemHelper.AMBIENT_COLOR) .build(); getStatusMessage(overviewChannel).flatMap(maybeStatusMessage -> { + logger.debug("Sending the updated question overview"); if (maybeStatusMessage.isEmpty()) { return overviewChannel.sendMessageEmbeds(embed); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java index 11705448d8..b6847bc882 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/BotCore.java @@ -92,14 +92,19 @@ public BotCore(@NotNull JDA jda, @NotNull Database database, @NotNull Config con .filter(Routine.class::isInstance) .map(Routine.class::cast) .forEach(routine -> { + Runnable command = () -> { + String routineName = routine.getClass().getSimpleName(); + logger.debug("Running routine %s...".formatted(routineName)); + routine.runRoutine(jda); + logger.debug("Finished routine %s.".formatted(routineName)); + }; + Routine.Schedule schedule = routine.createSchedule(); switch (schedule.mode()) { - case FIXED_RATE -> ROUTINE_SERVICE.scheduleAtFixedRate( - () -> routine.runRoutine(jda), schedule.initialDuration(), - schedule.duration(), schedule.unit()); - case FIXED_DELAY -> ROUTINE_SERVICE.scheduleWithFixedDelay( - () -> routine.runRoutine(jda), schedule.initialDuration(), - schedule.duration(), schedule.unit()); + case FIXED_RATE -> ROUTINE_SERVICE.scheduleAtFixedRate(command, + schedule.initialDuration(), schedule.duration(), schedule.unit()); + case FIXED_DELAY -> ROUTINE_SERVICE.scheduleWithFixedDelay(command, + schedule.initialDuration(), schedule.duration(), schedule.unit()); default -> throw new AssertionError("Unsupported schedule mode"); } });