0

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.

Kartikey
  • 4,516
  • 4
  • 15
  • 40
Baselistic
  • 57
  • 1
  • 1
  • 8
  • What are the values of `messagesArray` and `numberOfSnipes` before the loop starts? Also, you should include a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – t56k Sep 23 '21 at 21:47
  • 2
    Can you share a [mre] of your script here? The answer here is highly dependent on how you've architected your code as a whole - as of right now, it seems that you're trying to use `messagesArray` before the asynchronous `readFile` sets its value. – esqew Sep 23 '21 at 21:47
  • Your initial values terminate the loop before it starts (unless `numberOfSnipes` is a negative value?) – pilchard Sep 23 '21 at 21:47
  • `fs.readFile` is asynchronous. There's nothing in the array when the loop runs. – Jared Smith Sep 23 '21 at 21:48
  • 1
    your for loop states: 1. let `i` equal the length of an array, 2. if `i` is less than messagesArray legth then execute the code. Therefore, your loop will never execute – async await Sep 23 '21 at 21:49
  • You set your `i` to your array length therefore `i < messagesArray.length - numberOfSnipes` is already false since `i` is going to be greater than whatever `length - numberOfSnipes` is, probably meant to use the greater than sign, eg `i > messagesArray.length - numberOfSnipes` since you are iterating backwards – Patrick Evans Sep 23 '21 at 21:51
  • 1
    @PatrickEvans Thank you! It worked! (You can post this as an answer so I can mark it as a solution.) – Baselistic Sep 23 '21 at 21:55

0 Answers0