从消息中获取图像URL

时间:2019-03-28 17:41:31

标签: node.js discord discord.js

我想从消息中获取图像URL或图像本身,并将其放入我的嵌入文件中。我不知道自己做什么,希望您能帮助我:)

client.on('messageReactionAdd', (reaction, user, message) => {
const eggsa = client.emojis.find(emoji => emoji.name === "eggsa");
if (reaction.emoji.name === 'eggsa') {
  const msg = reaction.message;
  const guild = msg.guild;
  const guildMembers = guild.members;
  const guildMember = guildMembers.get(user.id);
  if (!guildMember.roles.some(r=>["Leder"].includes(r.name)) ) return guildMember.send(" ',:^\\ ");
    const message = reaction.message;
    const kanal = reaction.message.guild.channels.find('name', 'sitater');
    var embed = new Discord.RichEmbed()
    .setDescription(message.content)
    .setTimestamp()
    .setFooter(message.author.username, reaction.message.author.avatarURL)
    .setImage()
    kanal.send({embed});
}
});

1 个答案:

答案 0 :(得分:0)

您可以简单地使用消息对象中的.attachments属性。这将返回此消息的所有附件,包括所有图片。从那里开始,您可以使用属性.url,这将为您提供可以添加到嵌入中的图像URL。

在下面的代码中,我添加了一个检查是否有附件。如果是,请添加第一个图片网址作为嵌入图片。

这是代码:

    client.on('messageReactionAdd', (reaction, user, message) => {
    const eggsa = client.emojis.find(emoji => emoji.name === 'eggsa');
    if (reaction.emoji.name === 'eggsa') {
        const msg = reaction.message;
        const guild = msg.guild;
        const guildMembers = guild.members;
        const guildMember = guildMembers.get(user.id);
        if (!guildMember.roles.some(r => ['Leder'].includes(r.name))) return guildMember.send(" ',:^\\ ");
        const message = reaction.message;
        const kanal = reaction.message.guild.channels.find('name', 'sitater');
        const embed = new Discord.RichEmbed()
            .setDescription(message.content)
            .setTimestamp()
            .setFooter(message.author.username, reaction.message.author.avatarURL);

        if (message.attachments.size !== 0) {
            embed.setImage(message.attachments.first().url);
        }

        kanal.send({ embed });
    }
});