1
votes

I am making a Discord.js Bot that is supposed to log when a channel is created in a specific category.

My Discord Server looks like this (just to clarify what I mean with category):

enter image description here

So for example if channel2 is created in the Category the bot will console log something but if the channel isn't created in the category the bot will do nothing.

This is what I came up with:

client.on("channelCreate", function(channel){
    console.log(`channelCreated: ${channel}`);
});

This code didn't work for me because it logs every channel creation and not only the ones in the category.

If you know how to solve this problem let me know ;)

Thanks in advance

1

1 Answers

1
votes

Assuming you're using the latest v12 series of Discord.js, the channelCreate event's parameter is (in this case) a GuildChannel whose parent property can tell you what category the channel belongs to. So:

client.on("channelCreate", function(channel){
    if (channel.parent?.name === "Category"){
        console.log(`channelCreated: ${channel}`);
    }
});

(Replace the optional chaining ?. if you're using an older JS dialect. Or consider using parentID instead of parent?.name for a unique identifier comparison.)