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] });
};
You need to filter what channels you want to count for example text channels
interaction.guild.channels.cache.filter(ch => ch.type === 'GUILD_TEXT').size
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