From 823392e6100eab802152a04f4b143dfc2235793a Mon Sep 17 00:00:00 2001
From: Will Hunt <half-shot@molrams.com>
Date: Thu, 14 Sep 2017 01:23:27 +0100
Subject: [PATCH] Add support for parsing embeds on discord messages.

---
 src/messageprocessor.ts       | 15 ++++++-
 test/test_messageprocessor.ts | 76 +++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+), 1 deletion(-)

diff --git a/src/messageprocessor.ts b/src/messageprocessor.ts
index 058f119..f276324 100644
--- a/src/messageprocessor.ts
+++ b/src/messageprocessor.ts
@@ -32,8 +32,11 @@ export class MessageProcessor {
 
     public async FormatDiscordMessage(msg: Discord.Message): Promise<MessageProcessorMatrixResult> {
         const result = new MessageProcessorMatrixResult();
-        // Replace Users
+        // Replace embeds.
         let content = msg.content;
+        content = this.InsertEmbeds(content, msg);
+
+        // Replace Users
         content = this.ReplaceMembers(content, msg);
         content = this.ReplaceChannels(content, msg);
         content = await this.ReplaceEmoji(content, msg);
@@ -43,6 +46,16 @@ export class MessageProcessor {
         return result;
     }
 
+    public InsertEmbeds(content: string, msg: Discord.Message): string {
+        for (let embed of msg.embeds) {
+            let embedContent = "\n----\n"; // Horizontal rule.
+            const embedTitle = embed.url ? `[${embed.title}](${embed.url})` : embed.title;
+            embedContent += embedTitle != null ? `#### ${embedTitle}\n\n${embed.description}` : embed.description;
+            content += embedContent;
+        }
+        return content;
+    }
+
     public ReplaceMembers(content: string, msg: Discord.Message): string {
         let results = USER_REGEX.exec(content);
         while (results !== null) {
diff --git a/test/test_messageprocessor.ts b/test/test_messageprocessor.ts
index fcf7261..b8b39ee 100644
--- a/test/test_messageprocessor.ts
+++ b/test/test_messageprocessor.ts
@@ -35,6 +35,7 @@ describe("MessageProcessor", () => {
       it("processes plain text messages correctly", async () => {
         const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot);
         const msg = new Discord.Message(null, null, null);
+        msg.embeds = [];
         msg.content = "Hello World!";
         const result = await processor.FormatDiscordMessage(msg);
         Chai.assert(result.body, "Hello World!");
@@ -43,6 +44,7 @@ describe("MessageProcessor", () => {
       it("processes markdown messages correctly.", async () => {
         const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot);
         const msg = new Discord.Message(null, null, null);
+        msg.embeds = [];
         msg.content = "Hello *World*!";
         const result = await processor.FormatDiscordMessage(msg);
         Chai.assert.equal(result.body, "Hello *World*!");
@@ -162,4 +164,78 @@ describe("MessageProcessor", () => {
             Chai.assert.equal(content, "Welcome thatman");
         });
     });
+    describe("InsertEmbeds", () => {
+        it("processes titleless embeds properly", () => {
+            const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot);
+            const msg = new Discord.Message(null, null, null);
+            msg.embeds = [
+                new Discord.MessageEmbed(msg, {
+                    description: "TestDescription"
+                })
+            ]
+            const inContent = "";
+            const content = processor.InsertEmbeds(inContent, msg);
+            Chai.assert.equal(content, "\n----\nTestDescription");
+        });
+        it("processes urlless embeds properly", () => {
+            const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot);
+            const msg = new Discord.Message(null, null, null);
+            msg.embeds = [
+                new Discord.MessageEmbed(msg, {
+                    title: "TestTitle",
+                    description: "TestDescription",
+                })
+            ]
+            const inContent = "";
+            const content = processor.InsertEmbeds(inContent, msg);
+            Chai.assert.equal(content, "\n----\n#### TestTitle\n\nTestDescription");
+        });
+        it("processes linked embeds properly", () => {
+            const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot);
+            const msg = new Discord.Message(null, null, null);
+            msg.embeds = [
+                new Discord.MessageEmbed(msg, {
+                    title: "TestTitle",
+                    url: "testurl",
+                    description: "TestDescription",
+                })
+            ]
+            const inContent = "";
+            const content = processor.InsertEmbeds(inContent, msg);
+            Chai.assert.equal(content, "\n----\n#### [TestTitle](testurl)\n\nTestDescription");
+        });
+        it("processes multiple embeds properly", () => {
+            const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot);
+            const msg = new Discord.Message(null, null, null);
+            msg.embeds = [
+                new Discord.MessageEmbed(msg, {
+                    title: "TestTitle",
+                    url: "testurl",
+                    description: "TestDescription",
+                }),
+                new Discord.MessageEmbed(msg, {
+                    title: "TestTitle2",
+                    url: "testurl2",
+                    description: "TestDescription2",
+                })
+            ]
+            const inContent = "";
+            const content = processor.InsertEmbeds(inContent, msg);
+            Chai.assert.equal(content, "\n----\n#### [TestTitle](testurl)\n\nTestDescription\n----\n#### [TestTitle2](testurl2)\n\nTestDescription2");
+        });
+        it("inserts embeds properly", () => {
+            const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot);
+            const msg = new Discord.Message(null, null, null);
+            msg.embeds = [
+                new Discord.MessageEmbed(msg, {
+                    title: "TestTitle",
+                    url: "testurl",
+                    description: "TestDescription",
+                })
+            ]
+            const inContent = "Content that goes in the message";
+            const content = processor.InsertEmbeds(inContent, msg);
+            Chai.assert.equal(content, "Content that goes in the message\n----\n#### [TestTitle](testurl)\n\nTestDescription");
+        });
+    });
 });
-- 
GitLab