User connected to voice channel?

本小妞迷上赌 提交于 2021-02-08 11:19:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!