本文介绍了用户已连接到语音通道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

我不确定您是否想知道成员是否已连接到VoiceChannel或收听 voiceStateUpdate 事件,因此我将介绍两种情况./p>

检查成员是否已连接到VoiceChannel

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.

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

这篇关于用户已连接到语音通道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 11:38