tmijs / tmi.js

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

Bot disconnecting for receiving too many commands simultaneously #455

Closed ghost closed 3 years ago

ghost commented 3 years ago

The bot is disconnecting from the server because its receiving many commands at the same time, what can I do?

AlcaDesign commented 3 years ago

If you mean it's receiving and replying to chat command (like "!command"), then you need to create a rate limiter.

Also ensure you're not replying to self messages.

client.on('message', (channel, tags, message, self) => {
    if(self) return;
    // Write code here.
});
ghost commented 3 years ago

i understand almost nothing about coding, my code is this

console.log('FERRAMENTAS ESTÃO OK!');
const tmi = require('tmi.js');
const NOME_DO_BOT = 'x';
const O_TOKEN_DO_PASSO_4 = 'x';

// Definir opções de configuração
const opts = {
  identity: {
    username: NOME_DO_BOT,
    password: O_TOKEN_DO_PASSO_4
  },
  channels: ['x']
};s
const client = new tmi.client(opts);
//intercepta mensagem do chat
function mensagemChegou(alvo, contexto, mensagem, ehBot) {
  if (ehBot) {
    return;
  } 
  const nomeDoComando = mensagem.trim();
  if (nomeDoComando === '!quero') {
    console.log(`* Foi Executado o comando ${nomeDoComando}`);
    var delay=5000; 
setTimeout(function(){
    client.say(alvo, `!quero`);
},delay);
  } else {
    console.log(`* Não conheço o comando ${nomeDoComando}`);
  }
}

function entrouNoChatDaTwitch(endereco, porta) {
  console.log(`* Bot entrou no endereço ${endereco}:${porta}`);
}

// Registra nossas funções
client.on('message', mensagemChegou);
client.on('connected', entrouNoChatDaTwitch);

client.connect();

When people start flooding the command "!quero" this happens https://imgur.com/a/h7VbwLZ

AlcaDesign commented 3 years ago

What version of tmi.js are you using? The current version is 1.7.1 so you may wish to update.

Ensure you have a catcher for the client.connect call and an error event listener.

client.on('error', console.error);
client.connect().catch(console.error);

If you are actually being disconnected, in the configuration options you can set connection.reconnect to true to make it reconnect on disconnect.

const client = new tmi.Client({
    connection: {
        reconnect: true
    }
});

You could do this in your code to ensure it won't call client.say as many times as it's used within that delay time. This is called debouncing.

let queroCommandTimeout;

function mensagemChegou(alvo, contexto, mensagem, ehBot) {
    /* ... */
    if (nomeDoComando === '!quero') {
        let delay = 5000;
        clearTimeout(queroCommandTimeout);
        queroCommandTimeout = setTimeout(() => client.say(alvo, '!quero'), delay);
    }
    /* ... */
}

A more complex system could be devised to ensure