【问题标题】:How to create discord Embed's in chat?如何在聊天中创建不和谐嵌入?
【发布时间】:2021-07-28 23:13:28
【问题描述】:

我有以下名为 say 的命令,它使用以下语法生成嵌入:!say hello, It's a test,#000000

它工作得很好,但问题是我希望它更容易创建嵌入,以便任何特权用户都可以在不知道命令语法的情况下创建嵌入,并使我更容易维护所有营地embed 可以处理而不需要将它们分配给变量。

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"
}

【问题讨论】:

    标签: discord.js


    【解决方案1】:

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

    所以我的解决方案将具有以下语法:

    !say {
      "title": "hello",
      "description": "It's a test",
      "color": 000000
    }
    

    以及MessageEmbed 可以采用的许多其他属性。

    有用的链接: Embed builder

    其背后的代码将处理我们的参数并将它们转换为嵌入

    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}`)
    });
    
    

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 2021-12-12
      • 2020-12-05
      • 2022-10-23
      • 2020-08-20
      • 2021-05-10
      • 2022-01-05
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多