【问题标题】:How to set bot's status at different times of the day?如何在一天中的不同时间设置机器人的状态?
【发布时间】:2019-01-05 19:31:05
【问题描述】:

我有一个不和谐的机器人,我正在尝试每小时更改一次状态以带来一些多样性。但是我对javascript日期不太熟悉,有什么帮助吗? 这是我的代码:

var quotes = ["to my fire mix | >commands", "to youtube videos | >commands"]
if(it is this time of day){
var rand = quotes[Math.floor(Math.random() * quotes.length)];
client.user.setActivity(rand, {type: 'LISTENING'})
}

【问题讨论】:

    标签: javascript node.js npm discord.js


    【解决方案1】:

    更新: 我最终选择了增量计时器。

    function refreshStatus(){
      var quotes = ["my fire mix | >commands", "youtube videos | >commands", "the loneley sound of nothing | >commands", "audiomemes memes | >commands", "danno | >commands"]
    x = 5
    x=x*60
    rand = quotes[Math.floor(quotes.length * Math.random())]
    if(rand){
    client.user.setActivity(rand, {type: 'LISTENING'});
    }
    setTimeout(refreshStatus, x*1000)
    }
    

    【讨论】:

      【解决方案2】:

      您可以只检查当前时间是奇数还是偶数:

      client.user.setActivity(
        quotes[(new Date).getHours() % 2], 
        {type: 'LISTENING'}
      )
      

      或者如果你只是想每小时随机改变,使用 setInterval:

      setInterval(() => {
        client.user.setActivity(
           quotes[Math.floor(quotes.length * Math.random())], 
            {type: 'LISTENING'}
        );
      }, 1000 * 60 * 60);
      

      【讨论】:

      • 你给的代码不行,我也把Math floor改成了Math.floor
      • @danGamingTV“没用”很宽泛,你把这段代码放在哪里了?
      猜你喜欢
      • 2018-08-23
      • 2021-05-27
      • 1970-01-01
      • 2021-07-30
      • 2019-09-07
      • 2020-02-10
      • 2017-12-26
      • 1970-01-01
      • 2021-05-07
      相关资源
      最近更新 更多