【问题标题】:(node:2724) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'first' of undefined(节点:2724)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“第一个”
【发布时间】:2020-12-27 20:13:38
【问题描述】:

错误:

(node:2724) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'first' of undefined
    at Object.execute (C:\Users\ESP\Desktop\jetSerenity\Bots\jetMusic\Commands\upload.js:20:45)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:2724) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2724) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

守则:

const { MessageEmbed } = require(`discord.js`);

const fs = require('fs')
const request = require(`request`);

module.exports = {
    name: 'upload',
    description: 'command used to upload .mp3 file',
    async execute(message, args){
        const HelpEmbed = new MessageEmbed()
            .setTitle('Test')
            .setColor(0xff0000)
            .setDescription('Hello there ESP')
        message.channel.send(HelpEmbed);
        // Test 
        let fileMessage = await message.channel.awaitMessages(m => m.author.id == message.author.id, { max: 1, time: 300000 })
        request.get(fileMessage.attachments.first().url)
            .on('error', console.error)
            .pipe(fs.createWriteStream('./MusicFiles'));
    }
}

我想做的事:

我基本上希望它提示我已经设置为测试的嵌入,然后等待发送附件,然后下载附件并将其放入文件夹中,任何帮助将不胜感激,谢谢前进。

【问题讨论】:

  • 错误表示fileMessage没有attchment

标签: javascript discord discord.js


【解决方案1】:

awaitMessages 被解析后,它会为您提供一个消息集合,而不仅仅是一条消息。

您已将max 选项设置为1,因此它将只包含一条消息,但这条消息仍在集合中。

试试这样的:

fileMessage.first().attachments.first().url

我建议将fileMessage重命名为fileMessages,这样更清楚它是一个集合。

【讨论】:

  • 谢谢!这似乎解决了附件获取 URL 问题!
  • 但是它给了我一些非法的目录错误,我看看我能不能解决这个问题,:)
猜你喜欢
  • 2021-10-10
  • 2020-08-18
  • 2020-12-11
  • 1970-01-01
  • 2019-12-15
  • 2021-08-31
  • 2021-09-01
  • 2021-07-05
  • 1970-01-01
相关资源
最近更新 更多