首页 > 解决方案 > discord.js V12 - 控制台命令和变量输出显示为未定义

问题描述

所以,我是 JavaScript 新手。我正在制作一个 Discord 机器人,因为它是我目前唯一真正使用 JavaScript 的东西,而且我想学习一些东西。每当我尝试显示类似 id 的内容时,它都会显示未定义。

    client.on("channelCreate", function(channel){
    console.log(channel.type+` channel, `+channel.name+`, was created by `+`${channel.owner_id}`+` in the category `+channel.parent_id);
    });

我想为我的机器人制作一个基本的日志部分,而我才刚刚开始。我希望它像

00:00:00 CREATE >  [Text / Voice] Channel was created by [Creator] under category [category]!

我知道确切的格式没有在代码中实现,我在让它变得漂亮之前测试了很多函数。但结果是这样的(使用我现在拥有的代码)

text channel, s, was created by undefined in the category undefined

我很困惑,因为我喜欢细节,所以我想要谁创建它,什么时候,还有类别!那我该怎么做呢?

另外,有没有办法从控制台发送消息和东西?就像我在控制台中输入的一样

[channel or user] > [the message]
#testchannel > Announcing new video or something idk

并将其发送到频道?任何帮助表示赞赏!

编辑:我忘了提到我有 owner_id${var}格式的部分,因为我正在测试它是否可以使它工作,但我从来没有把它换回来大声笑

标签: javascriptdiscord.js

解决方案


更改channel.parent.name会起作用,但没有办法知道谁创建了频道,这是代码 -

client.on("channelCreate", function(channel){
    console.log(channel.type+` channel, `+channel.name+`,`+` in the category `+channel.parent.name);
    });

推荐阅读