简体   繁体   English

如何在 DiscordJS 中让用户进入没有缓存的语音通道?

[英]How to get users in voice channel without cache in DiscordJS?

I'm trying to get all users connected to a voice channel on my server.我正在尝试让所有用户连接到我服务器上的语音通道。 When someone talks to a bot in #general , I want to get the users inside Voice Channel 1 .当有人在#general中与机器人交谈时,我想让用户进入Voice Channel 1

I'm using Node 17 and DiscordJS 13.我正在使用节点 17 和 DiscordJS 13。

This is my code:这是我的代码:

message.guild.channels
  .fetch(channelID, { cache: false, force: true })
  .then((channels) => {
    console.log(channels.members);
  });

Also, I tried this one:另外,我试过这个:

let voiceChannel = client.guilds.cache
  .get(process.env.DISCORDJS_GUILD_ID)
  .channels.cache.get(process.env.DISCORDJS_CHANNEL_ID);
let membersInChannel = voiceChannel.members;
console.log(membersInChannel);

But, it always returns the voice channel users that joined when I start the node app.但是,它总是返回我启动节点应用程序时加入的语音频道用户。 If someone leaves the voice channel, it keeps showing him in the console.log when I say something to the bot in #general .如果有人离开语音频道,当我在#general中对机器人说些什么时,它会一直在console.log中显示他。 How can I achieve this?我怎样才能做到这一点?

I've had the same problem.我有同样的问题。 Although I added { force: true } as the option in fetch , it always showed the same number of members when I started the app, ignoring the ones who joined/left the channel.虽然我在fetch中添加了{ force: true }作为选项,但当我启动应用程序时,它总是显示相同数量的成员,忽略加入/离开频道的成员。

Enabling the GUILD_VOICE_STATES intent solved this.启用GUILD_VOICE_STATES意图解决了这个问题。 So don't forget to add it to your client :所以不要忘记将它添加到您的client

const { Client, Intents } = require('discord.js');
const client = new Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
    Intents.FLAGS.GUILD_VOICE_STATES,
  ],
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM