Jorge Montejo
Jorge Montejo

Reputation: 607

Discord bot set presence updating the data

I wanna share on the presence status the number of guilds the bot is in.

let activities = [
    "!help",
    `on ${client.guilds.cache.size} servers`,
];

setInterval(() => {
    //generate random number between 1 and list length.
    const randomIndex = Math.floor(Math.random() * activities.length);
    const newActivity = activities[randomIndex];
    client.user.setActivity(newActivity);
}, 10000);

Source, from other stakoverflow question. But this does not solve my problem.

This way you will only get the number of guilds at the moment of starting the bot, but it will not be updated. So I did this:

    let activities = [
    "!help",
    `on ${client.guilds.cache.size} servers`,
    ];

setInterval(() => {
    let guildCount = client.guilds.cache.size;

    activities = [
        "!help",,
        `on ${guildCount} servers`,
    ];
}, 600000);

setInterval(() => {
    const randomIndex = Math.floor(Math.random() * activities.length);
    const newActivity = activities[randomIndex];
    client.user.setActivity(newActivity);
}, 10000);

Do you know any way to do it without having 2 set intervals. Is it not recommended to have unnecessary intervals?

Upvotes: 0

Views: 279

Answers (1)

Lofn
Lofn

Reputation: 940

You could put the activities array inside your interval function.

setInterval(() => {
    let activities = [
    "!help",
    `on ${client.guilds.cache.size} servers`,
     ];
    //generate random number between 1 and list length.
    const randomIndex = Math.floor(Math.random() * activities.length);
    const newActivity = activities[randomIndex];
    client.user.setActivity(newActivity);
}, 10000);

Upvotes: 1

Related Questions