如何修复我的机器人的“附件不是构造函数”错误?

当我为 Discord 制作机器人并尝试附加图像时,由于此错误,我无法


这是一个运行在 Discord 上的机器人Discord.js 我在开始时尝试了 const Attachment,但这没有用,删除代码中的 new 和 const 也不起作用


        case 'about':

            message.channel.send('I am Damabot, developed by Damadion!')

            const attachment = new Attachment('./DidYouThinkIAmTheRealFace.png')

            message.channel.send('I am Damabot, developed by Damadion!' + attachment)

            console.log('Bot successfully replied')

            break;


我希望它发送附件,但它没有发送并发送此错误


人到中年有点甜
浏览 191回答 3
3回答

元芳怎么了

你可以这样做:message.channel.send('I am Damabot, developed by Damadion!', { files: ['./DidYouThinkIAmTheRealFace.png'] });它将文件直接添加到消息中,因此您不必创建附件。我用这个我的 BOT,它工作得很好。

忽然笑

对我来说,以下代码有效:const attachment = new Discord.MessageAttachment("url");channel.send(attachment);Discord.Attachment 被替换为 Discord.MessageAttachement

qq_花开花谢_0

Attachment是 Discord.js 中的一个类。除非您对 require 语句 ( const { Attachment } = require('discord.js'))使用解构赋值,否则Node.js 会尝试根据代码中的类构造一个 Attachment 对象。当它发现没有时,它会抛出您的错误。如果你想坚持构造附件对象,你可以使用:const attachment = new Discord.Attachment('./path/to/file.png', 'name'); // name is optionalmessage.channel.send('Hey there', attachment)  .catch(console.error);否则,您可以files像这样使用消息的属性:message.channel.send('Hey there', {  files: ['./path/to/file.png']}).catch(console.error);后者允许您也发送一个嵌入(并且可能在您的嵌入中使用附件)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript