You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.js机器人调用new Discord.Attachment()报错无法运行该如何解决

问题原因

Discord.Attachment 构造函数已在discord.js v12及以上版本被弃用移除,这是你代码报错的核心原因。

修复方案

根据你使用的discord.js版本选择对应修改方式:

  • 如果你使用v12、v13版本:
    仅需将构造函数替换为Discord.MessageAttachment即可,修改后代码如下:
    } else if (msg.content === '?_saifz test') {
          const attachment = new Discord.MessageAttachment('https://th.bing.com/th/id/OIP.CK95Aog4xZCaWVHnAr61pwHaHa?w=183&h=183&c=7&o=5&dpr=1.25&pid=1.7')
          msg.channel.send(attachment)
        }
        })
        keepAlive()
        client.login(process.env.lolUCannotGetToken)
    
  • 如果你使用v14及以上版本:
    除了替换构造函数名称,还需要调整消息发送逻辑,将附件放入files数组参数中,修改后代码如下:
    } else if (msg.content === '?_saifz test') {
          const attachment = new Discord.MessageAttachment('https://th.bing.com/th/id/OIP.CK95Aog4xZCaWVHnAr61pwHaHa?w=183&h=183&c=7&o=5&dpr=1.25&pid=1.7')
          msg.channel.send({
            files: [attachment]
          })
        }
        })
        keepAlive()
        client.login(process.env.lolUCannotGetToken)
    
补充说明

如果后续需要发送本地文件作为附件,仅需将URL参数替换为本地文件的相对/绝对路径即可,构造函数的使用规则不变。

内容的提问来源于stack exchange,提问作者Aaseer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 07:30:04