从不和谐服务器读取嵌入消息的内容

Read contents of an embed message from a discord server

场景: 我正在尝试 读取 发布到服务器的嵌入消息中的各个字段,进行一些处理,并将结果记录在数据库中。

测试: 使用 testBot 发送相关消息在使用普通文本消息时一切正常,但是当使用 "embed message" 时(理论上更容易识别要处理的字段等),我无法检索数据。我完全不知道如何从消息对象访问 "embed" 。

我意识到现在我应该弹出一些代码供您检查,但我还差得远呢!阅读文档(链接到最后)我很确定这将与其中之一有关 类:- message.embeds.x.y.z 或 MessageEmbed.x.y.x

Google 不是我的朋友,我找不到一个读取 "Embed message" 的代码示例,这很奇怪。

无论如何,为了确保我看起来不像一块完整的海绵,我将包含 "embed sender bot" 的工作代码。一些人似乎在破解语法方面遇到了问题,所以它可能对在这里搜索的其他人有用...

在此先感谢您提供的任何帮助。

找到文档Docs for MessageEmbed 并且;

Embed used within message class

测试代码嵌入发件人机器人:

  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);

获得 Message 对象后,检查 embeds 属性 以获得其中包含的所有 MessageEmbeds 的数组。然后您可以读取任何属性,例如 descriptionfields

下面是一些示例代码:

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