Discord.js:无法读取未定义的属性“类型”

因此,我尝试制作一个机器人,在本例中为通道create / delete命令,没有任何js知识(对此我非常讨厌),并且运行不正常

这是代码:

}else if (args[0] === "delete") {
      console.log('deleting')
      let ch = message.content.split(`${bot.prefix}vchannel ${args[0]}`).join('')
      let fetchedChannel = message.guild.channels.cache.find(r=>r.name === ch)
      if (fetchedChannel.type === "voice") {
        fetchedChannel.delete()
      message.channel.send(`Голосовой канал ${ch} удален`).then(msg=>msg.delete({timeout:5000}))
      console.log('delete complete')
      }
    }
    }

并且有错误:

(node:5216) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'type' of undefined

I tried to remove ch:

}else if (args[0] === "delete") {
      console.log('deleting')
      let fetchedChannel = message.guild.channels.cache.find(r=>r.name === args[1].join(''))
      if (fetchedChannel.type === "voice") {
        fetchedChannel.delete()
      message.channel.send(`Голосовой канал ${args[1].join('')} удален`).then(msg=>msg.delete({timeout:5000}))
      console.log('delete complete')
      }
    }
    }

但出现其他错误:

TypeError: args[1].join is not a function

试图自己找到解决方案,但我失败了

请帮助,我要疯了。

如果需要更多代码,我可以发送。

另外,我的英语不好,我会犯错,请原谅我

评论
caut
caut

In your first code, the error means there is no channel in message.guild.channels.cache that has name === ch, so fetchedChannel is undefined. It is the stablished result for not found.

In your second code, if args[1] is a string, you are trying to use an Array method, join, on a string that cannot be.

If you have a debugger, use it to trace variable changes and values. If you do not have one, use console.log to output the intermediate values.

清单:

  • Is message.content a string that contains ${bot.prefix}vchannel ${args[0]}? That line of code is just removing every occurrence of ${bot.prefix}vchannel ${args[0]}, because you split and join with "".

  • Is args[1] the string that you need?

点赞
评论