JS/node.js chat bot for multiple channels greeting users on chat

hey, just getting back into JS after about 4 years and thought a twitch bot would be a fun way to do that.
I am trying to greet a user the first time they chat in a specific channel, i originally got the bot to chat but if the same user went to another channel the bot was on they would not be greeted there. i am having trouble figuring out how to achieve this, code below and im sure there is a better way to go about it than what i am trying. please let me know if i left out any key info

const twitchJs = require('twitch-js');
const options = {
  options: {
    debug: true,
  connection: {
    cluster: 'aws',
    reconnect: true,
  identity: {
    username: 'bot',
    password: 'oauth:oathHere'
  channels: channelList,
const client = new twitchJs.client(options);


client.on('connected', (address, port) => {
  client.action(this.channelList, 'Hello, bot has connected.');

var channelList = ['channel1','channel2', 'channel3', 'channel4'];
const greetings = [' what it do yo, wassup?!', ' hello, welcome to the stream!', ' where have you been all my life?', ' about damn time you showed that beautiful face!'];

//create greeted viewers arrays for each channel
const guByChannel = {};
for (i=0; i<channelList.length; i++) {
  guByChannel[i] = {channelName: channelList[i],
                    greetedUsers: [],
                    greet: client.on('chat', (channel, user, message, self) => {
                      if (self) return;
                      if (this.guByChannel[greetedUsers].indexOf(user.username) === -1) {
                        client.say(channel, '@' + user.username + greetings[Math.floor(
                          Math.random() * greetings.length)]);

2 things to add/edits: figured out formatting. on chat this throws an error of “greetedUsers is not defined” so i either need a better way (using the api) to access first time chatters, or a JS answer for accessing the greetedUsers property.

First off, greeting users is generally considered a really bad idea, many people don’t appreciate it so make sure you only do it in channels that specifically asked for your bot to be there and do that.

Secondly, you’re creating the client.on('chat', ... listeners inside a loop, you should really try to have just a single listener for each event type.

A much simpler design would be to just create an object that has an array for each channel, and a single listener, eg:

const greetedUsers = {};

channelList.forEach(channel => greetedUsers[channel] = []);

client.on('chat', (channel, user, message, self) => {
  if (!greedUsers[channel].includes(user.username)) {
    // do your greeting

thank you for this, this code is perfect for filling the object. only problem is now the chat part doesnt work for some reason.

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