Nodejs: Twit Node API status posted without image and no error

I hope somebody can assist me. I have tried searching but can't seem to find anyone who has run into this issue.

I am busy learning NodeJS and on my path I decided to make a twitter bot to help me learn. I would be using :

  • NodeJS
  • Twit Node API
  • running on my Win 10 Desktop.

This Twitter bot would essentially post a random image from a folder of .png images with and a random status from delimited .txt file, at intervals between two given amounts of time stored in a prefs.js file.

e.g. 1.png, Hello World, minimum interval time: 400 min, maximum interval time: 600 min.

This bot runs most of the time flawlessly, however sometimes it would just post the status from the .txt file without the image and no error messages.

I originally thought it was the .png image size and reduced them all to less than 3Mb, and thought the issue was solved only to have one fail again during one of its runs.

I decided to find an instance of a status being posted without an image to test whether a specific image was causing the issue. Once I found one I removed all other images from the 'images' folder except the fail image and tested it, but it uploaded the image and posted it with the status without any issues multiple times.

I can't seem to figure out why this is happening and/or why I am not getting any error messages on failure. Hopefully someone can help me out.

Also any tips on improving code are welcome.

I have added main function below without the interval:

const fs = require("fs"),
  path = require("path"),
  Twit = require("twit"),
  prefs = require("./prefs.js");
config = require(path.join(__dirname, "config.js"));

// Tweet random image along with a random status
function tweetRandomImage() {
  //read filenames from directory into an array
  fs.readdir(__dirname + "/images", function (err, files) {
    if (err) {
      console.log("error:", err);
    } else {
      let images = [];
      files.forEach(function (f) {
        let imagePath = path.join(__dirname, "/images/" + f);
        let filesize = getFilesizeInMegabytes(imagePath);
        let file_ext = path.extname(f);

        images.push(f);
        
        });

      console.log("opening an image...");

      const imagePath = path.join(__dirname, "/images/" + randomFromArray(images)),
        b64content = fs.readFileSync(imagePath, { encoding: "base64" });

      console.log("uploading an image...");
      console.log("uploading image : " + imagePath);

      T.post(
        "media/upload",
        { media_data: b64content },
        function (err, data, response) {
          if (err) {
            console.log("error:", err);
          } else {
            let randStatus = getRandomText(";", "tweets.txt");
            console.log("\n");
            console.log("Random Status :");
            console.log(randStatus + "\n");
            console.log("\n");

            // post status
            T.post(
              "statuses/update",
              {
                status: randStatus,
                media_ids: new Array(data.media_id_string),
              },
              function (err, data, response) {
                if (err) {
                  console.log("error:", err);
                } else {
                  console.log("posted an image!\n");
                  console.log("\n");
                }
              }
            );
          }
        }
      );
    }
  });
}

tweetRandomImage();