简体   繁体   English

Discord.js:特定类别中的日志通道创建

[英]Discord.js: Log Channel Creation in specific Category

I am making a Discord.js Bot that is supposed to log when a channel is created in a specific category.我正在制作一个 Discord.js Bot,它应该在特定类别中创建频道时记录。

My Discord Server looks like this (just to clarify what I mean with category):我的 Discord 服务器看起来像这样(只是为了澄清我对类别的意思):

在此处输入图像描述

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.因此,例如,如果在类别中创建了 channel2,机器人将在控制台记录一些内容,但如果未在类别中创建通道,机器人将什么也不做。

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提前致谢

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.假设您使用的是最新的 v12 系列 Discord.js, channelCreate事件的参数(在这种情况下)是GuildChannel ,其parent属性可以告诉您频道属于哪个类别。 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.) (替换可选链接?.如果您使用的是较旧的 JS 方言。或者考虑使用parentID而不是parent?.name进行唯一标识符比较。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM