簡體   English   中英

如何以可以停止和重新啟動的方式制作 setInterval 函數?

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

我一直在嘗試為我的 discord.js v12 機器人創建一個與 Disboard 協同工作的函數。 基本上就是一個提醒功能,每兩小時ping某個角色,提醒他們撞服務器。 我一直在嘗試找到一種方法,例如 !d 顛簸,機器人通過 clearInterval 停止 setInterval 函數,然后再次啟動 setInterval 函數。 但是,我一直收到錯誤消息,指出腳本中未定義某個對象。

這是我的代碼:

    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!")
        }



    });

這個怎么樣:

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

這對你來說足夠好嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM