Home > database >  Intervals not turning on/off on command
Intervals not turning on/off on command

Time:06-06

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!');
    }
  }
};

CodePudding user response:

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

  • Related