[英]How to save richEmbed to file using FS in discord.js?
我在我的 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"});
...這是整個 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);
}
您的嵌入變量是 object 類型。 您可以通過以下方式進行檢查:
console.log(typeof embed);
在這種情況下,您必須使用JSON.Stringify將 object 轉換為 JSON 字符串,如 Mozilla 文檔中所述。
const text = JSON.stringify(embed);
然后在您的 fs function 中,只需將embed替換為text變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.