首页 > 解决方案 > 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)
                            }

标签: node.jsdiscord.js

解决方案


[Collected.attachments.values()].length永远都是1。为什么?那么你有这两种可能性:

[ [] ] //length 1
[ [someMessageAttachment] ] //length 1

检查的正确方法是使用扩展运算符 ( ...)

[...(Collected.attachments.values())].length //returns amount of attachments in the message

推荐阅读