-1

So my bot is connected to 3 channels and if all 3 channels are online how can bot only work on First channel if he's going offline so swap to next channel

const tmi = require('tmi.js'),
    { channel, username, password } = require('./settings.json');

const options = {
    options: { debug: true },
    connection: {
        reconnect: true,
        secure: true
    },
    identity : {
        username,
        password
    },
    channels: [
                '#Pok1',
                '#Pok2',
                '#Pok3',
    ]
};

const client = new tmi.Client(options);
client.connect().catch(console.error);

client.on('connected', () => {
    client.say(channel, ``);
});

client.on('message', (channel, user, message, self) => {
    if(self) return;
                    
    if(user.username == 'asd' && message === "zxc") {
        client.say(channel, 'abc');
    }

    

}); 
Gus
  • 3,534
  • 1
  • 30
  • 39
Cortex
  • 1
  • 1

1 Answers1

0

To say something in a channel you use client.say(channel, message);.

So if you only want to say something in only one channel, you would have to save the channel somewhere:

const TALK_CHANNEL = '#Pok_X';

client.on('message', (channel, user, message, self) => {
    if(self) return;
                    
    if(user.username == 'asd' && message === "zxc") {
        client.say(TALK_CHANNEL, 'abc');
}

Handling the channel swapping would look like this:

const USERNAME = 'asd';
const CHANNELS = ['#pok1', '#pok2', '#pok3', ];

let current_channel = null;
let last_joined_channel = null;

// From docs:
// Username has joined a channel. Not available on large channels and is also sent in batch every 30-60secs.

client.on("join", (channel, username, self) => {
 
    if (username != USERNAME || !CHANNELS.includes(channel))
        return;
       
    last_joined_channel = channel;

    if (current_channel === null)
         current_channel = channel;
});

// user left a channel
client.on("part", (channel, username, self) => {

    if (username != USERNAME || !CHANNELS.includes(channel))
        return;

    current_channel = last_joined_channel;
    last_joined_channel = null;
});


client.on('message', (channel, user, message, self) => {
    if(self)
        return;
                    
    if(user.username == USERNAME && message === "zxc" && current_channel != null) {
        client.say(current_channel, 'abc');
}

pytness
  • 357
  • 2
  • 13