Skip to content
Extraits de code Groupes Projets
Valider f8a8e575 rédigé par Will Hunt's avatar Will Hunt
Parcourir les fichiers

Make the damn bloody mention regex work for good this time.

parent a66f88e1
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -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;
......
......@@ -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);
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter