diff --git a/src/messageprocessor.ts b/src/messageprocessor.ts index 1cccba6f0c17cd572c8b5e2e80004a92f1efd20c..283b4011257866a0602f36947217beec7b4944b0 100644 --- a/src/messageprocessor.ts +++ b/src/messageprocessor.ts @@ -106,9 +106,12 @@ export class MessageProcessor { public FindMentionsInPlainBody(body: string, members: Discord.GuildMember[]): string { for (const member of members) { - const regex = new RegExp(`\\b(${escapeStringRegexp(member.displayName)})(?=\\b)` , "mg"); + const matcher = escapeStringRegexp(member.user.username + "#" + member.user.discriminator) + "|" + + escapeStringRegexp(member.displayName); body = body.replace( - regex, `<@!${member.id}>`, + new RegExp( + `\\b(${matcher})(?=\\b)` + , "mig"), `<@!${member.id}>`, ); } return body; diff --git a/test/mocks/user.ts b/test/mocks/user.ts index d66db47989c99d347fab338e7eb36208a15ea7b4..a2d7574c997d94eacb11eccc85a687d1e78682ce 100644 --- a/test/mocks/user.ts +++ b/test/mocks/user.ts @@ -1,6 +1,7 @@ export class MockUser { public id = ""; public username: string; + public discriminator: string; constructor(id: string, username: string = "") { this.id = id; this.username = username; diff --git a/test/test_messageprocessor.ts b/test/test_messageprocessor.ts index de0f6be67cb7f09166c90257c482bb2f18c95b5f..bc756796a806e92f38b339886c03834ee6909ca1 100644 --- a/test/test_messageprocessor.ts +++ b/test/test_messageprocessor.ts @@ -122,11 +122,17 @@ describe("MessageProcessor", () => { user: { username: "TestUsername", id: "12345", + discriminator: "54321", }, })]; - const msg = "Hello TestUsername"; - const content = processor.FindMentionsInPlainBody(msg, members); - Chai.assert.equal(content, "Hello <@!12345>"); + Chai.assert.equal( + processor.FindMentionsInPlainBody("Hello TestUsername", members), + "Hello <@!12345>", + ); + Chai.assert.equal( + processor.FindMentionsInPlainBody("Hello TestUsername#54321", members), + "Hello <@!12345>", + ); }); it("processes mentioned nickname correctly", async () => { const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot); @@ -148,6 +154,7 @@ describe("MessageProcessor", () => { 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("I wish TestNickname was here", members), "I wish <@!12345> was here",