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

为什么我的Discord机器人获取到频道链接而非附件下载链接

问题根因

  • Discord.js 中消息对象的附件存储在**复数属性attachments**中,是一个Map类型的集合,你代码中调用的单数attachment是不存在的属性,所以控制台输出undefined
  • collected.first().url返回的是当前消息的页面跳转链接,不是附件的下载直链,附件的直链存储在每个MessageAttachment实例的url属性中
  • 原代码中catch直接传入字符串是无效写法,必须传入回调函数处理异常

修复后代码

module.exports = {
    commands: 'test',
    callback: (message, arguments, text) => {
        const msg_filter = (m) => m.author.id === message.author.id;
        message.channel.awaitMessages({ filter: msg_filter, max: 1, time: 60000})
        .then((collected) => {
            const receivedMsg = collected.first();
            const targetAttachment = receivedMsg.attachments.first();
            if (!targetAttachment) {
                return message.channel.send('未检测到上传文件');
            }
            // 此处即可拿到可直接下载的Discord CDN直链
            const directDownloadUrl = targetAttachment.url;
            message.channel.send(`文件下载直链:${directDownloadUrl}`);
            console.log("finish")
        }).catch(() => {
            message.channel.send('超时未收到回复');
        })
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 21:54:02