简体   繁体   English

如何将消息保存到txt?

[英]How do I save messages to txt?

When I try to save chat between me and customer to txt file I cannot figure it out because when I try to code it in it will save only 1 message at time and when someone else sends message it will rewrite the txt file example: I sent: What do you need help with?当我尝试将我和客户之间的聊天保存到 txt 文件时,我无法弄清楚,因为当我尝试对其进行编码时,它一次只会保存 1 条消息,而当其他人发送消息时,它将重写 txt 文件示例:我发送了: 你有什么需要帮助的?

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

But when he answers it it will rewrite the txt to his message但是当他回答时,它会将 txt 重写为他的消息

How I want it to be:我希望它如何:

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

But I'll get:但我会得到:

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

I want to save all things we send but in txt it will save just the newest.我想保存我们发送的所有内容,但在 txt 中它只会保存最新的。

How do I solve the problem?我该如何解决问题?

I don't know how you did it before, but I would do it like this:我不知道你以前是怎么做的,但我会这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM