Just make all catch's throw errors instead of calling startBot() again.
We can use those Errors in a global try catch that we can use to spawn a subprocess, we can have that in a while loop.
Calling startBot is a good restart Mechanism but It fails because of stack overflow, if you want personalized logging for all the catche's then just throw personalized errors
for example,
try{functionThatCanThrowError();}catch(err){thrownewError("Error: ",{cause: err});// or "cause" in err}
constfs=require('fs/promises');classLeetcode{/** * Creates an instance of Leetcode. * @param {string} userDataFile - The path to the file containing user data. * @param {string} groupId - The ID of the user group. */constructor(userDataFile,groupId){this.userDataFile=userDataFile;this.groupId=groupId;this.LEETCODE_PREDICT_URL="https://lccn.lbao.site/api/v1";this.LEETCODE_MULTIPLE_USER_PREDICTIONS=this.LEETCODE_PREDICT_URL+"/contest-records/predicted-rating";this.userData=[];}asyncinitializeUserData(){try{constuserData=awaitfs.readFile(this.userDataFile,'utf8');constuserGroups=JSON.parse(userData);this.userData=userGroups[this.groupId]||[];}catch(error){console.error('Error initializing user data:',error);}}asyncfetchMultipleRatingPredictions(contestName){try{consturl=this.LEETCODE_MULTIPLE_USER_PREDICTIONS;constresponse=awaitfetch(url,{method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify({contest_name: contestName,users: this.userData.map(user=>({username: user,data_region: 'US'}))})});constjsonData=awaitresponse.json();constpredictions={};jsonData.forEach(jsonRow=>{if(jsonRow&&this.userData.includes(jsonRow.username)){predictions[jsonRow.username]={newRating: jsonRow.new_rating,deltaRating: jsonRow.delta_rating};}});returnpredictions;}catch(error){console.error('Error:',error);return{};}}asyncgetFormattedMessage(contest){try{if(this.userData.length===0)awaitthis.initializeUserData();constpredictions=awaitthis.fetchMultipleRatingPredictions(contest);letmessage='PREDICTIONS\n';for(const[username,prediction]ofObject.entries(predictions)){message+=`${username}: ${prediction.newRating}\n`;}returnmessage;}catch(error){console.error('Error generating message:',error);return'Error generating message';}}}// Example usage:constleetcode=newLeetcode('./data/lc-users.txt','groupId1');constcontestName="someContestName";leetcode.getFormattedMessage(contestName).then(message=>console.log(message)).catch(error=>console.error('Error:',error));module.exports=Leetcode;
implement a store to fix the waiting for message issue
quoting /** * fetch a message from your store * implement this so that messages failed to send (solves the "this message can take a while" issue) can be retried * */ getMessage: (key: proto.IMessageKey) => Promise<proto.IMessage | undefined>
generate main.js procedurally with plugins on the basis of this
Permissions:
if they want message.upsert even they need to ask for it and then we'll place them in there
ask the plugin entry point for each event they wanna subscribe to
this opens up the scope for creating a package manager for plugins and create mirrors for that (see how revanced manages plugins for refrence)
yk how npm generates a lock file? yeah, we'll need that with the id of the latest commit or something from the plugin repo but somehow individual for each plugin
i think a deadlock is occuring cus the bot tries to spam in all groups as soon as an update is available.
i think this is the reason for stale session on groups too.
maybe we need some kind of wait before trying second.
look into that.
some issues for refrence, tho not very helpful https://github.com/adiwajshing/Baileys/issues/2386
btw this is getting logged due to pino : {logger : error} level, making it silent should help make the terminal more clean and sane
before linkArr didn't exist, so updates with multiple links were impossible
now that linkArr has been added to data json interface, some parsers can be fixed.
nsutParser and iiitbhParser are good and easy.
When groupId Json object fetch fails then AlertBot throws a JSON.parse error as when groupIds is down there is a 404 page instead of that which it tries to parse and hence throws the error that html is not json
A copy of the error
^
SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at main (/data/data/com.termux/files/home/AlertBot/main.js:56:39)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v18.12.1
error Command failed with exit code 1.