问题
I want to know is it possible to know if any member is connected to a specific voice channel in discord.js v12.2.0. I've been sticking in this question in recent days. Please tell me if you have any clues on it.
回答1:
I am not sure if you want to know if the member is connected to a VoiceChannel or listen to the voiceStateUpdate event so I'll cover both cases.
Checking if the member is connected to a VoiceChannel
const Guild = client.guilds.cache.get("GuildID"); // Getting the guild.
const Member = Guild.members.cache.get("UserID"); // Getting the member.
if (Member.voice.channel) { // Checking if the member is connected to a VoiceChannel.
// The member is connected to a voice channel.
// https://discord.js.org/#/docs/main/stable/class/VoiceState
console.log(`${Member.user.tag} is connected to ${Member.voice.channel.name}!`);
} else {
// The member is not connected to a voice channel.
console.log(`${Member.user.tag} is not connected.`);
};
Listening to the voiceStateUpdate event
client.on("voiceStateUpdate", (oldVoiceState, newVoiceState) => { // Listeing to the voiceStateUpdate event
if (newVoiceState.channel) { // The member connected to a channel.
console.log(`${newVoiceState.member.user.tag} connected to ${newVoiceState.channel.name}.`);
} else if (oldVoiceState.channel) { // The member disconnected from a channel.
console.log(`${oldVoiceState.member.user.tag} disconnected from ${oldVoiceState.channel.name}.`)
};
});
来源:https://stackoverflow.com/questions/62637725/user-connected-to-voice-channel