Search code examples
javascriptdiscorddiscord.js

Message.embeds is undefined even though there is an embed displayed


This is the code I currently use to try and get the embeds from a message

const reportMessage = await interaction.channel.messages.fetch({
            limit: 1,
            cache: false,
        });

return console.log(reportMessage.embeds);

Now the kicker, reportMessage DOES exist, and I can console.log it and it returns the following (everywhere with -- is redacted for privacy):

Collection(1) [Map] {
  '--' => Message {
    channelId: '--',
    guildId: '--',
    id: '--',
    createdTimestamp: --,
    type: 0,
    system: false,
    content: '',
    author: ClientUser {
      id: '--',
      bot: true,
      system: false,
      flags: [UserFlagsBitField],
      username: '--',
      globalName: null,
      discriminator: '--',
      avatar: '--',
      banner: null,
      accentColor: null,
      avatarDecoration: null,
      verified: true,
      mfaEnabled: true
    },
    pinned: false,
    tts: false,
    nonce: null,
    embeds: [ [Embed] ],
    components: [],
    attachments: Collection(0) [Map] {},
    stickers: Collection(0) [Map] {},
    position: 0,
    roleSubscriptionData: null,
    resolved: null,
    editedTimestamp: null,
    reactions: ReactionManager { message: [Message] },
    mentions: MessageMentions {
      everyone: false,
      users: Collection(0) [Map] {},
      roles: Collection(0) [Map] {},
      _members: null,
      _channels: null,
      _parsedUsers: null,
      crosspostedChannels: Collection(0) [Map] {},
      repliedUser: null
    },
    webhookId: null,
    groupActivityApplication: null,
    applicationId: null,
    activity: null,
    flags: MessageFlagsBitField { bitfield: 0 },
    reference: null,
    interaction: null
  }
}

But when I try and console.log(reportMessage.embeds) that returns undefined, even though as you can see there is a collection of embeds. Trying console.log(reportMessage.embeds[0]) also doesn't work, even though it's supposed to be an array of all the embeds, and I'm taking the first one.

I was expecting to get an object with the embed inside of it, since thats clearly, what my code is trying to do. Does anyone know how to actually get an object with the embed inside of it?

Thanks in advance for the advice.


Solution

  • await interaction.channel.messages.fetch({ limit: 1, cache: false}); returns a collection. The property embeds does not exist on a collection. If you want to use the first message from the collection you could use the first() function which returns the first element.

    const reportMessage = await interaction.channel.messages.fetch({
                limit: 1,
                cache: false,
            });
    
    return console.log(reportMessage.first().embeds);