How to save data in the empty Array from message attachments? So I can send more attachments in one message Discord.js
Asked Answered
O

3

6

How I can fill the empty array with attachments url, so I can send them all of them in one message? Because when I send use url varaible, same message will be sent several times depending on attachment numbers. This is what I tried so far, but I can't pass through this.. I'm out of ideas.

      message.attachments.forEach(attachment => {
        const url = attachment.url;
        if(url) {
        let links = []
        Array.from(message.attachments).forEach(links => console.log(links));
        const exampleEmbed = new MessageEmbed()
        .setColor('#ff0000')
        .setAuthor(lastMessage.author.username, message.author.avatarURL({ dynamic: true }))
        .setTitle(`(Report)Bug report from ` + lastMessage.author.username + `#` + lastMessage.author.discriminator)
        .setURL('https://discord.com/channels/' + lastMessage.guildId + '/' + lastMessage.channelId + '/' + lastMessage.id)
        .setDescription(lastMessage.content.replace(/^([^ ]+ ){2}/, '') + ' ' + links);
        client.channels.cache.get('ID').send({ embeds: [exampleEmbed] });
        }
      })
    }
  }

Problem is I'm unable to fill links array with the urls , so then I can use them in the description, also links inside of .setDescription does not provide any value, it's just empty, so it seems that .forEach(links => console.log(links)); doesn't add anything to the let links = []

I was suggested to use this

Array
   .from(message.attachments)
   .forEach(...);

// Or

[...message.attachments]
   .forEach(...);

Trying it with Array.from(message.attachments).forEach, however I can't figure out how to use it correctly, I'm completely confused from the another examples that I saw(general examples) Every help will be much appreciated..

Log from the Array.from(message.attachments).forEach(links => console.log(links));

[
  '924775808831197184',
  MessageAttachment {
    attachment: 'https://cdn.discordapp.com/attachments/924298360603672576/924775808831197184/bug1.gif',
    name: 'bug1.gif',
    id: '924775808831197184',
    size: 5314,
    url: 'https://cdn.discordapp.com/attachments/924298360603672576/924775808831197184/bug1.gif',
    proxyURL: 'https://media.discordapp.net/attachments/924298360603672576/924775808831197184/bug1.gif',
    height: 128,
    width: 128,
    contentType: 'image/gif'
  }
]
[
  '924775809040932945',
  MessageAttachment {
    attachment: 'https://cdn.discordapp.com/attachments/924298360603672576/924775809040932945/bug.gif',
    name: 'bug.gif',
    id: '924775809040932945',
    size: 5314,
    url: 'https://cdn.discordapp.com/attachments/924298360603672576/924775809040932945/bug.gif',
    proxyURL: 'https://media.discordapp.net/attachments/924298360603672576/924775809040932945/bug.gif',
    height: 128,
    width: 128,
    contentType: 'image/gif'
  }
]
[
  '924775808831197184',
  MessageAttachment {
    attachment: 'https://cdn.discordapp.com/attachments/924298360603672576/924775808831197184/bug1.gif',
    name: 'bug1.gif',
    id: '924775808831197184',
    size: 5314,
    url: 'https://cdn.discordapp.com/attachments/924298360603672576/924775808831197184/bug1.gif',
    proxyURL: 'https://media.discordapp.net/attachments/924298360603672576/924775808831197184/bug1.gif',
    height: 128,
    width: 128,
    contentType: 'image/gif'
  }
]
[
  '924775809040932945',
  MessageAttachment {
    attachment: 'https://cdn.discordapp.com/attachments/924298360603672576/924775809040932945/bug.gif',
    name: 'bug.gif',
    id: '924775809040932945',
    size: 5314,
    url: 'https://cdn.discordapp.com/attachments/924298360603672576/924775809040932945/bug.gif',
    proxyURL: 'https://media.discordapp.net/attachments/924298360603672576/924775809040932945/bug.gif',
    height: 128,
    width: 128,
    contentType: 'image/gif'
  }
]
Overview answered 26/12, 2021 at 21:33 Comment(0)
V
1

If you want to fill an array named links with all attachment URLs from a message, use this simple one-liner

const links = message.attachments.map(a => a.url)

This gets all the message's attachments, and maps it with the URLs.

Vigorous answered 27/12, 2021 at 5:17 Comment(0)
O
0

I found a solution.

I had to create empty variable, then from the link array get the result as a string and then use forEach, this will store the links in the result and then bot will send attachments links separated by \n in my case in one embed message.

      let result = '';
      message.attachments.forEach(attachment => {
        const url = attachment.url;
        if(url) {
          let links = [url]
          links.forEach(function(link) {
            result = result+'\n'+link;
          })
        }
      });
      const exampleEmbed = new MessageEmbed()
      .setColor('#ff0000')
      .setAuthor(lastMessage.author.username, message.author.avatarURL({ dynamic: true }))
      .setTitle(`(Report)Bug report from ` + lastMessage.author.username + `#` + lastMessage.author.discriminator)
      .setURL('https://discord.com/channels/' + lastMessage.guildId + '/' + lastMessage.channelId + '/' + lastMessage.id)
      .setDescription(lastMessage.content.replace(/^([^ ]+ ){2}/, '') + ' ' + result);
      client.channels.cache.get('ID').send({ embeds: [exampleEmbed] });
      }
    }
  }
Overview answered 27/12, 2021 at 21:20 Comment(0)
L
0

The problem lies in this part:

let links = [];
Array.from(message.attachments).forEach(links => console.log(links));

In the first string you create a "links" variable and assign a empty array to it. In the second line you create an new array from "message.attachments" not assigning it to anything and output all of its elements to console. So after second line the "links" variable is still empty array but the console output is filled with "message.attachments" array items data.

You need to change second string to:

Array.from(message.attachments).forEach(messageAttachment =>
  links.push(messageAttachment[1].url)
);

This way you for each element of "message.attachments" array (which is array and consists of two elements: the string with number and an MessageAttachment object) than get an element with index "1" of this array (the MessageAttachment object itself) and get the "url" field from it and then add with push method to a "links" array.

Liberati answered 6/12, 2023 at 13:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.