Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

javascript - Read contents of an embed message from a discord server

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)
second line of Desc
third line of 
   Desc",
    footer: 
    {
        text: "Footnote ?"
    }
  }});
} else   if (message.content.startsWith(config.prefix + "test")) 
  {
  message.reply("Bot active");


  };

 });

  client.login(config.token);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!");
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...