Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
Write a input from a message to a txt file with Discord.js

I'm making an discord bot were people can upload there own jokes. I'm testing an very basic version of this idea and it's kinda working like expected. Here is my code:

client.on('message', msg => {
  const fs = require('fs');

  if(msg.content === "ff"){
    let con = (msg.content);
     const content = `${con}`;
       fs.appendFile('textart.txt',content, err => {
         if (err) {
           console.error(err)
           return
         }
       })
  }
  
})

Using fs it's simple to write an input down in a txt file. But I want that when a new joke is given it makes a new line in the txt file. So like this:

1 joke one
2 joke two
3 joke three

But when I'm using this code it does this in the txt file :

1 joke one joke two joke three

How can make it so when a new joke is given the bot creates a new line and puts the joke in that new line. I found something and they said too use /n/ but I dont know how to use that in my code. Anyone haves an idea?

I was expecting the text file to have rows with inputs like described above

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use \n, and for the numbers, see how many newlines there are

const line = fs.readFileSync("textart.txt", "utf8").split("\n").length
const content = `${line} ${con}\n`

Note that for the numbers to be accurate, you need to have a linebreak from the start, so it should look like this from the start:

1 joke one

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!