[英]Discord.js | How to get members currently connected to a Voice Channel?
我正在尝试获取当前连接到特定语音频道的成员。 有了一些属性,我可以得到它们,但我发现的问题是列出的成员不是当前连接的成员,而是机器人开始运行时已连接到语音通道的成员。
这是我的代码(它被缩短但被复制):
const {Client, Intents} = require('discord.js')
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_PRESENCES, Intents.FLAGS.GUILD_MEMBERS] })
client.on('messageCreate', messagesHandler)
function messagesHandler (msg) {
if (msg.author.bot) return false;
console.log(`Msg received: ${msg.content}`)
if (msg.content.startsWith('/')) {
const msgArr = msg.content.split(' ')
switch (msgArr[0]) {
case '/list':
listCommand(msg, msgArr)
break
}
}
}
async function listCommand (msg, msgArr) {
if(!msgArr[1]) {
const voiceChannel = await getChannel(msg, 'Voice1', true).fetch()
if (!voiceChannel) {
msg.channel.send('ERROR: Voice1 channel not found')
} else {
await getCurrentChannelMembers(msg, voiceChannel)
}
}
}
function getChannel(msg, channel, voiceChannel = false) {
return msg.guild.channels.cache.find(c => c.name === channel && (!voiceChannel || c.isVoice()))
}
async function getCurrentChannelMembers (msg, channel) {
const fetchedChannel = await channel.fetch(true)
const members = fetchedChannel.members
console.log('Members: ', members)
}
任何帮助,将不胜感激!
您的机器人需要GUILD_VOICE_STATES
意图
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_VOICE_STATES
]
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.