问题
I'm trying to make my Discord bot update a locked voice channel with the server's member count, but I can't figure out how to make it filter out bots. I've looked online and tried different things but none seem to work for me. Could anyone help?
This is my code:
let myGuild = client.guilds.cache.get("703315963583528991");
let memberCount = myGuild.members.filter(member => !member.user.bot).size;
let memberCountChannel = myGuild.channels.cache.get("704843759078211604");
memberCountChannel.setName('Members: ' + memberCount);
This is the error:
TypeError: myGuild.members.filter is not a function
at Client.<anonymous> (/root/onixo/index.js:21:37)
at Client.emit (events.js:196:13)
at WebSocketManager.triggerClientReady (/root/onixo/node_modules/discord.js/src/client/websocket/WebSocketManager.js:433:17)
at WebSocketManager.checkShardsReady (/root/onixo/node_modules/discord.js/src/client/websocket/WebSocketManager.js:417:10)
at WebSocketShard.<anonymous> (/root/onixo/node_modules/discord.js/src/client/websocket/WebSocketManager.js:199:14)
at WebSocketShard.emit (events.js:196:13)
at WebSocketShard.checkReady (/root/onixo/node_modules/discord.js/src/client/websocket/WebSocketShard.js:467:12)
at WebSocketShard.onPacket (/root/onixo/node_modules/discord.js/src/client/websocket/WebSocketShard.js:439:16)
at WebSocketShard.onMessage (/root/onixo/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
at WebSocket.onMessage (/root/onixo/node_modules/ws/lib/event-target.js:120:16)
Thank you in advance.
回答1:
That's because you need to use cache
to access the members
collection
let myGuild = client.guilds.cache.get("703315963583528991");
let memberCount = myGuild.members.cache.filter(member => !member.user.bot).size;
let memberCountChannel = myGuild.channels.cache.get("704843759078211604");
memberCountChannel.setName('Members: ' + memberCount);
来源:https://stackoverflow.com/questions/61859217/member-count-but-without-bots-discord-js-javascript