diff --git a/src/messageprocessor.ts b/src/messageprocessor.ts index 058f1199096f4dde88a37b4f165ba9045ec5f99c..f276324c309e52874b2f16e03eb152e9d0d3841b 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 fcf72612ce3d6a430945a9e437bfbfd45f13e2a7..b8b39eed9377b53523307fb8a8d0f6ae29bdaf8f 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"); + }); + }); });