简体   繁体   中英

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:

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.

// 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')
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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