Read contents of an embed message from a discord server

MIke picture MIke · Sep 6, 2017 · Viewed 10.9k times · Source

Scenario: I am trying to read various fields in an embed message posted to a sever, do some processing, and log results in a DB.

Testing: Using a testBot for sending relevant messages everything works when using a normal text message, but when an "embed message" is used (theoretically making it much easier to identify fields for processing etc), I can't retrieve the data. I'm at an total loss how to access the "embed" from the message object.

I realize that it is about now I should pop in some code for you to examine, but I'm not even that far along ! Reading the documentation (linked to at the end) I am pretty sure is will be something to do with one of these classes:- message.embeds.x.y.z or MessageEmbed.x.y.x

Google has not been my friend, I can not find one example of code that reads an "Embed message" which is odd.

Anyway, to ensure I don't look like a complete sponge I'll include the working code for the "embed sender bot". A few people seem to have problems cracking the syntax, so it maybe of use to someone else searching on here...

Thanks in advance for any help you can give.

Documentation Found: Docs for MessageEmbed And;

Embed used within message class

Code for test Embed sender bot:

  const Discord = require("discord.js");
  const client = new Discord.Client();
  const config = require("./config.json");

  /* A simple bot to throw out a test "Embed message" when asked to. */

  client.on("message", (message) => {
  if (!message.content.startsWith(config.prefix) || message.author.bot) 
  return;

   if (message.content.startsWith(config.prefix + "emb")) {
   console.log("Sending an embedd message");
   message.channel.send({embed: {
    color: 3447003,
    title: "This is an embed (Title)",
    description: "Embed! (first line)\nsecond line of Desc\nthird line of 
   Desc",
    footer: 
    {
        text: "Footnote ©"
    }
  }});
} else   if (message.content.startsWith(config.prefix + "test")) 
  {
  message.reply("Bot active");


  };

 });

  client.login(config.token);

Answer

Technoguyfication picture Technoguyfication · Oct 6, 2017

Once you have your Message object, check the embeds property to obtain an array of all MessageEmbeds contained inside it. You can then read any of the properties, such as description, fields, etc.

Here's some example code:

const client = new Discord.Client();
/* client.login, etc. etc. */

client.on('message', (msg) => {
    msg.embeds.forEach((embed) => {
       // add this embed to the database, using embed.description, embed.fields, etc.
        // if there are no embeds, this code won't run.
    });
    msg.reply("Embed sent!");
});