javascript - 如何获取附件的链接 Discord.js
问题描述
嘿,所以我正在尝试获取不和谐图像的链接但是当我尝试时,它会吐出错误:
(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')
这是因为[]
or {}
(空数组/对象) 是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",
};
推荐阅读
- python - 如何将值连接到一组字符串?
- javascript - 使用 jQuery 动态更改 CSS 背景
- python - 带有 tf.data.Dataset 的 RandomShuffleQueue 功能
- java - gradle中的多模块项目编译失败时
- reactjs - 如何解决 TypeError:无法在反应中读取未定义问题的属性“地图”?
- tensorflow - 如何在超参数调优期间最好地保存模型并编写摘要?
- dart - 作用域模型 - 接收者:闭包:({dynamic formData})=> void from Function 'login'
- java - 使用 jersey-multipart 将文件作为多部分参数发送
- ruby-on-rails - 资源/页面存在时删除 ActiveAdmin 自动链接
- angular - 在同一个 Angular 工作区中将多个 Angular 应用程序部署到 Azure