From 669b1680b7e4a4acf6dfffbd9ad398284e7cc9f9 Mon Sep 17 00:00:00 2001
From: Will Hunt <will@half-shot.uk>
Date: Fri, 26 Oct 2018 00:57:07 +0100
Subject: [PATCH] Don't delay messages behind each other by 750ms

---
 config/config.sample.yaml | 6 +++++-
 config/config.schema.yaml | 2 ++
 src/bot.ts                | 6 +++---
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/config/config.sample.yaml b/config/config.sample.yaml
index 2b83505..925df2b 100644
--- a/config/config.sample.yaml
+++ b/config/config.sample.yaml
@@ -87,5 +87,9 @@ channel:
        # Make all the discord users leave the room.
        ghostsLeave: true
 limits:
-    # Delay between discord users joining a room.
+    # Delay in milliseconds between discord users joining a room.
     roomGhostJoinDelay: 6000
+    # Delay in milliseconds before sending messages to discord to avoid echos.
+    # (Copies of a sent message may arrive from discord before we've
+    # fininished handling it, causing us to echo it back to the room)
+    discordSendDelay: 750
diff --git a/config/config.schema.yaml b/config/config.schema.yaml
index 56de3a8..4f832fa 100644
--- a/config/config.schema.yaml
+++ b/config/config.schema.yaml
@@ -89,6 +89,8 @@ properties:
         properties:
             roomGhostJoinDelay:
                 type: "number"
+            discordSendDelay:
+                type: "number"
     channel:
         type: "object"
         properties:
diff --git a/src/bot.ts b/src/bot.ts
index 523073e..96b8fa9 100644
--- a/src/bot.ts
+++ b/src/bot.ts
@@ -119,11 +119,11 @@ export class DiscordBot {
             Bluebird.delay(this.config.limits.discordSendDelay),
         ]).then(() => this.OnMessageUpdate(oldMessage, newMessage));
       });
-      client.on("message", (msg: Discord.Message) => {
+      client.on("message", async (msg: Discord.Message) => {
+        // tslint:disable-next-line:await-promise
+        await Bluebird.delay(this.config.limits.discordSendDelay);
         this.discordMessageQueue[msg.channel.id] = (async () => {
             await (this.discordMessageQueue[msg.channel.id] || Promise.resolve());
-            // tslint:disable-next-line:await-promise
-            await Bluebird.delay(this.config.limits.discordSendDelay);
             await this.OnMessage(msg);
         })();
       });
-- 
GitLab