I’m working on a Discord bot using discord.js-commando and I want it to switch its status message every 10 seconds. Right now I’ve got this code:
const statusOptions = ['Helping users', 'Debugging', 'Coding', 'Learning JS'];
const randomStatus = statusOptions[Math.floor(Math.random() * statusOptions.length)];
client.once('ready', () => {
client.user.setActivity(randomStatus);
});
This works, but it only changes the status when I restart the bot. How can I make it update automatically every 10 seconds? I’m pretty new to this so any help would be great. Thanks!
To achieve automatic status updates, you can leverage the setInterval function. Here’s a refined approach:
client.once('ready', () => {
const updateStatus = () => {
const randomStatus = statusOptions[Math.floor(Math.random() * statusOptions.length)];
client.user.setActivity(randomStatus);
};
updateStatus(); // Set initial status
setInterval(updateStatus, 10000); // Update every 10 seconds
});
This method ensures your bot’s status updates regularly without manual intervention. It’s worth noting that excessive status changes might strain Discord’s API. Consider implementing a cooldown mechanism or increasing the interval if you encounter rate limiting issues.
I encountered a similar issue with my own bot. What worked for me was wrapping your status update logic in a setInterval function so that it gets executed every 10 seconds. Here’s how I restructured it:
client.once(‘ready’, () => {
setInterval(() => {
const randomStatus = statusOptions[Math.floor(Math.random() * statusOptions.length)];
client.user.setActivity(randomStatus);
}, 10000);
});
Keep in mind that updating your bot’s status too frequently might raise some flags with Discord, so if you run into any issues, consider increasing the interval slightly. Hope this helps!
yo, i got u covered. try this:
setInterval(() => {
client.user.setActivity(statusOptions[Math.floor(Math.random() * statusOptions.length)]);
}, 10000);
put that in ur ready event. shud work like a charm. just make sure u dont spam it too much or discord might get mad lol