From 6ed8aba35f93f561a88d539f4c475ed5c8a99984 Mon Sep 17 00:00:00 2001
From: Dragory <2606411+Dragory@users.noreply.github.com>
Date: Thu, 2 May 2019 18:34:15 +0300
Subject: [PATCH] Optimizations + debug logging

---
 src/plugins/Mutes.ts    | 13 +++++++++++--
 src/plugins/Slowmode.ts |  3 ++-
 src/utils.ts            |  3 +++
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/plugins/Mutes.ts b/src/plugins/Mutes.ts
index 2c2f42f0..a8ec2245 100644
--- a/src/plugins/Mutes.ts
+++ b/src/plugins/Mutes.ts
@@ -53,6 +53,10 @@ export type UnmuteResult = {
   case: Case;
 };
 
+const EXPIRED_MUTE_CHECK_INTERVAL = 60 * 1000;
+let FIRST_CHECK_TIME = Date.now();
+const FIRST_CHECK_INCREMENT = 5 * 1000;
+
 export class MutesPlugin extends ZeppelinPlugin<IMutesPluginConfig> {
   public static pluginName = "mutes";
 
@@ -99,8 +103,13 @@ export class MutesPlugin extends ZeppelinPlugin<IMutesPluginConfig> {
     this.serverLogs = new GuildLogs(this.guildId);
 
     // Check for expired mutes every 5s
-    this.clearExpiredMutes();
-    this.muteClearIntervalId = setInterval(() => this.clearExpiredMutes(), 5000);
+    const firstCheckTime = Math.max(Date.now(), FIRST_CHECK_TIME) + FIRST_CHECK_INCREMENT;
+    FIRST_CHECK_TIME = firstCheckTime;
+
+    setTimeout(() => {
+      this.clearExpiredMutes();
+      this.muteClearIntervalId = setInterval(() => this.clearExpiredMutes(), EXPIRED_MUTE_CHECK_INTERVAL);
+    }, firstCheckTime - Date.now());
   }
 
   protected onUnload() {
diff --git a/src/plugins/Slowmode.ts b/src/plugins/Slowmode.ts
index 95ceac90..e5014d1b 100644
--- a/src/plugins/Slowmode.ts
+++ b/src/plugins/Slowmode.ts
@@ -10,6 +10,7 @@ import DiscordRESTError from "eris/lib/errors/DiscordRESTError"; // tslint:disab
 
 const NATIVE_SLOWMODE_LIMIT = 6 * 60 * 60; // 6 hours
 const MAX_SLOWMODE = 60 * 60 * 24 * 365 * 100; // 100 years
+const BOT_SLOWMODE_CLEAR_INTERVAL = 60 * 1000;
 
 interface ISlowmodePluginConfig {
   use_native_slowmode: boolean;
@@ -51,7 +52,7 @@ export class SlowmodePlugin extends ZeppelinPlugin<ISlowmodePluginConfig> {
   onLoad() {
     this.slowmodes = GuildSlowmodes.getInstance(this.guildId);
     this.savedMessages = GuildSavedMessages.getInstance(this.guildId);
-    this.clearInterval = setInterval(() => this.clearExpiredSlowmodes(), 2000);
+    this.clearInterval = setInterval(() => this.clearExpiredSlowmodes(), BOT_SLOWMODE_CLEAR_INTERVAL);
 
     this.onMessageCreateFn = this.onMessageCreate.bind(this);
     this.savedMessages.events.on("create", this.onMessageCreateFn);
diff --git a/src/utils.ts b/src/utils.ts
index 64a89f54..289404b2 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -600,6 +600,9 @@ export async function resolveMember(bot: Client, guild: Guild, value: string): P
     // If not, fetch it from the API
     if (!member) {
       try {
+        logger.info(`Fetching unknown member (${user.id} in ${guild.name} (${guild.id})) from the API`);
+        console.trace();
+
         member = await bot.getRESTGuildMember(guild.id, user.id);
         member.id = user.id;
         member.guild = guild;