(节点:2724)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ first”

时间:2020-09-09 11:40:54

标签: javascript discord discord.js

错误:

(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'));
    }
}

我想做什么:

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

1 个答案:

答案 0 :(得分:0)

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

您已将max选项设置为1,因此它将仅包含一条消息,但该消息仍位于集合中。

尝试这样的事情:

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

我建议将fileMessage重命名为fileMessages,以便更清楚地知道它是一个集合。

相关问题