Noblox.js refreshCookie TypeError: Cannot read properties of null (reading '1')

Hello,

I am currently trying to make a bot for ranking, it seems to work great but then after about 20 minutes to an hour, I start to get the error ‘TypeError: Cannot read properties of null (reading ‘1’)’ I’ve been debugging this for about 9 hours now, and I still have no idea why it’s doing this.

I’ve came to the conclusion that noblox.refreshCookie is the reason.

Here is the code for my function

/* eslint-disable linebreak-style */
/* eslint-disable max-len */
/* eslint-disable linebreak-style */
const noblox = require("noblox.js");
const config = require("./config.json"); // Config

let cachedCookie = null;
let isRefreshing = false;

// eslint-disable-next-line valid-jsdoc
/**
 * Refreshes the cookie
 */
async function refreshCookie(admin) {
  try {
    if (isRefreshing) {
      console.log("Cookie refresh in progress, waiting for completion...");
      await new Promise((resolve) => setTimeout(resolve, 2000));
      return [cachedCookie, "Using cached cookie after refresh"];
    }
    isRefreshing = true;

    const db = admin.firestore();
    const docRef = db.collection("main").doc("variables");
    const doc = await docRef.get();

    if (!doc.exists) {
      console.log("No such document");
      isRefreshing = false;
      return [null, "No such document"];
    }
    const variables = doc.data();

    if (!variables || typeof variables.bot_cookie !== "string" || !variables.bot_cookie.trim()) {
      console.log("Bot cookie not found in Firestore");
      isRefreshing = false;
      return [null, "Bot cookie not found in Firestore"];
    }

    console.log("Refreshing cookie...");
    let newCookie;
    try {
      console.log("Bot cookie:", variables.bot_cookie);
      newCookie = await noblox.refreshCookie(variables.bot_cookie);
      console.log("Cookie issued at:", new Date().toISOString());
    } catch (nobloxError) {
      console.error(`noblox.js error during cookie refresh ${nobloxError}`);
      isRefreshing = false;
      return [variables.bot_cookie || cachedCookie, "Failed to refresh cookie"];
    }

    if (!newCookie || typeof newCookie !== "string" || !newCookie.trim()) {
      console.log("Failed to refresh cookie: newCookie is null or invalid");
      isRefreshing = false;
      return [null, "Could not refresh cookie"];
    }

    // Update the document in Firestore with the new cookie
    await docRef.update({bot_cookie: newCookie});

    // Cache the new cookie
    cachedCookie = newCookie;

    console.log(`New cookie saved in Firestore: ${newCookie}`);
    isRefreshing = false;
    return [newCookie, "Successfully refreshed cookie"];
  } catch (error) {
    console.error(`Error refreshing cookie: ${error}`);
    isRefreshing = false;
    return [null, `${config.internal_error} ${error}`];
  }
}

module.exports = {
  refreshCookie,
};

Whenever the error will happen, the last line ran would be console.log(“Bot cookie:”, variables.bot_cookie); before stopping and giving the error. For now, I have added the

return [variables.bot_cookie || cachedCookie, "Failed to refresh cookie"];

so hopefully that’ll atleast help with the error. I have not tested it with that line yet, but I have deployed it.

If anyone has ran into this issue before, it’ll be greatly appreciated if I can get some help resolving this issue as it’s been a pain to try and fix.

Any help is appreciated!