【问题标题】:How to Get the Links of Attachments Discord.js如何获取附件的链接 Discord.js
【发布时间】:2021-05-13 08:10:03
【问题描述】:

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

    (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"
}

【问题讨论】:

    标签: javascript node.js discord discord.js attachment


    【解决方案1】:

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

    您收到错误的原因是 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",
    };
    

    【讨论】:

      【解决方案2】:

      我找到了解决办法

      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"
      }
      

      【讨论】:

      • 请解释您的解决方案,以防其他人遇到同样的问题并找到此帖子。此外,如果您不提供附件,此代码将崩溃。
      猜你喜欢
      • 2021-07-16
      • 2021-10-28
      • 2021-04-06
      • 2011-12-20
      • 2022-07-24
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 2020-04-19
      相关资源
      最近更新 更多