From fc452e7e7e4a53f71571052cf24d961d62fe2ab2 Mon Sep 17 00:00:00 2001
From: Will Hunt <will@half-shot.uk>
Date: Mon, 14 May 2018 16:47:27 +0100
Subject: [PATCH] Eat @ if captured in a mention

---
 src/matrixeventprocessor.ts       | 4 ++--
 test/test_matrixeventprocessor.ts | 6 +++++-
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts
index 92cdd4f..f0ef001 100644
--- a/src/matrixeventprocessor.ts
+++ b/src/matrixeventprocessor.ts
@@ -79,12 +79,12 @@ export class MatrixEventProcessor {
     }
 
     public FindMentionsInPlainBody(body: string, members: Discord.GuildMember[]): string {
-        const WORD_BOUNDARY = "(^|\:|\@|\#|```|\\s|$|,)";
+        const WORD_BOUNDARY = "(^|\:|\#|```|\\s|$|,)";
         for (const member of members) {
             const matcher = escapeStringRegexp(member.user.username + "#" + member.user.discriminator) + "|" +
                 escapeStringRegexp(member.displayName);
             const regex = new RegExp(
-                    `(${WORD_BOUNDARY})(${matcher})(?=${WORD_BOUNDARY})`
+                    `(${WORD_BOUNDARY})(@?(${matcher}))(?=${WORD_BOUNDARY})`
                     , "igmu");
 
             body = body.replace(regex, `$1<@!${member.id}>`);
diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts
index 343558c..611a49d 100644
--- a/test/test_matrixeventprocessor.ts
+++ b/test/test_matrixeventprocessor.ts
@@ -133,7 +133,7 @@ describe("MatrixEventProcessor", () => {
                     body: "@testuser2 Hello!",
                 },
             }, {avatar_url: "test"}, mockChannel as any);
-            Chai.assert.equal(evt.description, "@<@!12345> Hello!");
+            Chai.assert.equal(evt.description, "<@!12345> Hello!");
         });
 
         it("Should disable mentions if configured.", () => {
@@ -232,6 +232,10 @@ describe("MatrixEventProcessor", () => {
                 processor.FindMentionsInPlainBody("TestNickname was here with Test", members),
                 "<@!12345> was here with <@!54321>",
             );
+            Chai.assert.equal(
+                processor.FindMentionsInPlainBody("Fixing this issue provided by @Test", members),
+                "Fixing this issue provided by <@!54321>",
+            );
         });
         it("processes non-mentions correctly", async () => {
             const processor = createMatrixEventProcessor();
-- 
GitLab