如何在聊天中创建不和谐嵌入? [英] How to create discord Embed's in chat?

查看:19
本文介绍了如何在聊天中创建不和谐嵌入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下名为 say 的命令,它使用以下语法生成嵌入:!say hello, It's a test,#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"
}

推荐答案

实现这一目标的最佳方法是使用 MessageEmbed 接受对象作为数据.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆