从 Discord 服务器读取嵌入消息的内容

MIk*_*Ike 9 javascript discord

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

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

我意识到现在我应该弹出一些代码供您检查,但我什至还没有那么远!阅读文档(链接到最后)我很确定这与以下类之一有关:-消息。嵌入.xyz 或 MessageEmbed.xyx

谷歌不是我的朋友,我找不到一个读取“嵌入消息”的代码示例,这很奇怪。

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

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

找到的文档MessageEmbed 文档 和;

嵌入在消息类中使用

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

  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);
Run Code Online (Sandbox Code Playgroud)

Tec*_*ion 10

一旦你有你的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!");
});
Run Code Online (Sandbox Code Playgroud)