diff --git a/lib/bot.js b/lib/bot.js index e4295297..f223c5d7 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -48,7 +48,6 @@ class Bot { this.formatCommandPrelude = this.format.commandPrelude || 'Command sent from Discord by {$nickname}:'; this.formatIRCText = this.format.ircText || '<{$displayUsername}> {$text}'; this.formatURLAttachment = this.format.urlAttachment || '<{$displayUsername}> {$attachmentURL}'; - // "{$keyName}" => "variableValue" // author: IRC nickname // text: the (Discord-formatted) message content @@ -125,6 +124,20 @@ class Bot { this.sendToDiscord(author, to, `*${text}*`); }); + this.ircClient.on('nick', (oldNick, newNick, channels) => { + if (!this.ircStatusNotices) return; + channels.forEach((channelName) => { + const channel = channelName.toLowerCase(); + if (this.channelUsers[channel]) { + this.channelUsers[channel].delete(oldNick); + this.channelUsers[channel].add(newNick); + this.sendExactToDiscord(channel, `*${oldNick}* is now known as ${newNick}`); + } else { + logger.warn(`No channelUsers found for ${channel} when ${oldNick} changed.`); + } + }); + }); + this.ircClient.on('join', (channelName, nick) => { logger.debug('Received join:', channelName, nick); if (!this.ircStatusNotices) return; diff --git a/test/bot-events.test.js b/test/bot-events.test.js index 44f8fb1f..d3d18a83 100644 --- a/test/bot-events.test.js +++ b/test/bot-events.test.js @@ -113,6 +113,31 @@ describe('Bot Events', function () { this.bot.sendToDiscord.should.have.been.calledWithExactly(author, channel, formattedText); }); + it('should not send name change event to discord', function () { + const channel = '#channel'; + const oldnick = 'user1'; + const newnick = 'user2'; + this.bot.ircClient.emit('nick', oldnick, newnick, [channel]); + this.bot.sendExactToDiscord.should.not.have.been.called; + }); + + it('should send name change event to discord', function () { + const channel1 = '#channel1'; + const channel2 = '#channel2'; + const oldNick = 'user1'; + const newNick = 'user2'; + const bot = createBot({ ...config, ircStatusNotices: true }); + bot.connect(); + bot.ircClient.emit('names', '#channel1', { [bot.nickname]: '', [oldNick]: '' }); + const channelNicksPre = new Set([bot.nickname, oldNick]); + bot.channelUsers.should.deep.equal({ '#channel1': channelNicksPre }); + const formattedText = `*${oldNick}* is now known as ${newNick}`; + const channelNicksAfter = new Set([bot.nickname, newNick]); + bot.ircClient.emit('nick', oldNick, newNick, [channel1, channel2]); + bot.sendExactToDiscord.should.have.been.calledWithExactly(channel1, formattedText); + bot.channelUsers.should.deep.equal({ '#channel1': channelNicksAfter }); + }); + it('should send actions to discord', function () { const channel = '#channel'; const author = 'user';