Bot connets, read messages, but cant send messages


const tmi = require('tmi.js');

var client;

var controller;

var channel;

function init(controllerInstance) {

  controller = controllerInstance;

}

function connect(username,channelTMP,token) {

  opts = {

    identity: {

      username: username,

      password: token

    },

    channels: [

      channelTMP

    ],

    options: {

      debug: true,

    },

    connection: {

      cluster: 'aws',

      secure: true,

      reconnect: true,

    },

  };

  channel = channelTMP;

  client = new tmi.client(opts);

  client.on('message', onMessageHandler);

  client.on('connected', onConnectedHandler);

  return client.connect();

}

// Called every time a message comes in

function onMessageHandler (nick, context, message, self) {

  //if (self) { return; } // Ignore messages from the bot

  // Remove whitespace from chat message

  const msg = message.trim();

  console.log("from chat:",nick,message);

  controller.playOBFromTwitch(nick,message);

}

function setGame(game,title) {

  return new Promise((resolve, reject) => {

    let chatMSG = "!game " + game;

   

    if (!client.userstate.hasOwnProperty(channel)) reject({success:false,message:"not connected"});

    else {

      client.say(channel,chatMSG)

      client.say(channel,chatMSG).then(() => {

        console.log(result);

       

        chatMSG = "!title " + title;  

        client.say(channel,chatMSG).then(() => {

          let result = { success:true, message:"Game & title Changed" };

          console.log(result);

          resolve(result);

        })

        .catch(err => {

            console.log(err);

            reject(err);

        });

      })

      .catch(err => {

          console.log(err);

          reject(err);

      });

    }

  });

}

// Called every time the bot connects to Twitch chat

function onConnectedHandler (addr, port) {

  console.log("Twitch Connected");

}

module.exports = {

  init:init,

  connect:connect,

  setGame:setGame

};

The message i am recieving is “cannot send anonymous messages”

client.userstate.hasOwnProperty(channel) is never true…

I am using the oauth token… any ideas?

This suggests you didn’t specify a username and valid oAuth token to login to chat with.
And/or you used a Client Credentials token that doesn’t represent a user.

Therefore you are not logged in as a user whom can send messages.

You need to generate and user a user token with the needed chat scopes.

If you call validate

And the response does not have a user_id then your token is of the wrong type.

I am using:

https://twitchapps.com/tmi/

This link doesnt provide a valid oauth?

I don’t use third party generators myself. But I believe so.

Test the token you got back using the validate endpoint

But that is what this error means:

This error means you didn’t login to chat as a user.

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