diff --git a/src/messageprocessor.ts b/src/messageprocessor.ts index 3d186bf98fd105f22996ca0b0a64fad172354180..1cccba6f0c17cd572c8b5e2e80004a92f1efd20c 100644 --- a/src/messageprocessor.ts +++ b/src/messageprocessor.ts @@ -106,8 +106,9 @@ 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"); body = body.replace( - new RegExp(`(^| |\\t)(${escapeStringRegexp(member.displayName)})($| |\\t)` , "mg"), ` <@!${member.id}>`, + regex, `<@!${member.id}>`, ); } return body; diff --git a/test/test_messageprocessor.ts b/test/test_messageprocessor.ts index a793d3af14f068542258ec475e6bbc2e2e45183d..de0f6be67cb7f09166c90257c482bb2f18c95b5f 100644 --- a/test/test_messageprocessor.ts +++ b/test/test_messageprocessor.ts @@ -132,15 +132,34 @@ describe("MessageProcessor", () => { const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot); const guild: any = new MockGuild("123", []); const members: Discord.GuildMember[] = [new Discord.GuildMember(guild, { + nick: "Test", + user: { + username: "Test", + id: "54321", + }, + }), new Discord.GuildMember(guild, { nick: "TestNickname", user: { username: "TestUsername", id: "12345", }, })]; - const msg = "Hello TestNickname"; - const content = processor.FindMentionsInPlainBody(msg, members); - Chai.assert.equal(content, "Hello <@!12345>"); + 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("I wish TestNickname was here", members), + "I wish <@!12345> was here", + ); + Chai.assert.equal( + processor.FindMentionsInPlainBody("I wish TestNickname was here, TestNickname is cool", members), + "I wish <@!12345> was here, <@!12345> is cool", + ); + Chai.assert.equal( + processor.FindMentionsInPlainBody("TestNickname was here with Test", members), + "<@!12345> was here with <@!54321>", + ); }); it("processes non-mentions correctly", async () => { const processor = new MessageProcessor(new MessageProcessorOpts("localhost"), <DiscordBot> bot);