node.jsdiscord.js

Show how many channels are in a category with discord.js


Is there a way how I can show how many channels are in a category with discord.js?


Solution

  • There is actually a reasonably easy way of doing this. You can fetch all the channels from a guild and then loop through them. You then pick out all the channels which are of type category and get the size of their children property.

    Below is some code which will help you go in the right direction.

    client.on("message", async message =>
    {
        // Do all your validation and whatnot here
        ....
    
        const guild = message.guild;
    
        const categoryChannels = guild.channels.filter(channel => channel.type === "category");
    
        categoryChannels.forEach(channel => {
            console.log(`Category ${channel.name} has ${channel.children.size} channels`);
        });
    });
    
    

    Give it a try and let me know how it goes!