用户已连接到语音通道?

时间:2020-06-29 12:04:54

标签: javascript discord discord.js

我想知道是否可以知道discord.js v12.2.0中是否有任何成员连接到特定的语音通道。最近几天,我一直在坚持这个问题。请告诉我您是否有任何线索。

1 个答案:

答案 0 :(得分:2)

我不确定您是否想知道成员 是否已连接到VoiceChannel或收听voiceStateUpdate事件,所以我将介绍两种情况。

检查成员是否已连接到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.`);
};

收听voiceStateUpdate事件

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}.`)
    };
});