diff --git a/lib/bot.js b/lib/bot.js index 8417047a..7eaf0312 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -125,19 +125,24 @@ class Bot { this.sendToDiscord(author, to, `*${text}*`); }); - this.ircClient.on('join', (channel, nick) => { + this.ircClient.on('join', (channelName, nick) => { + logger.debug('Received join:', channelName, nick); if (!this.ircStatusNotices) return; if (nick === this.nickname && !this.announceSelfJoin) return; + const channel = channelName.toLowerCase(); // self-join is announced before names (which includes own nick) // so don't add nick to channelUsers if (nick !== this.nickname) this.channelUsers[channel].add(nick); this.sendExactToDiscord(channel, `*${nick}* has joined the channel`); }); - this.ircClient.on('part', (channel, nick, reason) => { + this.ircClient.on('part', (channelName, nick, reason) => { + logger.debug('Received part:', channelName, nick, reason); if (!this.ircStatusNotices) return; + const channel = channelName.toLowerCase(); // remove list of users when no longer in channel (as it will become out of date) if (nick === this.nickname) { + logger.debug('Deleting channelUsers as bot parted:', channel); delete this.channelUsers[channel]; return; } @@ -146,15 +151,19 @@ class Bot { }); this.ircClient.on('quit', (nick, reason, channels) => { + logger.debug('Received quit:', nick, channels); if (!this.ircStatusNotices || nick === this.nickname) return; - channels.forEach((channel) => { + channels.forEach((channelName) => { + const channel = channelName.toLowerCase(); if (!this.channelUsers[channel].delete(nick)) return; this.sendExactToDiscord(channel, `*${nick}* has quit (${reason})`); }); }); - this.ircClient.on('names', (channel, nicks) => { + this.ircClient.on('names', (channelName, nicks) => { + logger.debug('Received names:', channelName, nicks); if (!this.ircStatusNotices) return; + const channel = channelName.toLowerCase(); this.channelUsers[channel] = new Set(Object.keys(nicks)); }); diff --git a/test/bot-events.test.js b/test/bot-events.test.js index 0766b482..5e7e371e 100644 --- a/test/bot-events.test.js +++ b/test/bot-events.test.js @@ -135,6 +135,16 @@ describe('Bot Events', function () { bot.channelUsers.should.deep.equal({ '#channel': channelNicks }); }); + it('should lowercase the channelUsers mapping', function () { + const bot = createBot({ ...config, ircStatusNotices: true }); + bot.connect(); + const channel = '#channelName'; + const nicks = { [bot.nickname]: '' }; + bot.ircClient.emit('names', channel, nicks); + const channelNicks = new Set([bot.nickname]); + bot.channelUsers.should.deep.equal({ '#channelname': channelNicks }); + }); + it('should send join messages to discord when config enabled', function () { const bot = createBot({ ...config, ircStatusNotices: true }); bot.connect(); @@ -222,6 +232,22 @@ describe('Bot Events', function () { bot.sendExactToDiscord.getCall(1).args.should.deep.equal([channel3, text]); }); + it('should not crash with join/part/quit messages and weird channel casing', function () { + const bot = createBot({ ...config, ircStatusNotices: true }); + bot.connect(); + + function wrap() { + const nick = 'user'; + const reason = 'Leaving'; + bot.ircClient.emit('names', '#Channel', { [bot.nickname]: '' }); + bot.ircClient.emit('join', '#cHannel', nick); + bot.ircClient.emit('part', '#chAnnel', nick, reason); + bot.ircClient.emit('join', '#chaNnel', nick); + bot.ircClient.emit('quit', nick, reason, ['#chanNel']); + } + (wrap).should.not.throw(); + }); + it('should be possible to disable join/part/quit messages', function () { const bot = createBot({ ...config, ircStatusNotices: false }); bot.connect();