Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda