Discord bot set presence更新数据



我想分享一下bot所在的公会数量。

let activities = [
"!help",
`on ${client.guilds.cache.size} servers`,
];
setInterval(() => {
//generate random number between 1 and list length.
const randomIndex = Math.floor(Math.random() * activities.length);
const newActivity = activities[randomIndex];
client.user.setActivity(newActivity);
}, 10000);

来源,来自其他stackoverflow问题。但这并不能解决我的问题。

这样你只会得到公会的数量在启动机器人的时刻,但它不会被更新。所以我这样做:

let activities = [
"!help",
`on ${client.guilds.cache.size} servers`,
];
setInterval(() => {
let guildCount = client.guilds.cache.size;
activities = [
"!help",,
`on ${guildCount} servers`,
];
}, 600000);
setInterval(() => {
const randomIndex = Math.floor(Math.random() * activities.length);
const newActivity = activities[randomIndex];
client.user.setActivity(newActivity);
}, 10000);

你知道有什么方法可以做到没有两个设定的间隔。不建议有不必要的间隔吗?

您可以将activities数组放入区间函数中。

setInterval(() => {
let activities = [
"!help",
`on ${client.guilds.cache.size} servers`,
];
//generate random number between 1 and list length.
const randomIndex = Math.floor(Math.random() * activities.length);
const newActivity = activities[randomIndex];
client.user.setActivity(newActivity);
}, 10000);

相关内容

  • 没有找到相关文章