From 9557231010f8cf5cef6b702f6c3b629b0ec944cb Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 11 Apr 2025 14:17:37 +0700 Subject: [PATCH] refactor: only schedule discord ticking once, globally, not each for every server --- build-number.txt | 2 +- .../chomens_bot/plugins/DiscordPlugin.java | 88 ++++++++++--------- 2 files changed, 47 insertions(+), 43 deletions(-) diff --git a/build-number.txt b/build-number.txt index 9275af72..e71cd9f5 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -2634 \ No newline at end of file +2637 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java index 498043c6..6ef54db2 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/DiscordPlugin.java @@ -67,11 +67,11 @@ public class DiscordPlugin { new GuildMessageEventHandler(jda, prefix, messagePrefix); + Main.EXECUTOR.scheduleAtFixedRate(this::onDiscordTick, 0, 50, TimeUnit.MILLISECONDS); + for (final Bot bot : Main.bots) { final String channelId = servers.get(bot.getServerString(true)); - bot.executor.scheduleAtFixedRate(() -> onDiscordTick(channelId), 0, 50, TimeUnit.MILLISECONDS); - bot.addListener(new Bot.Listener() { @Override public void loadedPlugins (final Bot bot) { @@ -186,54 +186,58 @@ public class DiscordPlugin { } } - public void onDiscordTick (final String channelId) { - if (!logMessages.containsKey(channelId) || logMessages.get(channelId).isEmpty()) { - return; - } + public void onDiscordTick () { + for (final Bot bot : Main.bots) { + final String channelId = servers.get(bot.getServerString(true)); - final long currentTime = System.currentTimeMillis(); - if (!nextLogTimes.containsKey(channelId) - || (currentTime >= nextLogTimes.get(channelId) && doneSendingInLogs.get(channelId)) - || currentTime - nextLogTimes.get(channelId) > 5000 - ) { - final long logDelay = 2000; - - nextLogTimes.put(channelId, currentTime + logDelay); - - String message; - - final StringBuilder logMessage = logMessages.get(channelId); - - final Matcher inviteMatcher = Message.INVITE_PATTERN.matcher(logMessage.toString()); - - final StringBuilder messageBuilder = new StringBuilder(); - - while (inviteMatcher.find()) { - inviteMatcher.appendReplacement( - messageBuilder, - Matcher.quoteReplacement( - inviteMatcher.group() - // fixes discord.gg (and some more discord urls) showing invite - .replace(".", "\u200b.") - ) - ); + if (!logMessages.containsKey(channelId) || logMessages.get(channelId).isEmpty()) { + continue; } - inviteMatcher.appendTail(messageBuilder); + final long currentTime = System.currentTimeMillis(); + if (!nextLogTimes.containsKey(channelId) + || (currentTime >= nextLogTimes.get(channelId) && doneSendingInLogs.get(channelId)) + || currentTime - nextLogTimes.get(channelId) > 5000 + ) { + final long logDelay = 2000; - final int maxLength = 2_000 - (""" - ```ansi - - ```""" - ).length(); // kinda sus + nextLogTimes.put(channelId, currentTime + logDelay); - message = messageBuilder.substring(0, Math.min(messageBuilder.length(), maxLength)); + String message; - logMessage.setLength(0); + final StringBuilder logMessage = logMessages.get(channelId); - if (message.trim().isBlank()) return; + final Matcher inviteMatcher = Message.INVITE_PATTERN.matcher(logMessage.toString()); - sendMessageInstantly("```ansi\n" + message + "\n```", channelId); + final StringBuilder messageBuilder = new StringBuilder(); + + while (inviteMatcher.find()) { + inviteMatcher.appendReplacement( + messageBuilder, + Matcher.quoteReplacement( + inviteMatcher.group() + // fixes discord.gg (and some more discord urls) showing invite + .replace(".", "\u200b.") + ) + ); + } + + inviteMatcher.appendTail(messageBuilder); + + final int maxLength = 2_000 - (""" + ```ansi + + ```""" + ).length(); // kinda sus + + message = messageBuilder.substring(0, Math.min(messageBuilder.length(), maxLength)); + + logMessage.setLength(0); + + if (message.trim().isBlank()) continue; + + sendMessageInstantly("```ansi\n" + message + "\n```", channelId); + } } } }