Search code examples
javascriptnode.jsobjecttwitch

node.js tmi.js twitch bot Object Returning [object, Object]


I'm making a game for a twitch bot using node.js and tmi.js. I have an array which is dynamic(votePlayers), that changes each round depending on incoming messages on the twitch chat that are read by the bot. I then must have a counter for each time an item appears in this array, so I used the below approach to do so, which worked, and when I console log the object the data is saved to, all appears fine, but when I try to return it as a message from the bot to the twitch chat, it returns [object, Object], despite being printed correctly to the console. I have no idea what's going wrong and what I can do.

//calculate function 
function calculateWinner () {
    let roundCount = {};
    votePlayers.forEach(function(i) { roundCount[i] = (roundCount[i]||0) + 1;});
    console.log(roundCount);
    client.say(channel, `The votes for this round are: ${roundCount}`);
    newRound();
}


Solution

  • client.say expects a string as second argument, yet you're passing an object, that's why you're getting [object Object].

    First convert your object to string: i.e.

    roundCount = {};
    roundCount[1] = 5;
    roundCount["2"] = 3;
    
    console.log(Object.keys(roundCount).map(key => `${key}: ${roundCount[key]}`).join(", "));
    

    then calling

    client.say(channel, `The votes for this round are: ${Object.keys(roundCount).map(key => `${key}: ${roundCount[key]}`).join(", ")}`);
    

    should work