wawa (he/him)
wawa (he/him)
DIAdiscord.js - Imagine an app
Created by wawa (he/him) on 7/26/2023 in #djs-questions
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
9 replies