Glitchy Brick
Glitchy Brick

Reputation: 126

How to Get the Links of Attachments Discord.js

Hey so im Trying to Get the Link for Discord Images But when i try it spits out the error:

    (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.

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

Upvotes: 0

Views: 625

Answers (2)

Glitchy Brick
Glitchy Brick

Reputation: 126

I Have Found The solution

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

Upvotes: 0

Jakye
Jakye

Reputation: 6625

Your code is working if you upload at least an attachment, but I am assuming that you are not uploading any attachments.

The reason why you are getting the error is that Attachment is empty (therefore Attachemnt[0] is undefined) because the following if statement fails:

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

This is because [] or {} (empty arrays/objects) are thruthy.


To your code would have to check the size of the array. If it is 0, there are no attachments in the message.

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

Upvotes: 2

Related Questions