Code help for my simple Node.js chatbot?

A little help on this snippet of code? I am running it on 3 channels simultaneously. I want the first client.on("subscription" to listen on Channel1 only and respond only on Channel1 when that event occurs on Channel1. Same for Channel2 and Channel3.
As the code is right now, if a subscription occurs on any channel, the bot would reply on all 3 channels. I’ve tried everything I know how to do, including bracketing each section with various if channel="Channel1" conditions, but nothing seems to work correctly. Not sure of the “if” syntax to use for this particular application.

var tmi = require('tmi.js');

var options = {
	options: {
		debug: true
	},
	connection: {
		cluster: "aws",
		reconnect: true
	},
	identity: {
		username: "forbidinjustice",
		password: "oauth:redacted"
	},
	channels: ["Channel1","Channel2","Channel3"]
};

var client = new tmi.client(options);
client.connect();

// OPEN

client.on("subscription", function (channel, username, method) {
    client.say("Channel1", "Thanks for the sub to Channel 1! · MorphinTime");

});

client.on("subscription", function (channel, username, method) {
    client.say("Channel2", "Thanks for the sub to Channel 2! · MorphinTime");

});

client.on("subscription", function (channel, username, method) {
    client.say("Channel3", "Thanks for the sub to Channel 3! · MorphinTime");

});

Register only one listener for subscription and do something like

switch (channel) {
case "channel1":
  // handle channel1
  break;
case "channel2":
  // handle channel2
  break;
  // and so on
}

I’m almost a complete newbie so please, bear with me… but am I on the right track? If so, I’ll test it later on a few channels.

var tmi = require('tmi.js');

var options = {
	options: {
		debug: true
	},
	connection: {
		cluster: "aws",
		reconnect: true
	},
	identity: {
		username: "forbidinjustice",
		password: "oauth:redacted"
	},
	channels: ["Channel1","Channel2","Channel3"]
};

var client = new tmi.client(options);
client.connect();

// OPEN


client.on("subscription", function (channel, username, method) {

	switch (channel) {

	case "channel1":
    	client.say("channel1", "Thanks for the sub to Channel 1!");
    break;

    case "channel2":
    	client.say("channel2", "Thanks for the sub to Channel 2!");
    break;

    case "channel3":
    	client.say("channel3", "Thanks for the sub to Channel 3!");
}

});

On the right track, yes.

I tested it and a subscription occurred in the channel and the bot didn’t respond, not even an error in the log. The entire client.on section looks like this:

client.on("subscription", function (channel, username, method) {

	switch (channel) {
	case "channel1":
    	client.say("channel1", "thanks for the sub to channel1");
    break;
    case "channel2":
    	client.say("channel2", "thanks for the sub to channel2");
    break;
    case "channel3":
    	client.say("channel3", "thanks for the sub to channel3");
}
 	});

Adding a console.log(channel) to the subscription function can help you see that it’s firing and that your cases are correct. Keep in mind channel names should be all lowercase in your code; display names are only relevant in the message sent to users.

All channel names are lowercase in the code. Previously (when I coded the bot the first time around), in the Node.js window, when it detected a subscription then fired and the syntax was bad, it would throw an error and then disconnect the bot from the server. Now, it doesn’t even do that, so that tells me it’s not detecting the subscription event at all.

Previous bot worked great, it’s just that I have to run multiple instances of Node.js command prompt until I can figure out how to make certain parts of the code listen on certain channels.

channel starts with “#” – you must remove it from the variable, use a second variable, or add “#” to your channel names.

client.on('subscription', function(_channel, username, method, message, userstate) {
    let channel = _channel.slice(1);
    let output = '';
    switch(channel) {
        case 'channel1':
            output = 'Thanks for the sub to ' + channel;
        case 'channel2':
            output = 'Wowza! Thank you for the sub, ' + (userstate['display-name'] || username);
        case 'channel3':
            output = username + ' just subbed to ' + channel + '!';
    }
    if(output) client.say(channel, output);
});

Thanks for the reply, Alca. So the code you’ve included should be correct? I tested it a few times with the code below. The subscription event occurred on channel1, but the bot sent the response “output test 3” to the chat, which should have only been sent if the event occurred on channel3.

I also tried running 3 blocks of this code, modified so the 1st block is for subscription, 2nd is resub, 3rd is a cheer event. The bot ignored any event on the 1st (sub) and 2nd (resub) blocks of code. But when a cheer occurred on channel1, it sent ‘output test 3’. Basically, it seems like it’s ignoring every bit of code except for very last command in the entire bot, if that makes sense.

client.on('subscription', function(_channel, username, method, message, userstate) {
    let channel = _channel.slice(1);
    let output = '';
    switch(channel) {
        case 'channel1':
            output = 'output test 1';
        case 'channel2':
            output = 'output test 2';
        case 'channel3':
            output = 'output test 3';
    }
    if(output) client.say(channel, output);
});

Oh right, I don’t use switch/case … you need to add break; to the end of each case or else they “fall through.” This is why the last one gets to set the message last.

        case 'channel1':
            output = 'output test 1';
            break;
        case 'channel2':
            output = 'output test 2';
            break;
        case 'channel3':
            output = 'output test 3';
            break;

That did the trick. The bot now seems to be responding in the appropriate chats. Thanks a lot, Alca and 3ventic!

With the recent addition of subscription gifting, did something change within the API as to how subs are handled in general? Basically, all the events in my bot work. I use client.on('subscription', as well as resub, cheer, and ban. Everything works except for any first-time subscription. Whether it be Prime or any Tier sub, in addition to a gifted sub, the bot ignores it. The bot fires perfectly on resubs and every other event. Has anything changed? I’ve looked the code up and down and I’m sure syntax is correct.

If I knew/understood how to listen for events via PubSub, I’m sure my life would be a lot easier…

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.