Search code examples
discorddiscord.js

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


I have a smol Discord bot (with discord.js-commando), I have this code:

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

But that only changes it when I restart the bot. Can someone help me out here?


Solution

  • Program Description & Purpose: When the bot goes online, every ten seconds, randomly select a random activity from the list of activities and update the bot with the new activity:

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

    Material to consider: