简体   繁体   English

如何通过频道 ID 获取语音的用户数? Discordjs v.13

[英]How do I get the user count of a voice by channel id? Discordjs v.13

How do I get the user count of a voice by channel id?如何通过频道 ID 获取语音的用户数? I'm using Discordjs v.13.我正在使用 Discordjs v.13。

Here's my current code:这是我当前的代码:

async function usercount(channelId) {
  try {
    let voiceChannel = Client.guilds.cache
      .get('guildId')
      .channels.cache.get(channelId);
    let membersInChannel = voiceChannel.members.size;
    console.log(membersInChannel);
  } catch (error) {
    logger.error('Error while performing usercount');
    console.log(error);
  }
}

You can get the number of members by fetching the voice channel by its ID and grabbing its members property.您可以通过其 ID 获取语音通道并获取其members属性来获取成员数。 You'll probably need to use the { force: true } option to skip the cache check and request the API.您可能需要使用{ force: true }选项来跳过缓存检查并请求 API。 This way, when someone leaves/joins the channel, the number will still be accurate.这样,当有人离开/加入频道时,号码仍然是准确的。

Notice that you will either need a guild ID or a guild to which the voice channel belongs.请注意,您将需要公会 ID 或语音频道所属的公会。

async function userCount(guildId, channelId) {
  try {
    let guild = client.guilds.cache.get(guildId);
    let voiceChannel = await guild.channels.fetch(channelId, { force: true });

    return voiceChannel.members?.size;
  } catch (error) {
    logger.error('Error while performing usercount');
    console.log(error);
  }
}

And here is an example how to use this.这是一个如何使用它的示例。 Here, I'm passing down the message.guild.id as the guild ID, and the first argument as the channel ID.在这里,我将message.guild.id作为公会 ID 传递,第一个参数作为频道 ID。

const { Client, Intents } = require('discord.js');

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

client.on('messageCreate', async (message) => {
  if (message.author.bot) return;
  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();
  const channelId = args[0];

  message.reply(
    `There are ${await userCount(
      message.guild.id,
      channelId,
    )} members in the voice channel _"${
      message.guild.channels.cache.get(channelId).name
    }"_`,
  );
});

在此处输入图像描述

Important: Don't forget that you'll need to enable the GUILD_VOICE_STATES intents .重要提示:不要忘记您需要启用GUILD_VOICE_STATES意图 Without that the number of connected members won't change.否则,连接成员的数量不会改变。

It is not possible getting the current number of users in a voice channel by channel ID.无法通过通道 ID 获取语音通道中的当前用户数。 The ID is just like the name of the channel. ID 就像频道的名称一样。 For your bot to identify the channel and to fE change the name of the channel.让您的机器人识别频道并更改频道名称。 However you wont be able to get the currently connected users of the channel.但是,您将无法获取该频道当前连接的用户。

Or did I missunderstand something?还是我误会了什么? Hope this helps希望这可以帮助

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

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