【问题标题】:How do I make an ordered rotating status for a discord bot?如何为不和谐机器人设置有序旋转状态?
【发布时间】:2021-05-07 17:33:50
【问题描述】:

我想让我的 Discord 机器人按顺序更改状态。例如,我希望机器人的状态为“T”,然后是“Te”,然后是“Tes”,然后是“Test”,然后重复。

我已经按照我想要的方式排序了一个数组。这是我当前的代码:

const index = activities_list.sort()
var activitySet = activities_list[index]
client.user.setActivity(activitySet)

我正在使用 Node v12 和 discord.js v12。

【问题讨论】:

    标签: javascript node.js arrays discord discord.js


    【解决方案1】:

    您可以跟踪当前索引并每隔x 秒更新一次。您可以为此使用setInterval。每x 秒更新一次状态并检查这是否是最后一个索引。如果是,则将其重置为0,如果不是,则增加索引:

    const activities = ['t', 'te', 'tes', 'test'];
    
    client.on('ready', () => {
      const updateDelay = 5; // in seconds
      let currentIndex = 0;
    
      setInterval(() => {
        const activity = activities[currentIndex];
        client.user.setActivity(activity);
    
        // update currentIndex
        // if it's the last one, get back to 0
        currentIndex = currentIndex >= activities.length - 1 
          ? 0
          : currentIndex + 1;
      }, updateDelay * 1000);
    });
    

    【讨论】:

      猜你喜欢
      • 2021-05-27
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 2020-03-19
      • 1970-01-01
      • 2017-05-10
      • 2018-08-23
      相关资源
      最近更新 更多