首页 > 解决方案 > 从嵌入对象 DiscordJS 中删除 1 个元素

问题描述

我一直在尝试编写一个将 1 个嵌入从 1 个频道复制到另一个频道的机器人。

但是在将其发布到另一个频道之前,我希望它从嵌入对象中删除 1 个元素。

我目前如何拥有它:

client.on("message", (message) => {
    if (message.channel.id == channel1) {
        const embed = message.embeds[0];
    
        var params = {
            embeds: [embed],
        };
    
        fetch("WEBHOOK URL", {
            //send channel 2
            method: "POST",
            headers: {
                "Content-type": "application/json",
            },
            body: JSON.stringify(params),
        }).then((res) => {});
    }
});

如您所见,我直接使用 message.embeds[0]。

所以我什至不知道是否可以从中删除 1 个元素,例如页脚文本。

如果有人知道它是否可以完成,请说出来。

感谢您的阅读。

编辑:

会不会是这样的:

const embed = message.embeds[0];

embed.footer.text = [];
// or
embed.footer[0].text

标签: javascriptdiscorddiscord.js

解决方案


您可以使用delete运算符从对象中删除属性。

const embed = message.embeds[0];
if (!embed) return;

delete embed.footer;

message.channel.send({
    content: "Footer removed!",
    embeds: [embed]
});

请注意,您不能只删除text属性并保留图标。text如果没有属性,页脚将不会显示。

在此处输入图像描述

这些也有效:

// Remove only text from footer (footer won't be visible including icon)
embed.footer.text = "";
// Or
embed.footer = {};
// Or
embed.footer = null;
// Or ...

推荐阅读