node.js - discord.js v13 我将使用什么代码从 MessageCollector 收集第一个附件(图像或视频)?
问题描述
我到处寻找并尝试了所有我能想到的但一无所获,一切似乎都失败了。
我之前使用的一段代码失败了:
Message.author.send({ embeds: [AttachmentEmbed] }).then(Msg => {
var Collector = Msg.channel.createMessageCollector({ MessageFilter, max: 1, time: 300000 });
Collector.on(`collect`, Collected => {
if (Collected.content.toLowerCase() !== `cancel`) {
console.log([Collected.attachments.values()].length);
if ([Collected.attachments.values()].length > 0) {
var Attachment = [Collected.attachments.values()];
var AttachmentType = `Image`;
PostApproval(false, Mode, Title, Description, Pricing, Contact, Attachment[0], AttachmentType);
} else if (Collected.content.startsWith(`https://` || `http://`) && !Collected.content.startsWith(`https://cdn.discordapp.com/attachments/`)) {
var Attachment = Collected.content.split(/[ ]+/)[0];
var AttachmentType = `Link`;
PostApproval(false, Mode, Title, Description, Pricing, Contact, Attachment, AttachmentType);
console.log(Attachment)
} else if (Collected.content.startsWith(`https://cdn.discordapp.com/attachments/`)) {
var Attachment = Collected.content.split(/[ ]+/)[0];
var AttachmentType = `ImageLink`;
PostApproval(false, Mode, Title, Description, Pricing, Contact, Attachment, AttachmentType);
console.log(Attachment)
}
解决方案
[Collected.attachments.values()].length
永远都是1
。为什么?那么你有这两种可能性:
[ [] ] //length 1
[ [someMessageAttachment] ] //length 1
检查的正确方法是使用扩展运算符 ( ...
)
[...(Collected.attachments.values())].length //returns amount of attachments in the message
推荐阅读
- android - 如何在 Google Maps API 中调整地点的搜索半径?
- azure - Azure ML Studio 中回归神经网络的激活功能?
- python - 压缩长向量由0和1组成
- jquery - 当这个函数里面有带有':'的代码时,它会做什么?
- javascript - sahi:如何在 javascript 中使用转义字符
- matlab - 使用matlab将多个字节的数据从pc串口发送到自定义硬件
- android - Android Kotlin - 自定义视图未在设计编辑器中正确显示
- image - 对 `require` 的调用只需要 1 个字符串字面量参数,但找到了:
- c# - 如何使用 COM 组件解决 SEHException?
- mysql - 在 MYSQL 中使用一个查询更新列