【问题标题】:How to save richEmbed to file using FS in discord.js?如何在 discord.js 中使用 FS 将richEmbed 保存到文件中?
【发布时间】:2020-02-15 02:56:52
【问题描述】:

我在我的 discord 机器人上创建了一个取消清除功能,它只需将最后一批清除的消息发送到通用频道即可恢复它们。为此,我需要机器人保存richEmbed(其中包含所有已清除的消息),并将其保存在一个文本文件中,然后我将使用 simple-crypto.js 对其进行加密以确保安全。 当我尝试使用 fs 将 RichEmbed 保存到文本文件时出现问题,其中 FS 不会将 RichEmbed 保存为 UTF-8 文本,而是仅保存“[object Object]”,并且还会出现错误,

DeprecationWarning: Calling an asynchronous function without callback is deprecated.

以下是那部分代码:

var fs = require("fs");
                fs.writeFileSync("./unpurgeData.txt", embed ,{"encoding": "utf-8"});

...这是整个清除代码:

if (cmd.startsWith("unpurge")) {
        let x = 10, // x should be form 0 to 25
        embed = new Discord.RichEmbed().setTitle('Fecthed messages');

        msg.channel.fetchMessages({ limit: x }).then(messages => {
        let arr = messages.array(); // you get the array of messages

        for (let i = 0; i < arr.length; i++) { // you loop through them
            let curr = arr[i],
            str = curr.content.trim();
            if (str.length > 2048) str = str.substring(0, 2045) + '...';
            // if the content is over the limit, you cut it

            embed.addField(curr.author, str); // then you add it to the embed
            if (i == arr.length - 1) {
                msg.channel.send(embed);
                var fs = require("fs");
                fs.writeFileSync("./unpurgeData.txt", embed ,{"encoding": "utf-8"});
            }

        }

        }).catch(console.error);
    }

【问题讨论】:

    标签: javascript node.js embed discord discord.js


    【解决方案1】:

    您的 embed 变量是一个对象类型。您可以通过以下方式进行检查:

    console.log(typeof embed);
    

    在这种情况下,您必须使用 JSON.Stringify 将对象转换为 Mozilla 文档中所述的 JSON 字符串。

    const text = JSON.stringify(embed);
    

    然后在您的 fs 函数中,只需将 embed 替换为 text 变量即可。

    【讨论】:

      猜你喜欢
      • 2018-10-18
      • 2020-05-18
      • 2020-09-23
      • 1970-01-01
      • 2021-12-24
      • 2020-07-24
      • 1970-01-01
      • 2022-01-15
      • 2021-11-08
      相关资源
      最近更新 更多