From 0b8e25a7128be13c184f93c0cf013b57014f838e Mon Sep 17 00:00:00 2001
From: "Andrew Morgan andrew@amorgan.xyz" <andrew@amorgan.xyz>
Date: Wed, 23 May 2018 00:15:54 +0200
Subject: [PATCH] Add support for custom discord emojis.

---
 src/matrixeventprocessor.ts       | 25 +++++++++++++++++++++++++
 src/messageprocessor.ts           |  3 ++-
 test/test_matrixeventprocessor.ts |  1 +
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts
index 5506117..3d6aeea 100644
--- a/src/matrixeventprocessor.ts
+++ b/src/matrixeventprocessor.ts
@@ -11,6 +11,8 @@ import * as log from "npmlog";
 const MaxFileSize = 8000000;
 const MIN_NAME_LENGTH = 2;
 const MAX_NAME_LENGTH = 32;
+const DISCORD_EMOJI_REGEX = /:(\w+):/g;
+
 export class MatrixEventProcessorOpts {
     constructor(
         readonly config: DiscordBridgeConfig,
@@ -45,6 +47,10 @@ export class MatrixEventProcessor {
         if (this.config.bridge.disableHereMention) {
             body = body.replace(new RegExp(`@here`, "g"), "@ here");
         }
+
+        // Handle discord custom emoji
+        body = this.ReplaceDiscordEmoji(body, channel.guild);
+
         let displayName = event.sender;
         let avatarUrl = undefined;
         if (profile) {
@@ -92,6 +98,25 @@ export class MatrixEventProcessor {
         return body;
     }
 
+    public ReplaceDiscordEmoji(content: string, guild: Discord.Guild): string {
+        console.log("Gonna replace")
+        let results = DISCORD_EMOJI_REGEX.exec(content);
+        while (results !== null) {
+            const emojiName = results[1];
+            const emojiNameWithColons = results[0];
+
+            // Check if this emoji exists in the guild
+            if(guild.emojis[emojiName] !== null) {
+                // Replace :a: with <:a:123ID123>
+                const emojiID = guild.emojis.find((emoji) => { return emoji.name === emojiName }).id;
+                content = content.replace(emojiNameWithColons, `<${emojiNameWithColons}${emojiID}>`);
+            }
+
+            results = DISCORD_EMOJI_REGEX.exec(content);
+        }
+        return content;
+    }
+
     public async HandleAttachment(event: any, mxClient: any): Promise<string|Discord.FileOptions> {
         const hasAttachment = [
             "m.image",
diff --git a/src/messageprocessor.ts b/src/messageprocessor.ts
index f028933..919322f 100644
--- a/src/messageprocessor.ts
+++ b/src/messageprocessor.ts
@@ -25,7 +25,6 @@ export class MessageProcessorOpts {
     constructor (readonly domain: string, readonly bot: DiscordBot = null) {
 
     }
-
 }
 
 export class MessageProcessorMatrixResult {
@@ -163,7 +162,9 @@ export class MessageProcessor {
                     `Could not insert emoji ${id} for msg ${msg.id} in guild ${msg.guild.id}: ${ex}`,
                 );
             }
+
             results = EMOJI_REGEX.exec(content);
+
         }
         return content;
     }
diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts
index 07c4efe..f4e032e 100644
--- a/test/test_matrixeventprocessor.ts
+++ b/test/test_matrixeventprocessor.ts
@@ -204,6 +204,7 @@ describe("MatrixEventProcessor", () => {
             }, {avatar_url: "test"}, mockChannel as any);
             Chai.assert.equal(evt.description, "@ here Hello!");
         });
+        // TODO: Add a test for replaceDiscordEmoji
     });
     describe("FindMentionsInPlainBody", () => {
         it("processes mentioned username correctly", async () => {
-- 
GitLab