【问题标题】:How do I make my Discord bot change status every 10 seconds?如何让我的 Discord 机器人每 10 秒更改一次状态?
【发布时间】:2018-12-13 07:45:36
【问题描述】:

我有一个 smol Discord 机器人(使用 discord.js-commando),我有这个代码:

var activevar = ["with the &help command.", "with the developers console", "with some code", "with JavaScript"];
var activities = activevar[Math.floor(Math.random()*activevar.length)];
client.on('ready', () => {
    client.user.setActivity(activities);
}

但这只会在我重新启动机器人时改变。有人可以帮我吗?

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    为 v12 上的用户编辑,现在使用机器人而不是客户端

    const activities = [
      "with the &help command.",
      "with the developers console.",
      "with some code.",
      "with JavaScript."
    ];
    
    bot.on("ready", () => {
      // run every 10 seconds
      setInterval(() => {
        // generate random number between 1 and list length.
        const randomIndex = Math.floor(Math.random() * (activities.length - 1) + 1);
        const newActivity = activities[randomIndex];
    
        bot.user.setActivity(newActivity);
      }, 10000);
    });
    

    【讨论】:

    • 它需要一个 );最后XD
    • client.user.setActivity 这些天不再工作了,你应该用机器人替换客户端;)
    • javascript 数组是基于 0,而不是基于 1,索引应该只是 Math.floor(Math.random() * activities_list.length)
    【解决方案2】:

    我更改了它,以便您可以将状态从播放更改为观看或收听。

    const activities_list = [
        "For Rule Breakers", 
        "The purple names",
        "#general", 
        "The mods do their job"
        ]; // creates an arraylist containing phrases you want your bot to switch through.
    
    client.on('ready', () => {
        setInterval(() => {
            const index = Math.floor(Math.random() * (activities_list.length - 1) + 1); // generates a random number between 1 and the length of the activities array list (in this case 5).
            client.user.setActivity(activities_list[index], { type: 'WATCHING' }); // sets bot's activities to one of the phrases in the arraylist.
        }, 10000); // Runs this every 10 seconds.
    });
    

    【讨论】:

      【解决方案3】:

      考虑到此内容的查看频率,我想我会提供更新且更清晰的回复。

      const state = 0;
      const presences = [
          { type: 'PLAYING',  message: 'a game'  },
          { type: 'WATCHING', message: 'a video' }
      ];
      
      setInterval(() => {
          state = (state + 1) % presences.length;
          const presence = presences[state];
      
          client.user.setActivity(presence.message, { type: presence.type });
      }, 10000);
      

      【讨论】:

        【解决方案4】:

        尚未测试,但理论上应该可以。不是,试着找出问题所在,这是一种很好的做法。否则,请告诉我

        client.on("ready", function() {
            setInterval(function() {
                var actID = Math.floor(Math.random() * Math.floor(activevar.length));
                client.user.setActivity(activities);
            }, 10000)
        });
        

        【讨论】:

        • 在这种情况下声明变量interval没有用。
        • 在这种情况下,没有。但是还是可以用的
        【解决方案5】:

        播放、观看、聆听活动每 10 秒更换一次

        const activities_list = [
            { type: 'PLAYING',  message: 'a game'  },
            { type: 'WATCHING', message: 'a video' },
            { type: 'LISTENING', message: 'a music' }
        ];
        
        client.on('ready', () => {
            setInterval(() => {
                const index = Math.floor(Math.random() * (activities_list.length - 1) + 1);
        
                client.user.setActivity(activities_list[index].message, { type: activities_list[index].type });
            }, 10000);
        });
        

        【讨论】:

          【解决方案6】:

          我正在使用 discord.js v12。

          client.on("ready", () => {
              console.log(`ok`);
          
              const who = ["hi", "hello"];
          
              setInterval(() => {
                  const burh = Math.floor(Math.random() * who.length);    
                  client.user.setPresence({ activity: { name: who[burh]}, status: 'dnd'}); 
              }, 5000);  
          });
          

          【讨论】:

            猜你喜欢
            • 2021-01-13
            • 2021-06-10
            • 2018-02-26
            • 1970-01-01
            • 2020-07-29
            • 2021-08-29
            • 2020-12-21
            • 1970-01-01
            • 2018-12-17
            相关资源
            最近更新 更多