间隔不打开/关闭命令

发布于 2025-02-05 03:21:59 字数 1049 浏览 2 评论 0原文

我正在处理一个Discord Image命令,该命令每10秒发送一次随机图像。当我启动命令时,它可以正常工作。它开始每10秒发送一次图像,但是当我尝试停止它时,它不会。

怎么了?我该如何解决?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风夜微凉 2025-02-12 03:21:59

您不能仅仅在execute方法中检查message.content。它永远不会是?stopp作为execute仅在发送image命令(即?image搜索术语)时才会调用。 )。

您可以做的是在同一频道中设置新消息收集器,并检查传入消息是否是您的stop命令。在下面的示例中,我使用代码> 。我还添加了很多评论。

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();
  }
};

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文