【问题标题】:Read contents of an embed message from a discord server从不和谐服务器读取嵌入消息的内容
【发布时间】:2018-02-14 20:32:55
【问题描述】:

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

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

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

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

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

提前感谢您提供的任何帮助。

找到的文档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);

【问题讨论】:

    标签: javascript discord


    【解决方案1】:

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

    【讨论】:

    • 感谢 Techo。我设法偶然发现了正确的语法(比什么都幸运!)当然,在你得到第一个位之后,其余的就到位了。知道我只会收到嵌入在单个消息中的消息,因此我最终将其作为测试:-try {(message.embeds[0].description);} catch(err){return}; //work on only Embeds Your's 是一个更好的更全面和通用的答案;-) 再次感谢。
    • 确实试图给你一个赞成票,但显然对这个网站来说太新了,无法显示 ;-) 希望它注册了
    猜你喜欢
    • 2021-12-03
    • 2019-10-11
    • 1970-01-01
    • 2021-11-15
    • 2021-06-25
    • 2021-05-24
    • 2016-03-18
    • 2020-09-05
    • 2021-04-14
    相关资源
    最近更新 更多