简体   繁体   English

如何让我的机器人每 10 秒更改一次状态? (在线,空闲,免打扰)Discord.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.我想知道我是否可以将我的 Discord.js 机器人设置为每隔一段时间更改其在线状态,例如每 10 秒左右从在线切换到空闲、免打扰,然后再次返回在线,并永远重复。

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.使用setPresence方法更新机器人活动,您可以包含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.在javascript中,可以使用setTimeout function在特定时间后运行function里面的代码。

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只需创建 function 的 rest,就完全一样了

References参考

Discord.js Discord.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 discord.js 中闲置 15 秒后,如何让我的机器人与语音断开连接 - how can I make my bot disconnect from voice after 15 seconds being idle in discord.js Discord.js:我怎样才能让我的机器人打字几秒钟? - Discord.js: How can i make my bot typing for some seconds? Discord.js机器人程序需要帮助-如何使我的机器人每6小时自动执行一项任务而不发送命令? - Discord.js bot help needed - how can I make my bot auto do a task every 6 hours without sending it a command? 如何使我的 discord 机器人具有自定义状态 (discord.js) - How do I make my discord bot have a custom status (discord.js) 有什么方法可以在 discord.js v13 上将我的 Discord Bot 的在线状态设置为移动设备? - Is there any way I can set my Discord Bot's online status to mobile on discord.js v13? 如何更改 Discord.js 机器人的状态? - How to change the status of a Discord.js bot? 我如何在 discord.js 中每 10 秒编辑一次嵌入 - How can i edit an embed every 10 seconds in discord.js 如何让我的机器人每 10 秒更改一次状态? - How do i make so that my bot changes status every 10 seconds? Discord.js 如何为我的机器人所在的每个公会创建邀请? - Discord.js how can I create an invite for every guild my bot is in? 如何在网站上制作 discord.js 机器人统计信息? - How I can make discord.js bot statistics on website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM