John Flamesinger
John Flamesinger

Reputation: 45

Is there a way to count the channels inside of a category in discord.js?

I'm trying to write a bot that will tell me the number of channels in a specific category.

if (strMessage === "!outline") {
  var outlineSize;

  message.reply("There are " + outlineSize + " outlines currently hidden away. Type '?outline' to learn more about them.");
}

I'm trying to set outlineSize to the number of channels in the category (the category is always the same, hence the specific variable name). For example, if there are two channels I want outlineSize to equal 2.

I know that it has something to do with CategoryChannelChildManager in discord.js v14. However, I've found nothing that's been able to teach me how to use it. To be fair, I'm new to programming and JavaScript which is where I expect the main part of the problem lies.

Also note: There are a few similar questions on this site that pertain to this, but due to the always-changing nature of Discord's API I don't believe they've been accurate for over a year now and give me nothing but errors when I try them.

Upvotes: 3

Views: 935

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23160

You can fetch the category channel by its ID and grab its children property. As you've mentioned it's a CategoryChannelChildManager so you will need to access its cache that returns a Collection. And finally, Collections have a size property that returns the number of items in the given collection. That's what you'll need:

let CATEGORY_ID = '813845582854001392';
let category = await client.channels.fetch(CATEGORY_ID);
let outlineSize = category.children.cache.size;

message.reply(`There are ${outlineSize} outlines currently hidden away. Type '?outline' to learn more about them.`);

Upvotes: 1

Related Questions