Trying to make a counter for text and voice channels. So it should exclude the amoount of categories.
module.exports = async (client) => {
const guild = client.guilds.cache.get('912706237806829598');
setInterval(async () => {
const channelCount = (await guild.channels.fetch()).filter(
(channel) => channel.type !== 'category'
).size;
const channel = guild.channels.cache.get('960790479925039124');
channel.setName(`╭🎚・Channels: ${channelCount.toLocaleString()}`);
console.log('Updating Channel Count');
console.log(channelCount);
}, 600000);
};
This gives me 183 which is wrong. The number I want to achieve is 155. I have 28 categories, so 183 makes sense if the categories aren't filtered. Filtering the categories has been a bigger struggle that I anticipated.
I have also tried to filter on the cache. guild.channels.cache.filter(channel => channel.type !== 'category').size; but it results in the same way (183). So the filtering isn't working as intended.
If you're using discord.js v13+, channel types are now uppercase and align with Discord's naming conventions. See below the changes:
| channel type | v12 | v13 |
|---|---|---|
| DM channel | dm |
DM |
| group DM channel | N/A | GROUP_DM |
| guild text channel | text |
GUILD_TEXT |
| guild text channel's public thread channel | N/A | GUILD_PUBLIC_THREAD |
| guild text channel's private thread channel | N/A | GUILD_PRIVATE_THREAD |
| guild voice channel | voice |
GUILD_VOICE |
| guild stage voice channel | N/A | GUILD_STAGE_VOICE |
| guild category channel | category |
GUILD_CATEGORY |
| guild news channel | news |
GUILD_NEWS |
| guild news channel's public thread channel | N/A | GUILD_NEWS_THREAD |
| guild store channel | store |
GUILD_STORE |
| generic channel of unknown type | unknown |
UNKNOWN |
As you can see, category is now GUILD_CATEGORY. It means, your filter (channel.type !== 'category') won't match anything, as there is no category type anymore. To fix this, you can use the following:
const channelCount = (await guild.channels.fetch()).filter(
(channel) => channel.type !== 'GUILD_CATEGORY'
).size;