如何让我的 Discord 机器人每 10 秒更改一次状态?

How do I make my Discord bot change status every 10 seconds?

我有一个 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);
}

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

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

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

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

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

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

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

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

播放、观看、收听活动每 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);
});

我正在使用 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);  
});

这也适用于 v13。

const statuses = [
  { name: "to nothing", type: "LISTENING" },
  { name: "something", type: "PLAYING" },
  { name: "your commands", type: "WATCHING" },
];
client.on("ready", () => {
  setInterval(() => {
    var randomStatus = statuses[Math.floor(Math.random() * statuses.length)];
    client.user.setActivity(randomStatus);
  }, 10000);
});