Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
setInterval Not Repeating Function using Discord.js

I'm trying to make a discord bot that gives you weekly reminders. Im using momentjs to get the time. As well as using discord.js-commando. I found the best way to call a function multiple times is to use setInterval.

const moment = require('moment');
const Commando = require('discord.js-commando');
const bot = new Commando.Client();

bot.on('ready', function () {
  var day = moment().format('dddd');
  var time = moment().format('h:mm a');

  function Tuesday() {
    if (day == 'Tuesday' && time == '6:30 pm') {
      const channel = bot.channels.get('933047404645724234');
      console.log('Sending Message');
      channel.send('Reminder: You have a week to complete your To-Do List!');
    } else {
      console.log('Not Sending Message');
    }
  }

  console.log('Bot is now ready!');
  setInterval(Tuesday, 100);
});

I noticed the problem with the setInterval is that it is only called once. I also tried using an async function but that failed as well.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Well executing a function every 100ms is not quite optimal. I don't know what is the reason to run just once (it should run infinitely) but there is a better way to do what you need.

You will need the package called "node-schedule". It's really useful for such things.

Here's an example:

const schedule = require("node-schedule");

schedule.scheduleJob({hour: 18, minute: 30, dayOfWeek: 2}, function(){
    // your code here
});

You can read more in the documentation of node-schedule here.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda