简体   繁体   English

如何使用 DiscordJS 显示公会的每个语音频道 ID/名称?

[英]How can I show every voice channel ID/name of a guild with DiscordJS?

I'm trying to get and show all the voice channels name from guild.我正在尝试获取并显示公会的所有语音频道名称。

That's my code, that not working那是我的代码,不起作用

client.on('ready', () => {

    client.channels.fetch().then(channel =>
    {
        console.log(channel.name)
    });

}

I'd like to list all names of voice channels (not text).我想列出语音通道的所有名称(不是文本)。

First you have to retrieve all channels using the channel cache of your client object. Then you filter by their type:首先,您必须使用客户端 object 的频道缓存检索所有频道。然后按类型过滤:

let voiceChannels = client.channels.cache.filter(m => m.type === 'voice');
voiceChannels.forEach(channel => console.log(channel.name));

Filter the channels by type ChannelType.GuildVoice then map them to their name and id.按类型过滤通道ChannelType.GuildVoice然后 map 它们的名称和 ID。

// import or require ChannelType from discord.js

const allChannels = await client.channels.fetch();

const voiceChannels = allChannels
   .filter(ch => ch.type === ChannelType.GuildVoice);

console.log(
   voiceChannels
      .map(ch => `${ch.id} | ${ch.name}`)
      .join('\n')
);

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

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