Home > Mobile >  How to save data in the empty Array from message attachments? So I can send more attachments in one
How to save data in the empty Array from message attachments? So I can send more attachments in one

Time:12-28

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'
  }
]

CodePudding user response:

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.

CodePudding user response:

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] });
      }
    }
  }
  • Related