Reputation: 50
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):
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
Upvotes: 1
Views: 488
Reputation: 2099
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.)
Upvotes: 1