Is there any recursion here? Can i make a better performance code? Bot Telegram made in Node.Js

59 Views Asked by At

I realized that after a few days of running this code it starts to generate duplicate console.log, would that be some recursive action or something in the code that I wrote in an ineffective way that causes this situation?

Here is my code, to make a loop I used the setTimeout function, but I don't know if this is the best solution for long term performance mainly being a 24/7 online bot on a weak home computer that I will be using as a server.

const momentTimeZone = require('moment-timezone');

function timezoneRecifeFormated() {
  return momentTimeZone.tz("America/Recife").format('HH:mm');
}

function timezoneRecifeHour() {
  return momentTimeZone.tz("America/Recife").hour();
}

const minimumRangeMinute = 3;
const maxRangeMinute = 6;

function foo_interval_action() {
  if (pauseBot == false) {
    let RedOrGreen = Math.random();

    let onlinePeriod = timezoneRecifeHour() > 7 || timezoneRecifeHour() < 3;
    let offlinePeriod = timezoneRecifeHour() >= 3 && timezoneRecifeHour() <= 7

    console.log('red or green math random = ' + RedOrGreen);
    console.log('ONLINE period = ' + onlinePeriod);
    console.log('OFFLINE period = ' + offlinePeriod);

    if (RedOrGreen < 0.7 && onlinePeriod) {
      setTimeout(foo_interval_action, getRandomInterval(minimumRangeMinute, maxRangeMinute));
      console.log('GREEN aposte agora!' + timezoneRecifeFormated());
    } else if (RedOrGreen >= 0.7 && onlinePeriod) {
      setTimeout(foo_interval_action, getRandomInterval(minimumRangeMinute, maxRangeMinute));
      console.log(('RED CUIDADO!') + timezoneRecifeFormated());
    } else {
      setTimeout(foo_interval_action, getRandomInterval(minimumRangeMinute, maxRangeMinute));
      console.log('Bot offline, fora do periodo de trabalho. Agora são ' + timezoneRecifeFormated());
    }
  }
}

setTimeout(foo_interval_action, getRandomInterval(minimumRangeMinute, maxRangeMinute));
next();
});

function getRandomInterval(min, max) {
  let randomInterval = Math.floor(Math.random() * (max - min) + min); // Not include the max value
  console.log('randomInterval = Daqui a ' + randomInterval + ' minutos.timeZone Recife agora = ' + timezoneRecifeFormated());
  return randomInterval * 60000;
};
0

There are 0 best solutions below