从不一致的服务器读取嵌入消息的内容 [英] 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.
测试:
使用testBot发送相关消息时,使用普通短信时一切正常,但是当嵌入消息时使用(从理论上讲,它更容易标识要处理的字段等),我无法检索数据。我完全不知道如何从消息对象访问嵌入。
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.
我意识到,现在应该弹出一些代码供您检查,但是我还没走!阅读文档(末尾链接),我很确定这将与以下其中一个类有关:-
消息。嵌入 .xyz或 MessageEmbed.xyx
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并不是我的朋友,我找不到一个读取嵌入消息的代码示例。
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.
找到的文档:
用于MessageEmbed的文档
并且;
Documentation Found: Docs for MessageEmbed And;
嵌入嵌入式发件人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)\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);
推荐答案
一旦收到消息
对象,检查 embeds
属性以获取其中包含的所有 MessageEmbeds
的数组。然后,您可以读取任何属性,例如说明
,字段
等。
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.
下面是一些示例代码:
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!");
});
这篇关于从不一致的服务器读取嵌入消息的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!