diff --git a/src/matrixeventprocessor.ts b/src/matrixeventprocessor.ts index 9595abbd0c198e955ba5c20923a09bf9139a0701..3e20a85555195177587427e1b29d29246b7cfa62 100644 --- a/src/matrixeventprocessor.ts +++ b/src/matrixeventprocessor.ts @@ -79,14 +79,15 @@ export class MatrixEventProcessor { } public FindMentionsInPlainBody(body: string, members: Discord.GuildMember[]): string { + const WORD_BOUNDARY = "(^|\:|\@|\#|```|\\s|$|,)"; for (const member of members) { const matcher = escapeStringRegexp(member.user.username + "#" + member.user.discriminator) + "|" + escapeStringRegexp(member.displayName); - body = body.replace( - new RegExp( - `\\b(${matcher})(?=\\b)` - , "mig"), `<@!${member.id}>`, - ); + const regex = new RegExp( + `(${WORD_BOUNDARY})(${matcher})(?=${WORD_BOUNDARY})` + , "igmu"); + + body = body.replace(regex, `$1<@!${member.id}>`); } return body; } diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts index 67636e33740bc5d0a98b487a80efd63b6688fc8f..343558cd114b3daf669d01e391cdf266d5ca874d 100644 --- a/test/test_matrixeventprocessor.ts +++ b/test/test_matrixeventprocessor.ts @@ -204,12 +204,22 @@ describe("MatrixEventProcessor", () => { username: "TestUsername", id: "12345", }, + }), new Discord.GuildMember(guild, { + nick: "𝖘𝖔𝖒𝖊𝖋𝖆𝖓𝖈𝖞𝖓𝖎𝖈𝖐𝖓𝖆𝖒𝖊", + user: { + username: "SomeFancyNickname", + id: "66666", + }, })]; Chai.assert.equal(processor.FindMentionsInPlainBody("Hello TestNickname", members), "Hello <@!12345>"); Chai.assert.equal(processor.FindMentionsInPlainBody("TestNickname: Hello", members), "<@!12345>: Hello"); Chai.assert.equal(processor.FindMentionsInPlainBody("TestNickname, Hello", members), "<@!12345>, Hello"); Chai.assert.equal(processor.FindMentionsInPlainBody("TestNickname Hello", members), "<@!12345> Hello"); Chai.assert.equal(processor.FindMentionsInPlainBody("testNicKName Hello", members), "<@!12345> Hello"); + Chai.assert.equal( + processor.FindMentionsInPlainBody("𝖘𝖔𝖒𝖊𝖋𝖆𝖓𝖈𝖞𝖓𝖎𝖈𝖐𝖓𝖆𝖒𝖊 Hello", members), + "<@!66666> Hello", + ); Chai.assert.equal( processor.FindMentionsInPlainBody("I wish TestNickname was here", members), "I wish <@!12345> was here",