How can I get the voice states of members?

How can I get the voice states of members? For some reason do I only get the voice states of the members who joined a channel after the bot started. Is there a way around it?
6 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
SturmEnte
SturmEnte2y ago
npm list discord.js discord.js@14.6.0 node -v v17.4.0 Code:
import { CommandInteraction } from "discord.js";
import { joinVoiceChannel } from "@discordjs/voice";

module.exports.init = () => {};

module.exports.execute = async (interaction: CommandInteraction) => {
const member = await interaction.guild?.members.fetch(interaction.user.id);

if (!interaction.guild) {
interaction.reply("The music commands only work in guilds");
return;
}

if (!member?.voice.channelId) {
interaction.reply("You need to be in a channel to use this command");
console.log(member?.voice);
return;
}

joinVoiceChannel({
guildId: String(interaction.guild.id),
channelId: String(member.voice.channelId),
adapterCreator: interaction.guild.voiceAdapterCreator,
});

interaction.reply("Joined voice channel");
};

module.exports.command = {
name: "join",
description: "Joins your current voice channel",
};
import { CommandInteraction } from "discord.js";
import { joinVoiceChannel } from "@discordjs/voice";

module.exports.init = () => {};

module.exports.execute = async (interaction: CommandInteraction) => {
const member = await interaction.guild?.members.fetch(interaction.user.id);

if (!interaction.guild) {
interaction.reply("The music commands only work in guilds");
return;
}

if (!member?.voice.channelId) {
interaction.reply("You need to be in a channel to use this command");
console.log(member?.voice);
return;
}

joinVoiceChannel({
guildId: String(interaction.guild.id),
channelId: String(member.voice.channelId),
adapterCreator: interaction.guild.voiceAdapterCreator,
});

interaction.reply("Joined voice channel");
};

module.exports.command = {
name: "join",
description: "Joins your current voice channel",
};
Output when I execute the command before rejoining a channel
VoiceState {
guild: <ref *1> Guild {...},
id: '420225163423121418',
serverDeaf: null,
serverMute: null,
selfDeaf: null,
selfMute: null,
selfVideo: null,
sessionId: null,
streaming: null,
channelId: null,
suppress: null,
requestToSpeakTimestamp: null
}
VoiceState {
guild: <ref *1> Guild {...},
id: '420225163423121418',
serverDeaf: null,
serverMute: null,
selfDeaf: null,
selfMute: null,
selfVideo: null,
sessionId: null,
streaming: null,
channelId: null,
suppress: null,
requestToSpeakTimestamp: null
}
Squid
Squid2y ago
Do you have the GuildVoices GuildVoiceStates intent?
SturmEnte
SturmEnte2y ago
It's called GuildVoiceStates and yes
Squid
Squid2y ago
Ah right, my bad GulidMemberManager#fetch() checks the cache first, so you're defining member no differently than if you just accessed interaction.member If you want to directly query the API to hopefully get the latest fetch results and get the current voice state, you have to use the force option in the fetch method
SturmEnte
SturmEnte2y ago
It still doesnt work