Skip to content
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

The framework constantly crashes - "Cannot call method 'replace' of undefined" #364

Closed
Zamiell opened this issue May 13, 2015 · 11 comments
Closed

Comments

@Zamiell
Copy link

Zamiell commented May 13, 2015

I'm not sure what exactly causes this, but my IRC bot routinely crashes over the course of normal operation. The error message is always the same:

TypeError: Cannot call method 'replace' of undefined
    at /opt/ZamielBot/node_modules/irc/lib/irc.js:278:71
    at Array.forEach (native)
    at Client.<anonymous> (/opt/ZamielBot/node_modules/irc/lib/irc.js:259:26)
    at Client.emit (events.js:95:17)
    at iterator (/opt/ZamielBot/node_modules/irc/lib/irc.js:756:26)
    at Array.forEach (native)
    at Socket.<anonymous> (/opt/ZamielBot/node_modules/irc/lib/irc.js:751:15)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:765:14)
    at Socket.emit (events.js:92:17)

My bot will just echo traffic from one IRC server to another.
Here is the source code:

#! /usr/bin/env node

/* Variables */
var irc = require('irc');
var SRLBot = new irc.Client('irc.speedrunslive.com', 'ZamielBot', {
    debug: true,
    channels: ['#speedrunslive']
});
var TwitchBot = new irc.Client('irc.twitch.tv', 'ZamielBot', {
    debug: true,
    channels: ['#zamiell'],
    password: 'asdf'
});

/* Catch errors */
SRLBot.addListener('error', function(message) {
    console.error('SRL ERROR: %s: %s', message.command, message.args.join(' '));
});
TwitchBot.addListener('error', function(message) {
    console.error('TWITCH ERROR: %s: %s', message.command, message.args.join(' '));
});

/* Catch messages */
SRLBot.addListener('message', function(user, channel, message) {
    console.log('SRL [%s] <%s> %s', channel, user, message);
    TwitchBot.say('#zamiell', '<' + user + '> ' + message);
});
TwitchBot.addListener('message', function(user, channel, message) {
    console.log('Twitch [%s] <%s> %s', channel, user, message);
});

I'm assuming that the error lies in the framework, as the bot code is relatively simple. Can anyone shed some insight?

@sim642
Copy link
Contributor

sim642 commented May 14, 2015

The line on which the error happens seems to be regarding user modes in channel which have been known to be an issue with twitch IRC (#340). Although I don't think it should still be crashing maybe you could try using another network to test if it still happens.

@Zamiell
Copy link
Author

Zamiell commented May 14, 2015

So perhaps when a moderator enters my Twitch chat, the bot will crash? I will test this.

@Zamiell
Copy link
Author

Zamiell commented May 14, 2015

Having a moderator enter the channel does not cause the crash. It must be something else.
Output from the script:

14 May 11:26:32 - MODE: #zamiell sets mode: +o

@sim642
Copy link
Contributor

sim642 commented May 14, 2015

The erroring line has to do with mode removal. Maybe Twitch does something weird when moderators leave?

@Zamiell
Copy link
Author

Zamiell commented May 14, 2015

That was it! That causes the crash. Is there a way to turn on a deep debugging mode or something so that I can see the raw IRC message that is being sent to the framework? If not, let me know what else you need me to do. =)

@sim642
Copy link
Contributor

sim642 commented May 14, 2015

I'm guessing right now that Twitch's IRC implementation sends MODE when a moderator leaves but in actual IRC protocol that is not necessary. There is an debug: true option you can set for a Client but I'm not sure if that reveals anything more.

Also, I don't actually develop or maintain node-irc so I don't really "need" anything.

@Zamiell
Copy link
Author

Zamiell commented May 14, 2015

Yeah, I already have debug: true set, but it doesn't give me that kind of granularity. While I wait for an official maintainer to respond, are you familiar enough with the code to suggest a patch to stop this particular crashing? (As it would be nice to have an interim fix for the issue before the official repo is patched.)

@sim642
Copy link
Contributor

sim642 commented May 14, 2015

This is completely untested but adding an extra check should avoid it from trying to manipulate the impossible:

--- /home/simmo/Desktop/irc.js
+++ /home/simmo/Desktop/irc2.js
@@ -268,15 +268,17 @@
                     if (mode in self.prefixForMode) {
                         // channel user modes
                         var user = modeArgs.shift();
-                        if (adding) {
-                            if (channel.users[user] && channel.users[user].indexOf(self.prefixForMode[mode]) === -1)
-                                channel.users[user] += self.prefixForMode[mode];
-
-                            self.emit('+mode', message.args[0], message.nick, mode, user, message);
-                        }
-                        else {
-                            channel.users[user] = channel.users[user].replace(self.prefixForMode[mode], '');
-                            self.emit('-mode', message.args[0], message.nick, mode, user, message);
+                        if (user in channel.users) {
+                            if (adding) {
+                                if (channel.users[user] && channel.users[user].indexOf(self.prefixForMode[mode]) === -1)
+                                    channel.users[user] += self.prefixForMode[mode];
+
+                                self.emit('+mode', message.args[0], message.nick, mode, user, message);
+                            }
+                            else {
+                                channel.users[user] = channel.users[user].replace(self.prefixForMode[mode], '');
+                                self.emit('-mode', message.args[0], message.nick, mode, user, message);
+                            }
                         }
                     }
                     else {

@Zamiell
Copy link
Author

Zamiell commented May 14, 2015

Thank you so much! I'll give it a whirl later on tonight when I get home from work. =D

@Zamiell
Copy link
Author

Zamiell commented May 15, 2015

Looks like this fix works! Thanks again sim642.

@ghost
Copy link

ghost commented Aug 31, 2015

This should be merged into master. I'm getting crashes on Freenode too:

/home/lorenzo/bot/node_modules/irc/lib/irc.js:748
                        throw err;
                              ^
TypeError: Cannot call method 'replace' of undefined
    at /home/lorenzo/bot/node_modules/irc/lib/irc.js:278:71
    at Array.forEach (native)
    at Client.<anonymous> (/home/lorenzo/bot/node_modules/irc/lib/irc.js:259:26)
    at Client.emit (events.js:95:17)
    at iterator (/home/lorenzo/bot/node_modules/irc/lib/irc.js:745:26)
    at Array.forEach (native)
    at Socket.<anonymous> (/home/lorenzo/bot/node_modules/irc/lib/irc.js:740:15)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:765:14)
    at Socket.emit (events.js:92:17)
error: Forever detected script exited with code: 8
error: Script restart attempt #66

@jirwin jirwin closed this as completed in 09e509c Sep 3, 2015
jirwin added a commit that referenced this issue Sep 3, 2015
Fix channel user modes on Twitch IRC (closes #364)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants