Serverinfo interaction slash command showing 3 channels, but I have only 2 channels in the server

Viewed 174

serverinfo interaction slash command showing 3 channels, but I have only 2 channels in the server. It is including the category too, how I fix it?

Here is the code -

if (commandName === 'serverinfo') {
  let serverinfoembed = new MessageEmbed()
  .setAuthor(`Info for ${interaction.guild.name}`)
  .setColor('#4269f5')
  .addField('Owner', `<@${interaction.guild.ownerId}>`, true)
  .addField('Channels', `${interaction.guild.channels.cache.size}`, true)
  return interaction.reply({ embeds: [serverinfoembed] });
};
2 Answers

In Discord.js, it counts categories as channels. To filter out categories, you need to do the following:

interaction.guild.channels.cache.filter(c => c.type !== "GUILD_CATEGORY").size

This will display the amount of cached text and voice channels. Filtering out voice/text channels are just as easy:

//Getting text channels
interaction.guild.channels.cache.filter(c => c.type === "GUILD_TEXT").size
//Getting VCs
interaction.guild.channels.cache.filter(c => c.type === "GUILD_VOICE").size
Related