为什么我的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
相关产品推荐
相关产品推荐

