场景: 我正在尝试读取发布到服务器的嵌入消息中的各个字段,进行一些处理,并将结果记录在数据库中。
测试: 使用 testBot 发送相关消息在使用普通文本消息时一切正常,但是当使用“嵌入消息”时(理论上更容易识别要处理的字段等),我无法检索数据。我完全不知道如何从消息对象访问“嵌入”。
我意识到现在我应该弹出一些代码供您检查,但我什至没有那么远!阅读文档(链接到最后)我很确定这将与这些类之一有关:- 消息。嵌入.x.y.z 或 MessageEmbed.x.y.x
Google 不是我的 friend ,我找不到一个读取“嵌入消息”的代码示例,这很奇怪。
无论如何,为了确保我看起来不像一 block 完整的海绵,我将包含“嵌入发件人机器人”的工作代码。一些人似乎在破解语法方面遇到了问题,所以它可能对在这里搜索的其他人有用...
在此先感谢您提供的任何帮助。
找到文档: 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
的数组。然后您可以读取任何属性,例如 description
、fields
等。
下面是一些示例代码:
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!");
});
关于javascript - 从不和谐服务器读取嵌入消息的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46073719/