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

210
Visualizações
Intervals not turning on/off on command

I am working on a discord image command that sends a random image every 10 seconds. When I start the command, it works. It starts sending images every 10 seconds, but when I try to make it stop, it just won't.

What went wrong? How can I fix this?

Image.js:

var Scraper = require('images-scraper');
const google = new Scraper({
  puppeteer: {
    headless: true,
  },
});

var myinterval;
module.exports = {
  name: 'image',
  description: 'Sends profile pics)',
  async execute(kaoru, message, args, Discord) {
    //prefix "?"

    const image_query = args.join(' ');
    const image_results = await google.scrape(image_query, 100);

    if (!image_query)
      return message.channel.send('**Please enter name of the image!**');

    if (image_query) {
      message.delete();
      myinterval = setInterval(function () {
        message.channel.send(
          image_results[Math.floor(Math.random() * (100 - 1)) + 1].url,
        );
      }, 10000);
    } else if (message.content === '?stopp') {
      clearInterval(myinterval);
      message.reply('pinging successfully stopped!');
    }
  }
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can't just simply check the message.content inside the execute method. It will never be ?stopp as execute only called when you send the image command (i.e. ?image search term).

What you can do instead is to set up a new message collector in the same channel and check if the incoming message is your stop command. In my example below I used createMessageCollector. I've also added plenty of comments.

const google = new Scraper({
  puppeteer: {
    headless: true,
  },
});

module.exports = {
  name: 'image',
  description: 'Sends profile pics',
  async execute(kaoru, message, args, Discord) {
    const query = args.join(' ');

    if (!query)
      return message.channel.send('**Please enter name of the image!**');

    const results = await google.scrape(query, 100);

    // also send error message if there are no results
    if (!results?.length) return message.channel.send('**No image found!**');

    // you probably want to send the first image right away
    // and not after 10 seconds
    const image = results[Math.floor(Math.random() * results.length)];
    message.channel.send(image.url);

    let intervalID = setInterval(() => {
      // you don't need to add +1 and take away 1, it's unnecessary
      // also, use results.length instead of the hardcoded 100
      // as if there are no 100 results, it will throw an error
      const image = results[Math.floor(Math.random() * results.length)];
      message.channel.send(image.url);
    }, 10000);

    // use this filter to only collect message if...
    const filter = (m) =>
      // the author is the same as the user who executed the command
      m.author.id === message.author.id &&
      // and the message content is ?stopp
      m.content.toLowerCase() === '?stopp';

    // set up a message collector
    const collector = message.channel.createMessageCollector({
      // use the filter above
      filter,
      // you only need to collect a single message
      max: 1,
    });

    // it fires when a message is collected
    collector.on('collect', (collected) => {
      clearInterval(intervalID);
      // message.reply won't work as message is no longer available
      message.channel.send('Pinging successfully stopped!');
    });

    message.delete();
  }
};

enter image description 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