简体   繁体   English

如何以可以停止和重新启动的方式制作 setInterval 函数?

[英]How to make a setInterval function in such a way that it can stop and restart?

I've been trying to make a function for my discord.js v12 bot that works in tandem with Disboard.我一直在尝试为我的 discord.js v12 机器人创建一个与 Disboard 协同工作的函数。 Basically, it's a reminder function that pings a certain role every two hours to remind them to bump the server.基本上就是一个提醒功能,每两小时ping某个角色,提醒他们撞服务器。 I've been trying to work out a way so on !d bump, the bot stops the setInterval function through clearInterval, and starts the setInterval function again.我一直在尝试找到一种方法,例如 !d 颠簸,机器人通过 clearInterval 停止 setInterval 函数,然后再次启动 setInterval 函数。 However, I've been receiving an error that says that a certain object is not defined in the script.但是,我一直收到错误消息,指出脚本中未定义某个对象。

Here's my code:这是我的代码:

    client.on('message', async message => {
    if (message.content === (`${prefix}setReminder`)) {
        message.channel.send("Interval has been set! Members will be reminded to bump the server every two hours!")
        var role = message.guild.roles.cache.find(role => role.name === "bumping ping")
        let intervalOne = setInterval(() => {
            message.channel.send(`${role} chop-chop! It's time to bump! 😃`)
        }, 7200000);
        
        
    
    }
       
});
    client.on('message', async message => {
        if (message.content === "!d bump") {
            message.channel.send("Thank you for bumping the server. Please check back two hours later to bump the server again.")
            clearInterval(intervalOne)
             .then(setInterval(intervalOne)) | message.channel.send("I have successfully restarted the timer. You will get notified in two hours' time!")
        }



    });

How about this:这个怎么样:

module.exports = (client, message) => {
  if(message.content === '!d bump') {
    const pingRole = message.guild.roles.cache.find(role => role.name === 'bumping ping');
    message.channel.send('Thank you for bumping the server. Please check back two hours later to bump the server again.')
    setTimeout(() => message.channel.send(`${pingRole} chop-chop! It's time to bump! 😃`), 7200000);
  }
};

Is that good enough for you?这对你来说足够好吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM