ekmartin / slack-irc

Connects Slack and IRC channels by sending messages back and forth.
MIT License
588 stars 157 forks source link

Nick Changed on Reconnect #169

Open ilyanep opened 7 years ago

ilyanep commented 7 years ago

Hello! Apologies if this has been brought up, but I couldn't find it in a search.

We're bridging to a channel on Freenode and finding that when the bot loses connection and has to reconnect, it reconnects as 1 (since I guess the old nick hasn't been counted as disconnected yet when it comes back). I tried fixing this by adding the following to config.json, but I found that the bot just winds up with 2 for some reason:

        "autoSendCommands": [
            ["PRIVMSG", "NickServ", "GHOST <BotNick> <key>"],
            ["NICK", "<BotNick>"],
            ["PRIVMSG", "NickServ", "IDENTIFY <key>"]
        ],

Is there some way to either set up these auto sends or to have the functionality in the bot to deal with this (maybe if your nick != the configured nick, wait a configurable amount of time and try again?)

ilyanep commented 7 years ago

Another good alternative might be to add a bot command that just issues a nick for the configured nick.

ekmartin commented 7 years ago

Looking at the code of node-irc it looks like this should be handled already: https://github.com/martynsmith/node-irc/blob/master/lib/irc.js#L230-L237

Maybe this is a non-protocol event though, so it's up to each IRC server how they're handling it?

ilyanep commented 7 years ago

Ohhh, so the bot is doing the appending of the numbers itself? Does that explain why I'm getting 2 the second time? Seems like what might be happening is:

(I haven't looked at the code around to see if that makes sense)

In that case a solution might be to have some ability to tell the bot to set nickMod to undefined again and retry its nick process, or an ability to tell the autosendcmd to wait some amount of time.

ilyanep commented 7 years ago

Also, just realized that's an external library, so maybe I'm asking the wrong person for those things haha

ekmartin commented 7 years ago

It's supposed to append the number to the already existing nick though, not just replace it. See https://github.com/martynsmith/node-irc/blob/master/lib/irc.js#L234

ilyanep commented 7 years ago

Looks to me like it's appending to self.opt.nick which I think is what's in my config file, not self.nick which is already the existing nick?

ekmartin commented 7 years ago

It should set self.opt.nick to the correct nick here.

If you're interested in debugging further you could put a few console.logs inside node-irc' code to see what it's actually sending on the specific event. Should be in node_modules/irc/lib/irc.js if you've cloned the project, or something like /usr/local/lib/node_modules/discord-irc/irc/lib/irc.js if you're running it with the CLI.

ilyanep commented 7 years ago

Good idea. I modified that block to read:

            case 'err_nicknameinuse':
                if (typeof (self.opt.nickMod) == 'undefined')
                    self.opt.nickMod = 0;
                self.opt.nickMod++;
                console.log("Requested nickname in use. Attempting " + self.opt.nick + self.opt.nickMod);
                console.log("self.nick is: " + self.nick);
                console.log("self.opt.nick is: " + self.opt.nick);
                self.send('NICK', self.opt.nick + self.opt.nickMod);
                self.nick = self.opt.nick + self.opt.nickMod;
                self._updateMaxLineLength();
                console.log("Sent nick change.");
                console.log("self.nick is: " + self.nick);
                console.log("self.opt.nick is: " + self.opt.nick);
                break;

Will report back next time my bot suffers a disconnect