繁体   English   中英

如何获取附件链接 Discord.js

[英]How to Get the Links of Attachments Discord.js

嘿,所以我正在尝试获取 Discord 图像的链接但是当我尝试它时会吐出错误:

    (node:248) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'url' of undefined
    at Object.module.exports.run (C:\Users\nafiu\OneDrive\Desktop\Bots\Glitchz Bot\commands\rankbackground.js:11:123)
    at Client.<anonymous> (C:\Users\nafiu\OneDrive\Desktop\Bots\Glitchz Bot\events\message.js:24:37)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:248) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:248) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这是我的代码:

let fs = require("fs");
const Discord = require("discord.js");
const { Client, MessageAttachment, MessageEmbed } = require("discord.js");
const RankBackgroundModel = require('../models/rankbackground')

module.exports.run = async (client, message, args) => {
    
    const Attachment = (message.attachments).array();
    if(!Attachment) return message.reply('You Didnt Upload a Image to make it your custom rank background')

    const doc = await RankBackgroundModel.findOneAndUpdate({ id: message.author.id }, { $set: { Background: Attachment[0].url} }, {new: true});
    return message.reply('Sucsessfuly Set Rank Background to' + Attachment[0].url)
}

module.exports.help = {
  name: "rankbackground"
}

如果您至少上传一个附件,您的代码就可以工作,但我假设您没有上传任何附件。

您收到错误的原因是Attachment为空(因此Attachemnt[0]未定义),因为以下 if 语句失败:

if(!Attachment) return message.reply('You Didnt Upload a Image to make it your custom rank background')

这是因为[]{} (空数组/对象)是thruthy


您的代码将不得不检查数组的大小。 如果为 0,则邮件中没有附件。

let fs = require("fs");
const Discord = require("discord.js");
const { Client, MessageAttachment, MessageEmbed } = require("discord.js");
const RankBackgroundModel = require("../models/rankbackground");

module.exports.run = async (client, message, args) => {
    const Attachment = message.attachments.array();
    if (Attachment.length === 0) return message.reply("You Didnt Upload a Image to make it your custom rank background");

    const doc = await RankBackgroundModel.findOneAndUpdate({ id: message.author.id }, { $set: { Background: Attachment[0].url } }, { new: true });
    return message.reply("Sucsessfuly Set Rank Background to" + Attachment[0].url);
};

module.exports.help = {
    name: "rankbackground",
};

我找到了解决方案

let fs = require("fs");
const Discord = require("discord.js");
const { Client, MessageAttachment, MessageEmbed } = require("discord.js");
const RankBackgroundModel = require('../models/rankbackground')

module.exports.run = async (client, message, args) => {

    var Attachment = message.attachments.array();
    var OurAttachment = Attachment[0].url;

    if(!Attachment) return message.reply('You Didnt Upload a Image to make it your custom rank background')

    //console.log(OurAttachment)

    const doc = await RankBackgroundModel.findOneAndUpdate({ id: message.author.id }, { $set: { Background: OurAttachment} }, {new: true});
    return message.reply('Sucsessfuly Set Rank Background to:\n' + OurAttachment)
}

module.exports.help = {
  name: "rankbackground"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM