Getting Clients Connected to Voice Channels without Cache

I'm working on a bot that should help us see if people who signed up for an event are present in voice chat and if so assign them their attendance credit. For that I've created a slash command which loops through all voice channels and gets a list of all connected users and to what channels they are connected. The issue is: once the bot is running, if a new client joins voice, it won't be displayed. Furthermore if a client leaves voice chat it's still displayed. To me it seams clear there's a caching issue.
[...]
async execute(client, message, args) {
const peopleInAttendance = [];

message.guild.channels
.filter((c) => {
return c.type === 2;
})
.each((c) => {
c.members.each((m) => {
peopleInAttendance.push({
channel: c.name,
channelId: c.id,
member: m.user.username,
memberId: m.user.id,
});
});
});
console.log(peopleInAttendance);
[...]
[...]
async execute(client, message, args) {
const peopleInAttendance = [];

message.guild.channels
.filter((c) => {
return c.type === 2;
})
.each((c) => {
c.members.each((m) => {
peopleInAttendance.push({
channel: c.name,
channelId: c.id,
member: m.user.username,
memberId: m.user.id,
});
});
});
console.log(peopleInAttendance);
[...]
Question: how can I get an up to date list of members in a channel at command invocation? One thing I've already tried was refetching the guild channels, by using the fetch function of message.guild.channels, however even if I use that
[...]
async execute(client, message, args) {
const peopleInAttendance = [];

const channels = await message.guild.channels.fetch();
channels.filter((c) => {
return c.type === 2;
})
.each((c) => {
c.members.each((m) => {
peopleInAttendance.push({
channel: c.name,
channelId: c.id,
member: m.user.username,
memberId: m.user.id,
});
});
});
console.log(peopleInAttendance);
[...]
[...]
async execute(client, message, args) {
const peopleInAttendance = [];

const channels = await message.guild.channels.fetch();
channels.filter((c) => {
return c.type === 2;
})
.each((c) => {
c.members.each((m) => {
peopleInAttendance.push({
channel: c.name,
channelId: c.id,
member: m.user.username,
memberId: m.user.id,
});
});
});
console.log(peopleInAttendance);
[...]
I still get the stale/outdated list of users. The c.members doesn't have a fetch function. Intents used: ["Guilds", "GuildMembers", "GuildMessages", "MessageContent"] Versions: - Discord.JS: [email protected] - Node: v18.17.0
8 Replies
d.js toolkit
d.js toolkit17mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
duck
duck17mo ago
<GuildChannel>.members is just a getter that creates a collection based on the cached members that have permission to view the channel (or are connected to the channel in the case of voice channels) you'll want to fetch all members in the guild first with <Guild>.members.fetch() this only needs to be done once as the cache will be further updated by events
wawa (he/him)
wawa (he/him)OP17mo ago
To test this, I've added await message.guild.members.fetch(); at the start of the execute function, next I've joined voice with profile A and started the bot. I've invoked the command and as expected profile A shows up. Next I've joined with profile B on a different device, however, when invoking the command, still only A get's reported. After restarting the bot (while both A and B are connected to voice) both of them get reported...
duck
duck17mo ago
Sorry I somehow completely glossed over the word 'voice' You'll want the GuildVoiceStates intent if you want to receive updates relating to members' voice states
wawa (he/him)
wawa (he/him)OP17mo ago
Is there a convenient list of all intents and what they're needed for?
d.js docs
d.js docs17mo ago
- Websocket intents limit events and decrease memory usage: learn more - See what intents you need here
duck
duck17mo ago
second link lists each intent and which event requires that intent
wawa (he/him)
wawa (he/him)OP17mo ago
Awesome, you are a live saver, I've been screaming at the javascript for 2 hours, now it just works... Thanks a lot!
Want results from more Discord servers?
Add your server