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

231
Views
JS For loop is just not iterating, Why?

So, I am making this discord bot and I wanted to make it so if someone's message gets deleted, It can be stored onto a .txt file, And if someone wanted, They could just type in a command and the bot will show the list of deleted messages.

So, when the command is fired, it will need a number parameter and that parameter will be the number of messages that will be shown.

Let's call that number numberOfSnipes

The code will get the .txt file and turn it into an array by splitting each line and putting it in one array.

As so,

fs.readFile('deletedMsgs/' + message.guildId + '.txt', function (err, data) {
  messagesArray = data.toString().split('\n');
});

As the code says, It will be called messagesArray

Now the problem comes here, We will need to iterate through the array and get the number of messages needed, This will be put into another array which will be called messagesToShow

So, I tried coding that and failed,

Code:

messagesToShow = []
for (let i = messagesArray.length; i < messagesArray.length - numberOfSnipes; i--) {
     console.log(i)
     messagesToShow.push(messagesArray[i])
}

FYI, console.log(i) did not log anything.

I tried to log messagesArray, It logged an empty array

Keep in mind that numberOfSnipes and messagesArray were both logged and they returned the right information.

Since, This can be more easier for you all. To solve, I will provide the whole code:

if (message.content.toLowerCase().startsWith(config.prefix + 'snipelist')) {
  numberOfSnipes = parseInt(message.content.split(' ')[1]);
  if (fs.existsSync('deletedMsgs/' + message.guildId + '.txt')) {
    fs.readFile(
      'deletedMsgs/' + message.guildId + '.txt',
      function (err, data) {
        messagesArray = data.toString().split('\n');
        console.log(messagesArray);
        if (numberOfSnipes > messagesArray.length) {
          message.reply('Unable to return messages.');
        } else {
          messagesToShow = [];
          for (
            let i = messagesArray.length;
            i < messagesArray.length - numberOfSnipes;
            i--
          ) {
            console.log(i);
            messagesToShow.push(messagesArray[i]);
          }
          console.log(messagesToShow);
          finalMessage = ' ';
          for (let letter in messagesToShow.toString()) {
            if (letter != ',') {
              finalMessage += letter;
            } else if (letter == ',') {
              finalMessage += '\n';
            }
          }
          message.reply(finalMessage);
        }
      }
    );
  }
}

Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
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!