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.
This commit is contained in:
Edward Jones 2019-08-08 12:06:07 -03:00 committed by GitHub
parent 70e88498de
commit d528aedb6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 1 deletions

View File

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

View File

@ -37,6 +37,7 @@ class Bot {
this.discordToken = options.discordToken;
this.commandCharacters = options.commandCharacters || [];
this.ircNickColor = options.ircNickColor !== false; // default to true
this.ircPreventMention = options.ircPreventMention === true; // default: false
this.channels = _.values(options.channelMapping);
this.ircStatusNotices = options.ircStatusNotices;
this.announceSelfJoin = options.announceSelfJoin;
@ -325,9 +326,16 @@ class Bot {
const nickname = Bot.getDiscordNicknameOnServer(author, fromGuild);
let text = this.parseText(message);
let displayUsername = nickname;
if (this.ircPreventMention) {
// Prevent users of both IRC and Discord from
// being mentioned in IRC when they talk in Discord.
displayUsername = `${displayUsername.slice(0, 1)}\u200B${displayUsername.slice(1)}`;
}
if (this.ircNickColor) {
const colorIndex = (nickname.charCodeAt(0) + nickname.length) % NICK_COLORS.length;
displayUsername = irc.colors.wrap(NICK_COLORS[colorIndex], nickname);
displayUsername = irc.colors.wrap(NICK_COLORS[colorIndex], displayUsername);
}
const patternMap = {

View File

@ -336,6 +336,33 @@ describe('Bot', function () {
}
);
it('should break mentions when ircPreventMention is enabled', function () {
const newConfig = { ...config, ircPreventMention: true };
this.bot = new Bot(newConfig);
this.bot.connect();
const text = 'testmessage';
const username = 'otherauthor';
const brokenNickname = 'o\u200Btherauthor';
const message = {
content: text,
mentions: { users: [] },
channel: {
name: 'discord'
},
author: {
username,
id: 'not bot id'
},
guild: this.guild
};
this.bot.sendToIRC(message);
// Wrap in colors:
const expected = `<\u000304${brokenNickname}\u000f> ${text}`;
ClientStub.prototype.say.should.have.been.calledWith('#irc', expected);
});
it('should parse text from discord when sending messages', function () {
const text = '<#1234>';
const message = {