[英]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. - 谢谢。
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
)
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,就完全一样了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.