我用 discord.js 创建了一个机器人。我制作了一个会员计数器,以显示语音频道和我的机器人活动的会员人数。 我的问题是计数器只在我重新启动机器人时更新,即使在我的控制台中我也得到了所有日志。
这是我的代码。
const membersOnline = guild.members.cache.filter(member => member.presence.status !== 'offline' && !member.user.bot).size;
const humanMembers = guild.members.cache.filter(member => !member.user.bot).size;
const memberCount = guild.memberCount;
const customerCount = guild.roles.resolve('853015007532023859').members.size;
const channel1 = guild.channels.cache.get('853276389474172958');
const channel2 = guild.channels.cache.get('853298306753888257');
// member counter
setInterval(() =>{
channel1.setName(`Members: ${humanMembers}`);
channel2.setName(`Customers: ${customerCount.toLocaleString()}`);
console.log('Refreshing member count');
// bot presence
if(guild) {
client.user.setPresence({activity: {name: `${humanMembers.toLocaleString()} users | beta 1.3.0` , type: 'WATCHING'}, status: 'online' })};
}, 30000);
```