Home > Back-end >  Discord.js bot random presence and status not showing nor changing when the bot starts
Discord.js bot random presence and status not showing nor changing when the bot starts

Time:03-10

I've been trying to make a random bot presence/status change using Discord.js v13 that changes every 15 minutes. The problem I'm facing with my code is that the custom status and presence don't show when I first start the bot, I have to wait 15 minutes for it to show up and start changing.

Here is the code:

client.on("ready", async () => {
    let servers = await client.guilds.cache.size
    let servercount = await client.guilds.cache.reduce((a,b) => a b.memberCount, 0 )
    const statusArray = [
    {
        type: 'WATCHING',
        content: `${servers} servers`,
        status: 'online'
    },
    {
        type: 'PLAYING',
        content: `with my ${servercount} friends`,
        status: 'online'
    }
    ];
    async function pickPresence() {
        const option = Math.floor(Math.random() * statusArray.length);
        try {
            await client.user.setPresence({
                activities: [
                {
                    name: statusArray[option].content,
                    type: statusArray[option].type,
                    url: statusArray[option].url
                },
            ],
            status: statusArray[option].status
            }); 
        } catch (error) {
            console.error(error);
        }
    }
    setInterval(pickPresence, 1000*60*15);
});

Any ideas as to why it doesn't work instantly when I start the bot?

CodePudding user response:

setInterval actually waits for the specified delay (15 minutes) before executing the code in the function for the first time. So all you need to do is simply add pickPresence() on the line before the setInterval.

pickPresence();
setInterval(pickPresence, 1000*60*15);
  • Related