繁体   English   中英

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

[英]How to fix this 'Attachment is not a constructor' error with my bot?

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

这是一个在Discord.js上运行的 Discord 机器人我在开始时尝试了 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;

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

你可以这样做:

message.channel.send('I am Damabot, developed by Damadion!', { files: ['./DidYouThinkIAmTheRealFace.png'] });

它将文件直接添加到消息中,因此您不必创建附件。 我用这个我的 BOT,它工作得很好。

Attachment是 Discord.js 中的一个类。 除非您对 require 语句( const { Attachment } = require('discord.js') )使用解构赋值,Node.js 会尝试根据代码中的类构造一个 Attachment 对象。 当它发现没有时,它会抛出您的错误。

如果你想坚持构造附件对象,你可以使用:

const attachment = new Discord.Attachment('./path/to/file.png', 'name'); // name is optional

message.channel.send('Hey there', attachment)
  .catch(console.error);

否则,您可以像这样使用消息的files属性:

message.channel.send('Hey there', {
  files: ['./path/to/file.png']
})
.catch(console.error);

后者允许您也发送一个嵌入(并且可能在您的嵌入中使用附件)。

Discord.js 文档

对我来说,以下代码有效:

const attachment = new Discord.MessageAttachment("url");
channel.send(attachment);

Discord.Attachment被替换为Discord.MessageAttachement

我有同样的问题。 我运行npm install来更新我的软件包并检查我的不和谐版本是否已过时并因此导致任何问题。 在那之后,我决定浏览文档并在这里找到这个片段:

const attachment=new Discord.MessageAttachment("enter the URL u wanna enter here")
const generalChannel=client.channels.cache.get("enter ur channel id here")
$generalChannel.send(attachment);

至少,它对我有用。 请让我知道它是否也适用于您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM