tmijs/tmi.js

Bot disconnecting for receiving too many commands simultaneously

dibo999 opened this issue · 3 comments

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

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

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

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