Search code examples
javascriptdiscorddiscord.js

How can I make my bot change status every 10 seconds? (Online, Idle, DND) Discord.js


I wanted to know if I can set my Discord.js bot to change its online status in an interval, like every 10 seconds or so switch from Online, to Idle, to dnd, and back to online again, and repeat it forever.

This is my current status code:

bot.on("ready", ()=>{
    bot.user.setPresence({activity: {name: 'IN REWORK!' }, status: `idle` })
    .then(console.log)
    .catch(console.error);

There are solutions to change the activity but not the status, can someone please help? - Thanks.


Solution

  • Changing the bot status

    Using the setPresence method to update bot activity, you can include the status parameter to update the bot status.

    bot.user.setPresense(
        activity: {
            name: 'IN WORK'
        },
        status: 'online' // online, idle, invisible, dnd
    )
    

    Changing the status per intervals

    In javascript, you can use the setTimeout function to run the code inside the function after a specific time.

    var onlineStatus = 'online'
    
    function statusLoop() {
        setTimeout(() => {
        if (onlineStatus === 'online') idleStatus();
    
        statusLoop(); // schedule the next status update.
        }, 10000) // Time in ms, 10000 ms = 10s
    }
    
    function onlineStatus() {
        bot.user.setPresense(
            activity: {
                name: 'IN WORK'
            },
            status: 'online' // online, idle, invisible, dnd
        )
        onlineStatus = 'online'
    }
    
    statusLoop();
    

    Simply create the rest of the function, it is just very the same

    References

    Discord.js