javascript - 如何编写一个每 30 分钟运行一次的 Javascript 函数
问题描述
对于我的 discord.js 机器人,我试图让它有一个不断变化的状态消息,但我无法让计时器工作,所以现在这是我的基本状态代码,(此代码使机器人状态显示为“正在观看:你”)
client.on('ready', () =>{
console.log(`Logged in`);
client.user.setPresence({
status: "online",
game: {
name: "You",
type: "WATCHING"
}
});
});
解决方案
let statuses = [
{game: {name: `1`}, type: "WATCHING"},
{game: {name: `2`}, type: "PLAYING"}
{game: {name: `3`}, type: "STREAMING"}
];
let i = 0;
setInterval(() => {
// Get the status
let status = statuses[i];
// If it's undefined, it means we reached the end of the array
if(!status){
// Restart at the first status
status = statuses[0];
i = 0;
}
client.user.setPresence(status);
i++;
}, 5000);
机器人每 5 秒改变一次状态
推荐阅读
- c++11 - 将全局变量成员的指针发送到 WinHttpQueryHeaders (WinHTTP API) 不会改变它的值
- julia - Julia 中的 ccallable 宏是什么?
- iframe - 在 iframe 中显示 startpage.com
- dart - stdin.readLineSync() 无法处理特殊字符
- javascript - JS 函数参数已声明但从不读取
- python - 如何绘制渐近线
- c - 在 C 中打印字符串时如何解决此问题?
- android - 为什么用户详细信息会从 Firestore 中消失?
- bash - 如何使用户成为唯一能够访问文件的用户
- c# - 如果实例表示泛型参数,Type.Namespace 何时返回 null