Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

234
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda