Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 937
Sapero Mapper Avatar asked Aug 31 '25 17:08

Sapero Mapper


2 Answers

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:

  • https://discord.js.org/#/docs/discord.js/main/class/ClientUser?scrollTo=setActivity
  • https://developer.mozilla.org/en-US/docs/Web/API/setInterval
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
like image 67
Eray Chumak Avatar answered Sep 04 '25 09:09

Eray Chumak


I changed it so you can change the status from playing to watching or listening.

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.
});
like image 39
Outc4st Avatar answered Sep 04 '25 08:09

Outc4st