简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM