如何通过消息创建嵌入? [英] How to create embeds by message?
问题描述
我有以下名为 say
的命令,该命令生成具有以下语法的嵌入内容:!say hello,这是一个测试,#000000
.
I have the following command called say
that generates an embed with the following syntax: !say hello, It's a test,#000000
.
它工作得很好,但问题是我希望创建嵌入的代码更容易,以便任何特权用户都可以创建嵌入而不知道命令的语法,并使我更容易维护所有的阵营embed可以处理而无需将其分配给变量.
It works just fine, but the problem is that I want it to be easier to create embed's so that any privileged user could create an embed without knowing the syntax of the command and make it easier to me to maintain all the camps the embed can handle without needing to assign them to variables.
const Discord = require("discord.js");
module.exports.run = async (client, msg, args) => {
let [Title,Description,Color] = args;
let embed = new Discord.MessageEmbed()
.setColor(Color)
.setTitle(Title)
.setDescription(Description)
msg.channel.send(embed);
}
module.exports.help = {
name: "embed"
}
推荐答案
The best approach to achieve that would be to use the privilege of the MessageEmbed accepting object as data.
所以我的解决方案将具有以下语法:
So my solution would have this syntax:
!say {
"title": "hello",
"description": "It's a test",
"color": 000000
}
以及 MessageEmbed 可以使用的许多其他属性.
and many other properties that MessageEmbed can take.
有用的链接: 嵌入的构建器
Useful links: Embed builder
后面的代码将处理我们的参数并将其转换为嵌入
The code behind that will handle our arguments and transform them into an embed
const rawJson = args.join(" ") || "";
let json = {};
try {
json = JSON.parse(rawJson);
} catch (err) {
message.reply(`${err}`);
}
if (!json) return;
const content = json.content || json.text || json.plainText || "";
//In case the thumbnail comes as a string {json.thumbnail = "test.com/image.png"} so we assign it into the url of the thumbnail
if (typeof json.thumbnail === "string") {
json.thumbnail = { url: json.thumbnail };
}
//Same here
if (typeof json.image === "string") {
json.image = { url: json.image };
}
message.channel.send(content, { embed: json }).catch((err) => {
message.reply(`${err}`)
});
这篇关于如何通过消息创建嵌入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!