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

151
Visualizações
Discord.js interval message

Is there any way to stop an interval based on a specified message content? Or make the users stop the interval first before starting a new interval?

Here is the code for starting an interval:

const ms = require('ms')

let interval;
function isNumeric(str) {
    return !isNaN(str) && !isNaN(parseFloat(str));
}
module.exports = {
    name: 'interval',
    aliases: ['int'],
    run: async (client, message, args) => {
        let time = args[0];
        if(!time) return message.reply('Please enter the duration of message interval!').then(msg => {
    setTimeout(() => {
  msg.delete()
}, 5000)    
  })
  .catch()

  let reason = args.slice(1).join(' ');
  if(!reason) return message.reply('Please enter a message!').then(msg => {
    setTimeout(() => {
  msg.delete()
}, 5000)
  })
  .catch()

        interval = setInterval(function() {
            message.channel.send(reason)
            .catch(console.error);
        }, ms(time));
    },
    stopInterval() {
        if(interval) {
            clearInterval(interval);
        }
    }
}

The specified message content would be the

let reason = args.slice(1).join(' ');

As for the code for stopping the interval:

const stopInt = require('./interval');

module.exports = {
    name: 'stopinterval',
    aliases: 'stopint',
    run: async (client, message, args) => {
        message.channel.send("Message reminder has been stopped.");
        stopInt.stopInterval();
    }
}

I also have a problem which if I use the interval twice, I can only stop the last interval I created and couldn't stop the first one.

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

0

You could use a Set or an array to keep track of a list of intervals:

const ms = require("ms");

const intervals = new Set()
function isNumeric(str) {
  return !isNaN(str) && !isNaN(parseFloat(str));
}

module.exports = {
  name: "interval",
  aliases: ["int"],
  run: async (client, message, args) => {
    let time = args[0];
    if (!time)
      return message
        .reply("Please enter the duration of message interval!")
        .then((msg) => {
          setTimeout(() => {
            msg.delete();
          }, 5000);
        })
        .catch();

    let reason = args.slice(1).join(" ");
    if (!reason)
      return message
        .reply("Please enter a message!")
        .then((msg) => {
          setTimeout(() => {
            msg.delete();
          }, 5000);
        })
        .catch();

    intervals.add(setInterval(function () {
      message.channel.send(reason).catch(console.error);
    }, ms(time)));
  },
  stopInterval() {
    for (const interval of intervals) {
      intervals.delete(interval);
      clearInterval(interval);
    }
  },
};

(I toke the liberty to format your code using prettier)

Unrelated to your question but I noted:

  • You mixed async / await with .then / .catch
  • Empty .catch() calls should be removed
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