Skip to content
Advertisement

Discord js v13 channel filter not working

I’m currently trying to get the total amount of text channels and voice channels to display in my embed, when I try to filter them as I did in discord.js v12 it gives me an output of 0 but if I use no filter and do guild.channels.cache.size, it prints 4 which is the correct amount ( 2 text channels, 1 voice channel , 1 category channel).

If anyone can explain why it’s printing 0 and not the correct amount of text/voice channels that would be amazing, I’ve searched everywhere and can’t find a reason why it wouldn’t work.

const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed } = require('discord.js');

// EXPORT SERVERINFO COMMAND DATA TO NODE
module.exports = ({
    data: new SlashCommandBuilder()
        .setName('serverinfo')
        .setDescription('Basic Server Info.'),
    async execute(interaction) {
        // REFERENCE THE GUILD
        const guild = interaction.guild;
        // CREATE TEST EMBED
        const serverInfoEmbed = new MessageEmbed();
        serverInfoEmbed.setColor('#36393F');
        serverInfoEmbed.setAuthor('Fyce Bot - /serverinfo', interaction.user.avatarURL(), 'https://github.com/ttommie/fyce-bot/');
        serverInfoEmbed.setTitle('Server Information');
        serverInfoEmbed.setThumbnail(guild.iconURL());
        serverInfoEmbed.addFields(
            { name: 'Name', value: `${guild.name}`, inline: true },
            { name: 'u200B', value: 'u200B', inline: true },
            { name: 'Owner', value: `<@${guild.ownerId}>`, inline: true },
            { name: 'Total Members', value: `${guild.memberCount}`, inline: true },
            { name: 'Users Count', value: `${guild.members.cache.filter(member => !member.user.bot).size}`, inline: true },
            { name: 'Bots Count', value: `${guild.members.cache.filter(member => member.user.bot).size}`, inline: true },
            { name: 'Text Channels', value: `${guild.channels.cache.filter(channels => channels.type === 'text').size}`, inline: true }, // PROBLEM HERE 
            { name: 'Voice Channels', value: `${guild.channels.cache.filter(c => c.type === 'voice').size}`, inline: true }, // PROBLEM HERE 
            { name: 'Roles Count', value: `${guild.roles.cache.size}`, inline: true },
        );
        serverInfoEmbed.setFooter(`${guild.name} - Date Created`);
        serverInfoEmbed.setTimestamp(`${guild.createdAt.toUTCString().substr(0, 16)}`);

        await interaction.reply({ embeds: [serverInfoEmbed] });
    },
});

Advertisement

Answer

Discord.js v13 changed the possible values of Channel.type.

Here is how you change it

//text channel filter
- guild.channels.cache.filter(c => c.type === 'text')
+ guild.channels.cache.filter(c => c.type === 'GUILD_TEXT')

//vc filter
- guild.channels.cache.filter(c => c.type === 'voice')
+ guild.channels.cache.filter(c => c.type === 'GUILD_VOICE')

//category filter
- guild.channels.cache.filter(c => c.type === 'category')
+ guild.channels.cache.filter(c => c.type === 'GUILD_CATEGORY')

Replace whatever is preceded with a - with the text below which is preceded with a +

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement