455 commands2 envUpdated 7h agoCreated Aug 26, 2026
commands/_onAiSuccess.js
javascript · 222 lines
1/**#command2name: /onAiSuccess3answer: 4keyboard: 5parse_mode: markdown6aliases: 7allow_only_group: false8need_reply: false9is_web: 010#command**/11 12/**#command13name: /onAiSuccess14answer: 15keyboard: 16parse_mode: markdown17aliases: 18allow_only_group: false19need_reply: false20is_web: 021#command**/22 23// کامند: onAiSuccess24// کل Logic را پاک کن و فقط این را بگذار25 26var answer = "";27var raw = null;28var data = null;29var txt = "";30var uid = "";31var chatId = "";32var statusDisplay = "";33var full = "";34var part = "";35var i = 0;36var historyStr = "[]";37var history = [];38var lastPrompt = "";39 40try {41 if (typeof content !== "undefined" && content !== null && content !== "") {42 raw = content;43 } else if (typeof http_response !== "undefined" && http_response) {44 if (http_response.data !== undefined) raw = http_response.data;45 else if (http_response.body !== undefined) raw = http_response.body;46 else raw = http_response;47 } else if (typeof response !== "undefined" && response) {48 if (response.data !== undefined) raw = response.data;49 else raw = response;50 }51 52 data = raw;53 54 if (typeof raw === "string") {55 txt = String(raw);56 if (txt.length > 0 && (txt.charAt(0) === "{" || txt.charAt(0) === "[")) {57 try {58 data = JSON.parse(txt);59 } catch (e1) {60 data = raw;61 }62 }63 }64 65 try {66 if (data && data.choices && data.choices[0]) {67 if (data.choices[0].message) {68 if (data.choices[0].message.content) {69 answer = String(data.choices[0].message.content);70 } else if (data.choices[0].message.reasoning) {71 answer = String(data.choices[0].message.reasoning);72 }73 } else if (data.choices[0].text) {74 answer = String(data.choices[0].text);75 }76 }77 } catch (e2) {}78 79 if (!answer) {80 try {81 if (data && data.error && data.error.message) {82 answer = "خطای AI: " + String(data.error.message);83 }84 } catch (e3) {}85 }86 87 if (!answer && typeof raw === "string") {88 answer = String(raw);89 }90 91 if (!answer) {92 answer = "جواب خالی بود. دوباره بفرست.";93 }94 95 answer = String(answer).split("</think>").join("");96 answer = answer.split("<think>").join("");97 answer = answer.trim();98 while (answer.indexOf("【") >= 0) {99 var markA = answer.indexOf("【");100 var markB = answer.indexOf("】", markA);101 if (markB < 0) break;102 answer = answer.slice(0, markA) + answer.slice(markB + 1);103 }104 answer = answer.trim();105 106 try {107 if (typeof chat !== "undefined" && chat) {108 if (chat.chatid) chatId = chat.chatid;109 else if (chat.id) chatId = chat.id;110 }111 } catch (e4) {}112 113 try {114 if (chatId) {115 uid = await db.global.get("last_ask_uid_" + chatId);116 if (uid) uid = String(uid);117 else uid = "";118 }119 } catch (e5) {120 uid = "";121 }122 123 if (!uid) {124 try {125 if (typeof user !== "undefined" && user) {126 if (user.telegramid) uid = String(user.telegramid);127 else if (user.id) uid = String(user.id);128 }129 } catch (e6) {}130 }131 132 try {133 if (uid) {134 statusDisplay = await db.global.get("last_status_" + uid);135 if (statusDisplay) statusDisplay = String(statusDisplay);136 else statusDisplay = "";137 }138 } catch (e7) {139 statusDisplay = "";140 }141 142 if (uid) {143 try {144 await db.global.set("last_ai_answer_" + uid, answer.slice(0, 4000));145 } catch (e8) {}146 147 try {148 lastPrompt = await db.global.get("last_user_prompt_" + uid);149 if (!lastPrompt) lastPrompt = "";150 lastPrompt = String(lastPrompt);151 152 historyStr = await db.global.get("history_" + uid);153 if (!historyStr) historyStr = "[]";154 try {155 history = JSON.parse(String(historyStr));156 } catch (e9) {157 history = [];158 }159 if (!history || typeof history.length !== "number") history = [];160 161 if (lastPrompt) {162 history.push({ role: "user", content: lastPrompt.slice(0, 1500) });163 }164 history.push({ role: "assistant", content: answer.slice(0, 2500) });165 166 var histMax = 40;167 try {168 histMax = parseInt(await db.global.get("cfg_history_len") || 40) || 40;169 if (histMax < 10) histMax = 10;170 if (histMax > 60) histMax = 60;171 } catch (eHM) { histMax = 40; }172 while (history.length > histMax) {173 history.shift();174 }175 176 await db.global.set("history_" + uid, JSON.stringify(history));177 } catch (e10) {}178 }179 180 full = answer;181 if (statusDisplay) {182 full = full + "\n\n➖➖➖➖➖➖\n📊 وضعیت: " + statusDisplay;183 }184 185 i = 0;186 while (i < full.length) {187 part = full.slice(i, i + 3500);188 i = i + 3500;189 190 if (i >= full.length) {191 try {192 Api.sendMessage({193 text: part,194 reply_markup: {195 inline_keyboard: [196 [197 { text: "♻️ کوتاهتر", callback_data: "ai_shorter" },198 { text: "✍️ کاملتر", callback_data: "ai_longer" }199 ],200 [201 { text: "📅 روزانه", callback_data: "daily" },202 { text: "👥 دعوت", callback_data: "invite" },203 { text: "⚡️ XP", callback_data: "xp" }204 ]205 ]206 }207 });208 } catch (e11) {209 Bot.sendMessage(part);210 }211 } else {212 Bot.sendMessage(part);213 }214 }215 216} catch (err) {217 try {218 Bot.sendMessage("⚠️ onAiSuccess crash:\n" + String(err && err.message ? err.message : err));219 } catch (e12) {220 try { Bot.sendMessage("onAiSuccess failed"); } catch (e13) {}221 }222}