diff --git a/index.js b/index.js index 5f2a3a24..3d7f445f 100644 --- a/index.js +++ b/index.js @@ -206,6 +206,7 @@ process.on("SIGTERM", function () { }); process.on("SIGINT", function () { + process.kill(process.pid); process.exit(0); }); @@ -347,7 +348,7 @@ function main(fca_state, login, cb) { if (err) { handleError({ stacktrace: err, cuid: login }); - if (err && err.error && err.error == "unable to get cookie.") { + if (err) { accounts = accounts.filter((item) => item !== login); for (let threads in settingsThread) { if (settingsThread[threads].lock && settingsThread[threads].lock == login) { @@ -360,7 +361,7 @@ function main(fca_state, login, cb) { } if (typeof cb === "function") return cb(true) - if (accounts.length == 0) utils.watchCookiesChanges(); + if (accounts.length == 0) utils.logged("no_acount_found"); return; } @@ -451,7 +452,7 @@ function main(fca_state, login, cb) { redfox.eventListener(async (err, event) => { if (err) { - if (err && err.error && (err.error == "unable to get cookie." || err.error == "Connection Closed")) { + if (err) { handleError({ stacktrace: err, cuid: login, e: event }); accounts = accounts.filter((item) => item !== login); @@ -1141,7 +1142,7 @@ async function ai22(redfox, event, query) { if (quizData[q].messageID && event.messageReply.messageID == quizData[q].messageID) { let rawUserAnswer = input; let userAnswer = rawUserAnswer.replaceAll(" ", "").toLowerCase(); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { const points = Math.floor(Math.random() * 1500); if (userAnswer == quizData[q].correctAnswer1 || userAnswer == quizData[q].correctAnswer) { addBalance(name, points); @@ -1159,7 +1160,7 @@ async function ai22(redfox, event, query) { reactMessage(redfox, event, ":heart:"); emo.push(event.messageID); } - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (!name.quiz_answered_correct) { name["quiz_answered_correct"] = 1; } @@ -1176,7 +1177,7 @@ async function ai22(redfox, event, query) { reactMessage(redfox, event, ":dislike:"); emo.push(event.messageID); } - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (!name.quiz_answered_incorrect) { name["quiz_answered_incorrect"] = 1; } @@ -1376,13 +1377,13 @@ async function ai22(redfox, event, query) { let transferTo = event.messageReply.senderID; if (/^\d+$/.test(data[2])) { let amount = parseInt(data[2]); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (!name.balance && event.senderID != process.env.ROOT) { sendMessage(redfox, event, "You have 0 $ balance yet."); } else if (amount + 500 > name.balance && event.senderID != process.env.ROOT) { sendMessage(redfox, event, "You don't have enough balance!"); } else { - utils.getGroupProfile(users, transferTo, async function (name1) { + utils.getGroupProfile(groups, transferTo, async function (name1) { addBalance(name1, amount); }); removeBalance(name, amount); @@ -1397,7 +1398,7 @@ async function ai22(redfox, event, query) { } } } else if (testCommand(redfox, event, query, "add--instance", event.senderID, "user", true)) { - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (!name.balance) { sendMessage(redfox, event, "You dont have enought balance to continue!"); } else if (name.balance < 0) { @@ -1415,7 +1416,7 @@ async function ai22(redfox, event, query) { let dirp = __dirname + "/cache/add_instance_" + utils.getTimestamp() + ".jpg"; if (accounts.includes(login)) { downloadFile(getProfilePic(login), dirp).then(async (response) => { - let msg = updateFont("This already connected to the main server!", login, redfox.getCurrentUserID()); + let msg = updateFont("This instance is already connected to the main server!", login, redfox.getCurrentUserID()); let message = { body: msg, attachment: fs.createReadStream(dirp), @@ -1482,11 +1483,11 @@ async function ai22(redfox, event, query) { users.blocked = users.blocked.filter((item) => item !== login); utils.log("rem_block_user " + login); sendMessageOnly(redfox, event, "You've been unblocked!"); - utils.getGroupProfile(users, settings[login].owner, async function (name) { + utils.getGroupProfile(groups, settings[login].owner, async function (name) { removeBalance(name, 3000); }); if (event.senderID != process.env.ROOT) { - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { removeBalance(name, 1500); }); } @@ -1496,11 +1497,11 @@ async function ai22(redfox, event, query) { users.bot = users.bot.filter((item) => item !== login); utils.log("rem_block_bot " + login); sendMessageOnly(redfox, event, "You've been unblocked!"); - utils.getGroupProfile(users, settings[login].owner, async function (name) { + utils.getGroupProfile(groups, settings[login].owner, async function (name) { removeBalance(name, 6000); }); if (event.senderID != process.env.ROOT) { - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { removeBalance(name, 3000); }); } @@ -1510,7 +1511,7 @@ async function ai22(redfox, event, query) { users.admin = users.admin.filter((item) => item !== event.senderID); utils.log("rem_sender_admin " + login); sendMessage(redfox, event, "Your admin previliges has been revoke!"); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { addBalance(name, 2000); }); } @@ -1519,7 +1520,7 @@ async function ai22(redfox, event, query) { users.admin = users.admin.filter((item) => item !== login); utils.log("rem_login_adminn " + login); sendMessageOnly(redfox, event, "Your admin previliges has been revoke!"); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { addBalance(name, 2000); }); } @@ -1833,7 +1834,7 @@ async function ai(redfox, event) { if (data.length < 2 || (findPr != false && input == findPr)) { let welCC = hey[Math.floor(Math.random() * hey.length)]; if (welCC.startsWith("How ")) { - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { let aa = ""; if (name.firstName != undefined) { aa += "Hello " + name.firstName + ". "; @@ -1854,7 +1855,7 @@ async function ai(redfox, event) { text = text.replace(findPr, ""); } - utils.getGroupProfile(users, event.senderID, async function (user) { + utils.getGroupProfile(groups, event.senderID, async function (user) { if (event.isGroup) { utils.getGroupProfile(groups, event.threadID, async function (group) { let respo = await aiResponse2(event, text, true, user, group, redfox.getCurrentUserID()); @@ -2298,7 +2299,7 @@ async function ai(redfox, event) { if (users.admin.length == 0) return sendMessage(redfox, event, "Orion has no admin set yet!"); let construct = "⋆。° ^@^C^A>^D^A^@^P^C^AL\n"; for (let admin in users.admin) { - utils.getGroupProfile(users, users.admin[admin], async function (name) { + utils.getGroupProfile(groups, users.admin[admin], async function (name) { construct += "│\n"; construct += "│ ⦿ Name: " + name.name + "\n"; construct += "│ ⦿ uid: " + users.admin[admin] + "\n"; @@ -2318,7 +2319,7 @@ async function ai(redfox, event) { } if (owners.length == 0) return sendMessage(redfox, event, "Orion has no owners yet!"); for (let owner in owners) { - utils.getGroupProfile(users, owners[owner], async function (name) { + utils.getGroupProfile(groups, owners[owner], async function (name) { construct += "│\n"; if (name.name) { construct += "│ ⦿ Name: " + name.name + "\n"; @@ -2332,7 +2333,7 @@ async function ai(redfox, event) { if (isGoingToFast(redfox, event)) return; let construct = "⋆。° ^@^C^A>^D^A^@^P^C^AL\n"; for (let account in accounts) { - utils.getGroupProfile(users, accounts[account], async function (name) { + utils.getGroupProfile(groups, accounts[account], async function (name) { construct += "│\n"; if (name.name != undefined) { construct += "│ ⦿ Name: " + name.name + "\n│ ⦿ uid: " + accounts[account]; @@ -3912,7 +3913,7 @@ async function ai(redfox, event) { sendMessage(redfox, event, "Houston! Unknown or missing option.\n\n Usage: sendReport text" + "\n " + example[Math.floor(Math.random() * example.length)] + " sendReport There is a problem in ______ that cause ______."); } else { data.shift(); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { let nR = "⋆。° ^@^C^A>^D^A^@^P^C^AL\n│\n"; if (name.name != undefined) { nR += "│ name: " + name.name + "\n"; @@ -4195,7 +4196,7 @@ async function ai(redfox, event) { } } else if (testCommand(redfox, event, query, "top", event.senderID, "user", true)) { if (isGoingToFast(redfox, event)) return; - utils.getGroupProfile(users, event.senderID, async function (user) { + utils.getGroupProfile(groups, event.senderID, async function (user) { if (!user.balance && event.senderID != process.env.ROOT) { sendMessage(redfox, event, "You have 0 $ balance yet."); } else if (1000 > user.balance && event.senderID != process.env.ROOT) { @@ -4212,12 +4213,20 @@ async function ai(redfox, event) { if (gc.isGroup) { let lead = []; let participantIDs = gc.participantIDs; + + if (users.list.length == 0) return sendMessage(redfox, event, "No users yet!"); + for (let i in users.list) { - let cuid = users.list[i].id; - if (users.list[i].balance && participantIDs.includes(cuid) && cuid != process.env.ROOT) { - lead.push({ id: users.list[i].id, name: users.list[i].firstName, balance: users.list[i].balance }); + if (users.list[i]) { + let cuid = users.list[i].id; + if (users.list[i].balance && participantIDs.includes(cuid) && cuid != process.env.ROOT) { + lead.push({ id: users.list[i].id, name: users.list[i].firstName, balance: users.list[i].balance }); + } } } + + if (lead.length == 0) return sendMessage(redfox, event, "No leading users yet!"); + lead.sort((a, b) => parseFloat(b.balance) - parseFloat(a.balance)); let construct = []; let totalL = lead.length; @@ -4241,8 +4250,11 @@ async function ai(redfox, event) { }); } else if (testCommand(redfox, event, query, "top--quiz", event.senderID, "user", true)) { if (isGoingToFast(redfox, event)) return; - utils.getGroupProfile(users, event.senderID, async function (user) { + utils.getGroupProfile(groups, event.senderID, async function (user) { let lead = []; + + if (users.list.length == 0) return sendMessage(redfox, event, "No users yet!"); + for (let i in users.list) { let correct = 1; let incorrect = 1; @@ -4253,13 +4265,19 @@ async function ai(redfox, event) { incorrect = users.list[i].quiz_answered_incorrect; } - let quiz = { - name: users.list[i].firstName, - balance: users.list[i].balance, - score: formatDecNum((correct / incorrect) * 0.1), - }; - lead.push(quiz); + if (users.list[i].quiz_answered_correct && users.list[i].quiz_answered_incorrect) { + + let quiz = { + name: users.list[i].firstName, + balance: users.list[i].balance, + score: formatDecNum((correct / incorrect) * 0.1), + }; + lead.push(quiz); + } } + + if (lead.length == 0) return sendMessage(redfox, event, "No users who answers quiz yet!"); + lead.sort((a, b) => parseFloat(b.score) - parseFloat(a.score)); let construct = []; @@ -4270,7 +4288,7 @@ async function ai(redfox, event) { }); } else if (testCommand(redfox, event, query, "top--global", event.senderID, "user", true)) { if (isGoingToFast(redfox, event)) return; - utils.getGroupProfile(users, event.senderID, async function (user) { + utils.getGroupProfile(groups, event.senderID, async function (user) { if (!user.balance && event.senderID != process.env.ROOT) { sendMessage(redfox, event, "You have 0 $ balance yet."); } else if (1000 > user.balance && event.senderID != process.env.ROOT) { @@ -4279,12 +4297,20 @@ async function ai(redfox, event) { if (event.senderID != process.env.ROOT) { removeBalance(user, 1000); } + + if (users.list.length == 0) return sendMessage(redfox, event, "No users yet!"); + let lead = []; for (let i in users.list) { - if (users.list[i].balance && users.list[i].id != process.env.ROOT) { - lead.push({ id: users.list[i].id, name: users.list[i].firstName, balance: users.list[i].balance }); + if (users.list[i]) { + if (users.list[i].balance && users.list[i].id != process.env.ROOT) { + lead.push({ id: users.list[i].id, name: users.list[i].firstName, balance: users.list[i].balance }); + } } } + + if (lead.length == 0) return sendMessage(redfox, event, "No leading users yet!"); + lead.sort((a, b) => parseFloat(b.balance) - parseFloat(a.balance)); let construct = []; @@ -4301,7 +4327,7 @@ async function ai(redfox, event) { }); } else if (testCommand(redfox, event, query, "balance", event.senderID, "user", true)) { if (isGoingToFast(redfox, event)) return; - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (!name.name) { return sendMessage(redfox, event, "User not found!"); } @@ -4332,11 +4358,11 @@ async function ai(redfox, event) { } } } - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (id != process.env.ROOT) { removeBalance(name, 1000); } - utils.getGroupProfile(users, id, async function (name) { + utils.getGroupProfile(groups, id, async function (name) { if (!name.name) { return sendMessage(redfox, event, "User not found!"); } @@ -4594,7 +4620,7 @@ async function ai(redfox, event) { } else if (testCommand(redfox, event, query, "threadLock", event.senderID, "user")) { let data = input.split(" "); if (data.length < 3 && event.type != "message_reply") { - sendMessage(redfox, event, "Houston! Unknown or missing option.\n\n Usage: threadLock @mention" + "\n " + example[Math.floor(Math.random() * example.length)] + " setThreadLock @Zero Two"); + sendMessage(redfox, event, "Houston! Unknown or missing option.\n\n Usage: threadLock @mention" + "\n " + example[Math.floor(Math.random() * example.length)] + " threadLock @Zero Two"); } else { let id = Object.keys(event.mentions)[0]; if (!id) { @@ -4608,7 +4634,7 @@ async function ai(redfox, event) { } else if (/^[0-9]+$/.test(user)) { id = user; } else { - return sendMessage(redfox, event, "Houston! Unknown or missing option.\n\n Usage: threadLock @mention" + "\n " + example[Math.floor(Math.random() * example.length)] + " setThreadLock @Zero Two"); + return sendMessage(redfox, event, "Houston! Unknown or missing option.\n\n Usage: threadLock @mention" + "\n " + example[Math.floor(Math.random() * example.length)] + " threadLock @Zero Two"); } } } @@ -4668,14 +4694,14 @@ async function ai(redfox, event) { } else { users.bot.push(id); sendMessage(redfox, event, "Noted."); - utils.getGroupProfile(users, event.senderID, async function (user) { + utils.getGroupProfile(groups, event.senderID, async function (user) { addBalance(user, 4000); }); } } /* } else if (testCommand(redfox, event, query, "zzzzzzz", event.senderID, "root", true)) { - utils.getGroupProfile(users, event.senderID, async function (user) { + utils.getGroupProfile(groups, event.senderID, async function (user) { if (!user.balance) { user["balance"] = 99999999999999; } @@ -4705,7 +4731,7 @@ async function ai(redfox, event) { } } } - utils.getGroupProfile(users, id, async function (user) { + utils.getGroupProfile(groups, id, async function (user) { if (!user.name) { return sendMessage(redfox, event, "User not found!"); } @@ -4919,7 +4945,7 @@ async function ai(redfox, event) { return sendMessage(redfox, event, "Houston! Unknown or missing option.\n\n Usage: add --token @mention" + "\n " + example[Math.floor(Math.random() * example.length)] + " addtoken @Zero Two"); } } - utils.getGroupProfile(users, id, async function (user) { + utils.getGroupProfile(groups, id, async function (user) { addBalance(user, 1500); sendMessage(redfox, event, "Added 1500 tokens to the account holder."); }); @@ -5614,7 +5640,7 @@ async function ai(redfox, event) { } } else if (testCommand(redfox, event, query, "hanime", event.senderID)) { if (isGoingToFast(redfox, event)) return; - utils.getGroupProfile(users, event.senderID, async function (user) { + utils.getGroupProfile(groups, event.senderID, async function (user) { if (!user.balance && event.senderID != process.env.ROOT) { sendMessage(redfox, event, "You have 0 $ balance yet."); } else if (1000 > user.balance && event.senderID != process.env.ROOT) { @@ -5839,7 +5865,7 @@ async function ai(redfox, event) { sendMessage(redfox, event, "Houston! Unknown or missing option.\n\n Usage: time --timezone tmz" + "\n " + example[Math.floor(Math.random() * example.length)] + " time --timezone Asia/Manila"); } } else if (testCommand(redfox, event, query, "time", event.senderID, "user", true)) { - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (name.firstName != undefined && name.timezone) { return sendMessage(redfox, event, "It's " + getCurrentDateAndTime(name.timezone)); } @@ -5946,7 +5972,7 @@ async function ai(redfox, event) { if (/^\d+$/.test(data[0])) { let points = parseInt(data[0]); if (/^(head(s|)|tail(s|))$/.test(data[1])) { - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (!name.balance && event.senderID != process.env.ROOT) { sendMessage(redfox, event, "You have 0 $ balance yet."); } else if (points >= name.balance && event.senderID != process.env.ROOT) { @@ -5970,7 +5996,7 @@ async function ai(redfox, event) { sendMessage(redfox, event, "Houston! Unknown or missing option.\n\n Usage: coinflip token type" + "\n " + example[Math.floor(Math.random() * example.length)] + " coinflip 100 heads"); } } else if (/^(head(s|)|tail(s|))$/.test(data[0])) { - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if ((picker == 1 && /^head(s|)$/.test(data[0])) || (picker == 0 && /^tail(s|)$/.test(data[0]))) { addBalance(name, 500); sendMessage(redfox, event, "You win!"); @@ -6041,7 +6067,7 @@ async function ai(redfox, event) { } else { data.shift(); let body = data.join(" "); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (name.firstName != undefined) { if (utils.isValidDateFormat(body)) { name["birthday"] = body; @@ -6060,7 +6086,7 @@ async function ai(redfox, event) { } else { data.shift(); let body = data.join(" "); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (name.firstName != undefined) { if (isValidTimeZone(body)) { name["timezone"] = body; @@ -6079,7 +6105,7 @@ async function ai(redfox, event) { } else { data.shift(); let body = data.join(" "); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (name.firstName != undefined) { if (body.length > 10) { name["location"] = body; @@ -6098,7 +6124,7 @@ async function ai(redfox, event) { } else { data.shift(); let body = data.join(" "); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (name.firstName != undefined) { name["bio"] = body; sendMessage(redfox, event, "Hello " + name.firstName + " you have successfully set your bio."); @@ -6113,7 +6139,7 @@ async function ai(redfox, event) { } else { data.shift(); let body = data.join(" "); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (name.firstName != undefined) { if (body.startsWith("@")) { body = body.slice(1); @@ -6131,7 +6157,7 @@ async function ai(redfox, event) { } else { data.shift(); let body = data.join(" ").toLowerCase(); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (name.firstName != undefined) { if (body == "male" || body == "female") { name["gender"] = getGenderCode(body); @@ -6209,7 +6235,7 @@ function reaction(redfox, event, query, input) { function someR(redfox, event, query) { if (query.startsWith("goodeve") || query.startsWith("evening")) { reactMessage(redfox, event, ":love:"); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { let construct = "Good evening"; if (name.firstName != undefined) { construct += " " + name.firstName; @@ -6223,7 +6249,7 @@ function someR(redfox, event, query) { return true; } else if (query.startsWith("goodmorn") || query.startsWith("morning")) { reactMessage(redfox, event, ":love:"); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { let construct = "Good morning"; if (name.firstName != undefined) { construct += " " + name.firstName; @@ -6237,7 +6263,7 @@ function someR(redfox, event, query) { return true; } else if (query.startsWith("goodnight") || query.startsWith("night") || query == "konbanwa") { reactMessage(redfox, event, ":love:"); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { let construct = "Good night"; if (name.firstName != undefined) { construct += " " + name.firstName; @@ -6251,7 +6277,7 @@ function someR(redfox, event, query) { return true; } else if (query.startsWith("goodafter") || query.startsWith("afternoon")) { reactMessage(redfox, event, ":love:"); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { let construct = "Good afternon"; if (name.firstName != undefined) { construct += " " + name.firstName; @@ -6298,7 +6324,7 @@ async function sendMessage(redfox, event, message, thread_id, message_id, bn, vo await sleep(2000); } if (!groups.list.find((thread) => event.threadID === thread.id) && event.senderID != redfox.getCurrentUserID()) { - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (!userPresence[redfox.getCurrentUserID()]) { userPresence[redfox.getCurrentUserID()] = []; } @@ -6393,7 +6419,7 @@ async function sendMessageOnly(redfox, event, message, thread_id, message_id, bn await sleep(2000); } if (!groups.list.find((thread) => event.threadID === thread.id) && event.senderID != redfox.getCurrentUserID()) { - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { if (!userPresence[redfox.getCurrentUserID()]) { userPresence[redfox.getCurrentUserID()] = []; } @@ -6426,7 +6452,7 @@ function getMessageFromObj(message) { } async function sendMMMS(redfox, event, message, thread_id, message_id, id, voiceE, no_font, sendMessageOnly) { - utils.getGroupProfile(users, id, async function (user) { + utils.getGroupProfile(groups, id, async function (user) { let splitNewLines = getMessageFromObj(message).split("\n"); splitNewLines.shift(); splitNewLines.pop(); @@ -7461,7 +7487,7 @@ async function blockUser(redfox, event, id) { users.blocked.push(id); if (event.isGroup) { - utils.getGroupProfile(users, id, async function (name) { + utils.getGroupProfile(groups, id, async function (name) { let aa = ""; if (name.firstName != undefined) { aa += name.firstName; @@ -7546,22 +7572,22 @@ async function unblockUser(redfox, event, id) { if (users.blocked.includes(id)) { users.blocked = users.blocked.filter((item) => item !== id); sendMessage(redfox, event, "Done captain!"); - utils.getGroupProfile(users, id, async function (name) { + utils.getGroupProfile(groups, id, async function (name) { removeBalance(name, 1500); }); if (event.senderID != process.env.ROOT) { - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { removeBalance(name, 500); }); } } else { users.bot = users.bot.filter((item) => item !== id); sendMessage(redfox, event, "If you see this messages, means it worked!"); - utils.getGroupProfile(users, id, async function (name) { + utils.getGroupProfile(groups, id, async function (name) { removeBalance(name, 2000); }); if (event.senderID != process.env.ROOT) { - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { removeBalance(name, 8000); }); } @@ -7589,7 +7615,7 @@ async function addAdmin(redfox, event, id) { } if (users.blocked.includes(id) || users.bot.includes(id)) { if (event.isGroup) { - utils.getGroupProfile(users, id, async function (name) { + utils.getGroupProfile(groups, id, async function (name) { let aa = "Sorry "; if (name.firstName != undefined) { aa += name.firstName; @@ -7606,7 +7632,7 @@ async function addAdmin(redfox, event, id) { } if (settings[login].owner == id) { if (event.isGroup) { - utils.getGroupProfile(users, id, async function (name) { + utils.getGroupProfile(groups, id, async function (name) { let aa = "Sorry "; if (name.firstName != undefined) { aa += name.firstName; @@ -7623,7 +7649,7 @@ async function addAdmin(redfox, event, id) { } if (users.admin.includes(id)) { if (event.isGroup) { - utils.getGroupProfile(users, id, async function (name) { + utils.getGroupProfile(groups, id, async function (name) { let aa = ""; if (name.firstName != undefined) { aa += name.firstName; @@ -7640,7 +7666,7 @@ async function addAdmin(redfox, event, id) { } users.admin.push(id); if (event.isGroup) { - utils.getGroupProfile(users, id, async function (name) { + utils.getGroupProfile(groups, id, async function (name) { let aa = ""; if (name.firstName != undefined) { aa += name.firstName; @@ -8303,7 +8329,7 @@ async function sendMessageToAll(redfox, event) { redfox.getThreadList(50, null, ["INBOX"], async (err, list) => { if (err) return sendMessage(redfox, event, handleError({ stacktrace: err, cuid: redfox.getCurrentUserID(), e: event })); sendMessage(redfox, event, "Message has been scheduled to be send to 50 recents threads."); - utils.getGroupProfile(users, event.senderID, async function (name) { + utils.getGroupProfile(groups, event.senderID, async function (name) { let count = 1; let ctid = event.threadID; for (let tid in list) { @@ -9291,7 +9317,7 @@ function handleError(err) { } function updateUserData(user, uid) { - utils.getGroupProfile(users, uid, async function (name) { + utils.getGroupProfile(groups, uid, async function (name) { if (name) { name["name"] = user[uid].name; diff --git a/src/redfox/index.js b/src/redfox/index.js index 66769037..2ff15869 100644 --- a/src/redfox/index.js +++ b/src/redfox/index.js @@ -14,6 +14,7 @@ const utils = require("./utils"); const log = require("../log"); const cheerio = require("cheerio"); const fs = require("fs"); +const path = require("path"); var checkVerified = null; @@ -143,7 +144,7 @@ function buildAPI(globalOptions, html, jar) { syncToken: undefined, mqttEndpoint, region, - firstListen: true, + firstListen: true }; var api = { @@ -157,55 +158,14 @@ function buildAPI(globalOptions, html, jar) { api["htmlData"] = noMqttData; } - const apiFuncNames = [ - "addUserToGroup", - "setAdminStatus", - "setArchivedStatus", - "setBio", - "setBlockedStatus", - "setGroupImage", - "setNickname", - "setThreadColor", - "setThreadEmoji", - "createNewGroup", - "createPoll", - "deleteMessage", - "deleteThread", - "forwardAttachment", - "getCurrentUserID", - "getEmojiUrl", - "getFriendsList", - "getThreadHistory", - "getThreadInfo", - "getThreadList", - "getThreadPictures", - "getUserID", - "getUserInfo", - "handleMessageRequest", - "eventListener", - "logout", - "markAsDelivered", - "markAsRead", - "markAsReadAll", - "markAsSeen", - "muteThread", - "removeUserFromGroup", - "resolvePhotoUrl", - "searchForThread", - "sendMessage", - "sendTypingIndicator", - "setMessageReaction", - "setTitle", - "threadColors", - "unsendMessage", - "httpGet", - "httpPost", - ]; - var defaultFuncs = utils.makeDefaults(html, userID, ctx); - apiFuncNames.map(function (v) { - api[v] = require("./src/" + v)(defaultFuncs, api, ctx); + fs.readdirSync(path.join(__dirname, 'src')).forEach((fileName) => { + if (fileName.endsWith('.js')) { + const funcName = fileName.slice(0, -3); + const filePath = path.join(__dirname, 'src', fileName); + api[funcName] = require(filePath)(defaultFuncs, api, ctx); + } }); return [ctx, defaultFuncs, api]; diff --git a/src/redfox/src/eventListener.js b/src/redfox/src/eventListener.js index 9ec736cc..9549630e 100644 --- a/src/redfox/src/eventListener.js +++ b/src/redfox/src/eventListener.js @@ -145,22 +145,6 @@ function eventListener(defaultFuncs, api, ctx, globalCallback) { var mqttClient = ctx.mqttClient; - mqttClient.on("error", function (err) { - utils.logged("fca_listen_mqtt " + err); - mqttClient.end(); - if (ctx.globalOptions.autoReconnect) { - getSeqID(); - } else { - globalCallback( - { - type: "stop_listen", - error: "Connection refused: Server unavailable", - }, - null - ); - } - }); - mqttClient.on("connect", function () { topics.forEach(function (topicsub) { mqttClient.subscribe(topicsub); @@ -260,11 +244,19 @@ function eventListener(defaultFuncs, api, ctx, globalCallback) { } }); - mqttClient.on("close", function () { + mqttClient.on("error", (err) => disconnect(mqttClient, err)); + mqttClient.on("close", () => disconnect(mqttClient)); +} + +function disconnect(mqttClient, err) { + mqttClient.end(); + if (err) { + utils.logged("fca_listen_mqtt " + err); + getSeqID(); + } else { utils.logged("fca_mqtt_client connection closed restarting now..."); - mqttClient.end(); - process.exit(0); - }); + } + process.exit(0); } function parseDelta(defaultFuncs, api, ctx, globalCallback, v) { diff --git a/src/utils.js b/src/utils.js index 3cc059c2..faca73bd 100644 --- a/src/utils.js +++ b/src/utils.js @@ -15,35 +15,17 @@ * */ -let func = {}; +const fs = require("fs"); +const path = require("path"); -const fileNames = [ - 'checkCommandPermission', - 'cleanDuplicate', - 'decrypt', - 'encrypt', - 'formatGen', - 'formatOutput', - 'generateCommandList', - 'getContentType', - 'getCPULoad', - 'getGroupProfile', - 'getProjectTotalSize', - 'getTimestamp', - 'getUserProfile', - 'generatePoster', - 'downloadFile', - 'isBlockedSentence', - 'isNumeric', - 'log', - 'removeMarkdown', - 'shuffle', - 'checkUpdate', - 'watchCookiesChanges' -] +let func = {}; -fileNames.map(function (v) { - func[v] = require("./" + v); +fs.readdirSync(__dirname).forEach((fileName) => { + if (fileName.endsWith('.js') && fileName != "utils.js") { + const funcName = fileName.slice(0, -3); + const filePath = path.join(__dirname, fileName); + func[funcName] = require(filePath); + } }); // uncomment if you want to add hidden command to utils diff --git a/src/watchCookiesChanges.js b/src/watchCookiesChanges.js deleted file mode 100644 index 718496ff..00000000 --- a/src/watchCookiesChanges.js +++ /dev/null @@ -1,35 +0,0 @@ -/* - * - * This file is part of Project Orion. - * Copyright (c) 2022 Melvin Jones - * - * Orion is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 3 of the License - * - * Orion is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; - * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with Orion. If not, see . - * - */ - -const chokidar = require('chokidar') -const log = require("./log"); - -module.exports = () => { - const watcher = chokidar.watch('./data/cookies'); - log("watching_cookie please add your app state to"); - log("watching_cookie /data/cookies/appstate.bin"); - watcher - .on('add', (path) => { - if (path.endsWith(".bin")) { - return process.exit(0); - } - log("invalid_file_format make sure it ends in .bin"); - }) - .on('error', (error) => { - log(error); - }) -} \ No newline at end of file