为了账号安全,请及时绑定邮箱和手机立即绑定

如何在discord.js中使用FS将richEmbed保存到文件中?

如何在discord.js中使用FS将richEmbed保存到文件中?

Qyouu 2021-11-25 15:29:01
我在我的不和谐机器人上创建了一个取消清除功能,它通过将最后一批清除的消息发送到通用频道来恢复它们。为了做到这一点,我需要机器人保存 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"});...这里是整个 unpurge 代码: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);    }
查看完整描述

1 回答

?
www说

TA贡献1775条经验 获得超8个赞

您的嵌入变量是一种对象类型。您可以通过执行以下操作来检查:

console.log(typeof embed);

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

const text = JSON.stringify(embed);

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


查看完整回答
反对 回复 2021-11-25
  • 1 回答
  • 0 关注
  • 160 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信