为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

人到中年有点甜 2021-06-19 14:01:57
当我为 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;我希望它发送附件,但它没有发送并发送此错误
查看完整描述

3 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

你可以这样做:

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

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


查看完整回答
反对 回复 2021-06-24
?
忽然笑

TA贡献1806条经验 获得超5个赞

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


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

channel.send(attachment);

Discord.Attachment 被替换为 Discord.MessageAttachement


查看完整回答
反对 回复 2021-06-24
?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

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);

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


查看完整回答
反对 回复 2021-06-24
  • 3 回答
  • 0 关注
  • 190 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信