如何将邮件保存到txt?

时间:2019-07-27 15:13:59

标签: javascript node.js discord.js

当我尝试将我和客户之间的聊天保存为txt文件时,我无法弄清楚,因为当我尝试对其进行编码时,一次只会保存1条消息,而当其他人发送消息时,它将重写txt文件示例:我发送了:您需要什么帮助?

In TXT: 
User: *me*
Message: *What do you need help with?*
Channel: *channel name*

但是当他回答时,它会将txt重写为他的消息

我想要的样子:

In TXT: 
User: *me*
Message: *What do you need help with?*
Channel: *channel name*
=======================================
User: *Customer*
Message: *Customer's reply*
Channel: *channel name*
=======================================

但我会得到:

In TXT:
User: *Customer*
Message: *Customer's reply*
Channel: *channel name*

我想保存所有发送的内容,但以txt格式保存,只会保存最新的内容。

我该如何解决问题?

1 个答案:

答案 0 :(得分:1)

我以前不知道你是怎么做到的,但是我会这样:

const fs = require("fs");

# FIRST MESSAGE
fs.writeFileSync("/1.txt", `User: ${username}\nMessage: ${messageContent}\nChannel: ${channelName}`)

# SECOND MESSAGE
let fileContent = fs.readFileSync("/1.txt").toString();
fs.writeFileSync("/1.txt", fileContent + `\n-\nUser: ${username}\nMessage: ${messageContent}\nChannel: ${channelName}`);

# and so on