tmijs / tmi.js

💬 Javascript library for the Twitch Messaging Interface. (Twitch.tv)
https://tmijs.com
MIT License
1.55k stars 214 forks source link

Is there a way to use join() and part() with arrays from getChannels() inside of the parenthesis of join and part? #496

Closed WarCatStudios closed 2 years ago

WarCatStudios commented 2 years ago

Situation I am creating a function that uses client.getChannels() to first get an array of every channel my Twitch bot is joined to, then to second, use part() to leave all those channels in the array. Finally the bot is supposed to use join() to rejoin a few at the end of the function.

Function

function leaveAllChannels(userstate, message, channel){

    var currentChannels = (client.getChannels());
    console.log(currentChannels); 
    client.part(currentChannels);

    client.part(currentChannels)
    .then((data) => {
        // data returns [channel]
    }).catch((err) => {
        return;
    });

    client.join(ADMIN_CHANNELS)
    .then((data) => {
        // data returns [channel, username]
    }).catch((err) => {
        return;
    });
}

Things to note about the function

Actual behaviour: Every time the function is run I get the error message seen below. I've noticed that this is triggered when I place an array or a variable containing an array inside of the parenthesis of join() and part()

Expected behaviour: The function should leave every single channel that the bot is currently joined to and then rejoin from an array of a few selective channels,

Error log:

C:\Users\anon\Desktop\JustSomeCookiesTwitch\node_modules\tmi.js\lib\utils.js:49
                const channel = (str ? str : '').toLowerCase();
                                                 ^

TypeError: (intermediate value)(intermediate value)(intermediate value).toLowerCase is not a function
    at Object.channel (C:\Users\anon\Desktop\JustSomeCookiesTwitch\node_modules\tmi.js\lib\utils.js:49:36)
    at client.part (C:\Users\anon\Desktop\JustSomeCookiesTwitch\node_modules\tmi.js\lib\commands.js:32:14)
    at Timeout.leaveFreeChannels [as _onTimeout] (C:\Users\anon\Desktop\JustSomeCookiesTwitch\src\app.js:238:9)
    at listOnTimeout (node:internal/timers:559:11)
    at processTimers (node:internal/timers:500:7)

Server configuration

WarCatStudios commented 2 years ago

I figured it out, it's not a tmi.js thing its a javaScript documentation thing. For those of you are are curious it is the forEach() function.

Here is my code now

function startChannelLeave(userstate, message, channel){
    var currentChannels = (client.getChannels());
    console.log(currentChannels); 

    currentChannels.forEach(function (e) {
        console.log(e);
        leaveFreeChannels(userstate, message, channel, e);
    });

    CHANNEL_NAMES.forEach(function (e) {
        rejoinPremiumChannels(userstate, message, channel, e);
    });

}

function leaveFreeChannels(userstate, message, channel, e){

    client.part(e)
    .then((data) => {
        // data returns [channel]
    }).catch((err) => {
        return;
    });

}

function rejoinPremiumChannels(userstate, message, channel, e){
    client.join(e)
    .then((data) => {
        // data returns [channel]
    }).catch((err) => {
        return;
    });
}