Skip to content

Commit d528aed

Browse files
authored
Add ircPreventMention (#502)
* Add ircPreventMention (This adds a zero-width character to nicknames to prevent IRC-side pings.) Original commits: - Add ircPreventMention code - Document ircPreventMention - Make ircNickColor wrap the displayUsername - Fix build errors - Default ircPreventMention to false * Correct that ircPreventMention is off by default * Test ircPreventMention Only tests turning it on; extant tests ensure it's off by default.
1 parent 70e8849 commit d528aed

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ First you need to create a Discord bot user, which you can do by following the i
8888
"webhookAvatarURL": "https://robohash.org/{$nickname}" // Default avatar to use for webhook messages
8989
},
9090
"ircNickColor": false, // Gives usernames a color in IRC for better readability (on by default)
91+
"ircPreventMention": true, // Prevents users of both IRC and Discord from being mentioned in IRC when they speak in Discord (off by default)
9192
// Makes the bot hide the username prefix for messages that start
9293
// with one of these characters (commands):
9394
"commandCharacters": ["!", "."],

lib/bot.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Bot {
3737
this.discordToken = options.discordToken;
3838
this.commandCharacters = options.commandCharacters || [];
3939
this.ircNickColor = options.ircNickColor !== false; // default to true
40+
this.ircPreventMention = options.ircPreventMention === true; // default: false
4041
this.channels = _.values(options.channelMapping);
4142
this.ircStatusNotices = options.ircStatusNotices;
4243
this.announceSelfJoin = options.announceSelfJoin;
@@ -325,9 +326,16 @@ class Bot {
325326
const nickname = Bot.getDiscordNicknameOnServer(author, fromGuild);
326327
let text = this.parseText(message);
327328
let displayUsername = nickname;
329+
330+
if (this.ircPreventMention) {
331+
// Prevent users of both IRC and Discord from
332+
// being mentioned in IRC when they talk in Discord.
333+
displayUsername = `${displayUsername.slice(0, 1)}\u200B${displayUsername.slice(1)}`;
334+
}
335+
328336
if (this.ircNickColor) {
329337
const colorIndex = (nickname.charCodeAt(0) + nickname.length) % NICK_COLORS.length;
330-
displayUsername = irc.colors.wrap(NICK_COLORS[colorIndex], nickname);
338+
displayUsername = irc.colors.wrap(NICK_COLORS[colorIndex], displayUsername);
331339
}
332340

333341
const patternMap = {

test/bot.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,33 @@ describe('Bot', function () {
336336
}
337337
);
338338

339+
it('should break mentions when ircPreventMention is enabled', function () {
340+
const newConfig = { ...config, ircPreventMention: true };
341+
this.bot = new Bot(newConfig);
342+
this.bot.connect();
343+
344+
const text = 'testmessage';
345+
const username = 'otherauthor';
346+
const brokenNickname = 'o\u200Btherauthor';
347+
const message = {
348+
content: text,
349+
mentions: { users: [] },
350+
channel: {
351+
name: 'discord'
352+
},
353+
author: {
354+
username,
355+
id: 'not bot id'
356+
},
357+
guild: this.guild
358+
};
359+
360+
this.bot.sendToIRC(message);
361+
// Wrap in colors:
362+
const expected = `<\u000304${brokenNickname}\u000f> ${text}`;
363+
ClientStub.prototype.say.should.have.been.calledWith('#irc', expected);
364+
});
365+
339366
it('should parse text from discord when sending messages', function () {
340367
const text = '<#1234>';
341368
const message = {

0 commit comments

Comments
 (0)