Skip to content

Use ircClient.nick instead of nickname when guarding against events for current nick #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ class Bot {
this.ircClient.on('join', (channelName, nick) => {
logger.debug('Received join:', channelName, nick);
if (!this.ircStatusNotices) return;
if (nick === this.nickname && !this.announceSelfJoin) return;
if (nick === this.ircClient.nick && !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);
if (nick !== this.ircClient.nick) this.channelUsers[channel].add(nick);
this.sendExactToDiscord(channel, `*${nick}* has joined the channel`);
});

Expand All @@ -157,7 +157,7 @@ class Bot {
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) {
if (nick === this.ircClient.nick) {
logger.debug('Deleting channelUsers as bot parted:', channel);
delete this.channelUsers[channel];
return;
Expand All @@ -172,7 +172,7 @@ class Bot {

this.ircClient.on('quit', (nick, reason, channels) => {
logger.debug('Received quit:', nick, channels);
if (!this.ircStatusNotices || nick === this.nickname) return;
if (!this.ircStatusNotices || nick === this.ircClient.nick) return;
channels.forEach((channelName) => {
const channel = channelName.toLowerCase();
if (!this.channelUsers[channel]) {
Expand Down
12 changes: 12 additions & 0 deletions test/bot-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ describe('Bot Events', function () {
this.warnSpy.getCall(1).args.should.deep.equal([`No channelUsers found for ${channel} when user2 quit, ignoring.`]);
});

it('should not crash if it uses a different name from config', function () {
// this can happen when a user with the same name is already connected
const bot = createBot({ ...config, nickname: 'testbot' });
bot.connect();
const newName = 'testbot1';
bot.ircClient.nick = newName;
function wrap() {
bot.ircClient.emit('join', '#channel', newName);
}
(wrap).should.not.throw;
});

it('should not listen to discord debug messages in production', function () {
logger.level = 'info';
const bot = createBot();
Expand Down
7 changes: 6 additions & 1 deletion test/stubs/irc-client-stub.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import events from 'events';

class ClientStub extends events.EventEmitter {}
class ClientStub extends events.EventEmitter {
constructor(...args) {
super();
this.nick = args[1];
}
}

export default ClientStub;