首页 > 解决方案 > 不和谐.js | 无法读取未定义的属性“setParent”| 类别创建

问题描述

client.on('message', async (message, user) => {

    if(message.content == "!createcategory"){
        //const name = message.content.replace('!createcategory ', '')
        
        if(message.guild.channels.cache.find(c => c.name == message.author.username && c.type == "category") === undefined){
            message.guild.channels.create(message.author.username, {
                type: 'category',
                permissionOverwrites: [
                    {id: message.guild.id, deny: ['VIEW_CHANNEL']},
                    {id: message.author.id, allow: ['VIEW_CHANNEL']},
                ]
            }).then(parent => {
                // Create the text channel
                message.guild.channels.create('Text channel', {
                    type: 'text',
                    // under the parent category
                    parent, // shorthand for parent: parent
                    permissionOverwrites: [
                        {id: message.guild.id, deny: ['VIEW_CHANNEL']},
                        {id: message.author.id, allow: ['VIEW_CHANNEL']},
                    ]
                }).catch(console.error)
                // Same with the voice channel
                message.guild.channels.create('Voice channel', {
                    type: 'voice',
                    parent,
                    permissionOverwrites: [
                        {id: message.guild.id, deny: ['VIEW_CHANNEL']},
                        {id: message.author.id, allow: ['VIEW_CHANNEL']},
                    ]
                }).catch(console.error)
            }).then(channel => {
        let category = message.guild.channels.cache.find(c => c.name == message.author.username && c.type ==       "category");
    
        if (!category) throw new Error("Category channel does not exist");
        channel.setParent(category.id);
      }).catch(console.error);
         } else {message.author.send("<@" + message.author.id + ">" + ' Jau turi sukurtą kategoriją su kanalais.').then(msg => {
            msg.delete({timeout:15000})}
            )
        }
    }
});

我收到一个错误Cannot read property 'setParent' of undefined ,我正在尝试向语音和文本频道添加权限,但不在类别中。用户将有权更改文本和语音频道名称、移动成员、踢他们、更改他们的名字、静音和聋。

标签: javascriptdiscord.js

解决方案


发生错误是channel因为undefined该功能

parent => {
    message.guild.channels.create('Text channel', {/* ... */}).catch(console.error)
    message.guild.channels.create('Voice channel', {/* ... */}).catch(console.error)
}

返回undefined(您没有明确返回任何内容)。这意味着message.guild.channels.create(message.author.username, { ... }).then( ... )返回一个解析为undefined.

有关更多信息,请参阅MDN 关于使用 Promise 的文章


看起来您从旧问题中保留了一些不再需要的代码。您不需要设置文本或语音通道的父级 - 这些是在创建时设置的。

您的最终代码应如下所示:

message.guild.channels.create(message.author.username, {type: 'category', /* ... */})
    .then(parent =>
        Promise.all([
            message.guild.channels.create(
                'Text channel',
                {type: 'text', parent, /* ... */}
            ),
            message.guild.channels.create(
                'Voice channel',
                {type: 'voice', parent, /* ... */}
            )
        ])
    )
    .then(() =>
        message.author.send("<@" + message.author.id + ">" + ' Jau turi sukurtą kategoriją su kanalais.')
    )
    .then(msg => msg.delete({timeout:15000}))
    // While you didn't handle any errors from the channel category creation in
    // your original code, I assume you want to log all errors
    .catch(console.error)

此外,您可以使用带有模板文字的字符串插值来提及某人(有关更多信息,请参见此处),因此发送消息的代码可以更改为

message.author.send(`${message.author} Jau turi sukurtą kategoriją su kanalais.`)

推荐阅读