类型错误:无法使用 try catch 读取未定义的属性“内容”?

时间:2021-01-27 16:29:50

标签: node.js discord discord.js

我尝试将 try 和 catch 添加到我的代码中,以便它执行我想要的操作,但是,我收到此错误:

TypeError: Cannot read property 'content' of undefined

这是我的代码:

if (!message.guild.me.hasPermission(`MANAGE_CHANNELS`)) return message.reply('I lack the required permissions to run this command. (Required Permissions: ``MANAGE_CHANNELS``)');          
        if(message.member.hasPermission("MANAGE_CHANNELS")){
            const channel = message.channel;
            
            const Discord = require("discord.js");
            let role = message.mentions.roles.first() || message.guild.roles.cache.find(role => role.name === args.join(" "));
            if(!role){
                message.reply("please mention a role.")
            } else{
                message.reply('Are you sure you want to continue this command, it will disallow send permissions from the role you mentioned. Type ``yes`` to continue.')
                let filter = msg => msg.author.id == message.author.id
                message.channel.awaitMessages(filter, {max: 1, time: 20000}).then(collected => {           
  (Error line)> if (collected.first().content.toLowerCase() !== 'yes') return;
                    try {
                        const embed = new Discord.MessageEmbed()
                                    .setTitle("Channel Locked")
                                    .setColor("RED")
                                    .setDescription(`Channel locked to the specified role`)
                    
                                    message.channel.updateOverwrite(role, {
                                        SEND_MESSAGES: false
                                    })
                                    .then(channel => channel.send(embed))
                                    .catch(console.error)
                    }
                    catch(error) {
                        message.reply("Command either timed out or there was an error.")
                    }
                    })   
            }
                
        }

我完全没有尝试和捕捉的经验,所以我假设我做错了什么。 谢谢

1 个答案:

答案 0 :(得分:0)

prob collect.first() 不返回任何内容。 尝试 console.log(collected) 进行调试。

也许你没有收到消息