Skip to content

Commit 0359657

Browse files
committed
Warn if a part/quit is received and no channelUsers is set
Attempts to fix #216's crash but is unlikely to fix the underlying issue (that somehow the client isn't saving a list of nicks in a channel, either because the server isn't sending a names event, or the node-irc library is processing it poorly, or otherwise).
1 parent aefd88c commit 0359657

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/bot.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ class Bot {
146146
delete this.channelUsers[channel];
147147
return;
148148
}
149-
this.channelUsers[channel].delete(nick);
149+
if (this.channelUsers[channel]) {
150+
this.channelUsers[channel].delete(nick);
151+
} else {
152+
logger.warn(`No channelUsers found for ${channel} when ${nick} parted.`);
153+
}
150154
this.sendExactToDiscord(channel, `*${nick}* has left the channel (${reason})`);
151155
});
152156

@@ -155,6 +159,10 @@ class Bot {
155159
if (!this.ircStatusNotices || nick === this.nickname) return;
156160
channels.forEach((channelName) => {
157161
const channel = channelName.toLowerCase();
162+
if (!this.channelUsers[channel]) {
163+
logger.warn(`No channelUsers found for ${channel} when ${nick} quit, ignoring.`);
164+
return;
165+
}
158166
if (!this.channelUsers[channel].delete(nick)) return;
159167
this.sendExactToDiscord(channel, `*${nick}* has quit (${reason})`);
160168
});

test/bot-events.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,19 @@ describe('Bot Events', function () {
263263
bot.sendExactToDiscord.should.not.have.been.called;
264264
});
265265

266+
it('should warn if it receives a part/quit before a names event', function () {
267+
const bot = createBot({ ...config, ircStatusNotices: true });
268+
bot.connect();
269+
const channel = '#channel';
270+
const reason = 'Leaving';
271+
272+
bot.ircClient.emit('part', channel, 'user1', reason);
273+
bot.ircClient.emit('quit', 'user2', reason, [channel]);
274+
this.warnSpy.should.have.been.calledTwice;
275+
this.warnSpy.getCall(0).args.should.deep.equal([`No channelUsers found for ${channel} when user1 parted.`]);
276+
this.warnSpy.getCall(1).args.should.deep.equal([`No channelUsers found for ${channel} when user2 quit, ignoring.`]);
277+
});
278+
266279
it('should not listen to discord debug messages in production', function () {
267280
logger.level = 'info';
268281
const bot = createBot();

0 commit comments

Comments
 (0)