我把它放在状态中是为了显示总人数中有多少成员在线,但它不会更新真实数字,只有当机器人重新启动时,有没有办法在这个活动到达时,信息会与新数据一起出现?
const server = client.guilds.cache.get("ID OF GUILD")
let onlineMembers = 0;
server.members.cache.forEach((m) => {
if (m.presence.status === "online" || m.presence.status === "dnd" || m.presence.status === "idle") {
onlineMembers++;
}
});
let activities = [
`${onlineMembers}/${server.members.cache.filter(member => !member.user.bot).size} members online`,
]
i = 0;
setInterval( () => client.user.setActivity(`${activities[i++ % activities.length]}`, {
type: "PLAYING"
}), 10000 * 60);
每次运行都需要重新生成数组中的字符串:
let i = 0;
setInterval( () => {
const server = client.guilds.cache.get("ID OF GUILD")
let onlineMembers = 0;
server.members.cache.forEach((m) => {
if (m.presence.status === "online" || m.presence.status === "dnd" || m.presence.status === "idle") {
onlineMembers++;
}
});
let activities = [
`${onlineMembers}/${server.members.cache.filter(member => !member.user.bot).size} members online`,
];
client.user.setActivity(`${activities[i++ % activities.length]}`, {
type: "PLAYING"
});
}, 10000 * 60);