Twitch bot 多个频道但仅在 1 发送消息

Twitch bot multiple channels but message only at 1

所以我的 bot 连接到 3 个频道,如果所有 3 个频道都在线,如果 bot 要离线,怎么能只在第一个频道上工作,所以切换到下一个频道

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');
    }

    

}); 

要在您使用的频道中说点什么 client.say(channel, message);

因此,如果您只想在一个频道中说些什么,则必须将频道保存在某个地方:

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');
}

处理频道交换将如下所示:

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');
}