The following changes were made by the replace_file_content tool to: /root/aadharbot/index.js. If relevant, proactively run terminal commands to execute this code for the USER. Don't ask for permission.
[diff_block_start]
@@ -2274,8 +2274,8 @@
}
});
-// Owner command to manually give 120 free credits to all users (and notify them)
-bot.command('givefree', async (ctx) => {
+// Owner command to broadcast 1 daily free search to all users
+bot.command(['givefree', 'broadcast_daily_free'], async (ctx) => {
const userId = ctx.from.id;
const isOwner = await isUserAdmin(userId);
if (!isOwner) {
@@ -2282,92 +2282,23 @@
}
- const waitMsg = await ctx.reply("⏳ Processing manual free credit distribution... Please wait.", { parse_mode: 'HTML' });
-
- let targets = [];
- try {
- if (!process.env.DATABASE_URL) {
- const fs = require('fs');
- if (fs.existsSync('db.json')) {
- let db = JSON.parse(fs.readFileSync('db.json', 'utf8'));
- let updated = false;
- for (const uId in db) {
- if (db[uId] && (!db[uId].plan_type || db[uId].plan_type === 'credits') && (db[uId].credits || 0) < 120) {
- db[uId].credits = 120;
- targets.push(uId);
- updated = true;
- }
- }
- if (updated) {
- fs.writeFileSync('db.json', JSON.stringify(db, null, 2));
- }
- }
- } else {
- // Get all user IDs whose credits < 120 and plan_type != 'unlimited'
- const res = await pool.query(
- `UPDATE users
- SET credits = 120
- WHERE (plan_type IS NULL OR plan_type = 'credits')
- AND credits < 120
- RETURNING telegram_id`
- );
- targets = res.rows.map(r => String(r.telegram_id));
- }
- } catch (err) {
- console.error("[MANUAL FREE CREDIT ERROR] Failed to distribute:", err.message);
- return ctx.reply(`❌ **Failed to distribute credits:** ${err.message}`, { parse_mode: 'Markdown' });
- }
-
- if (targets.length === 0) {
- return ctx.reply("ℹ️ **No users needed credit reset (all active users already have 120 or more credits).**", { parse_mode: 'Markdown' });
- }
-
- await ctx.reply(`✅ **Successfully updated credits to 1 for ${targets.length} users!** Starting notification broadcast...`, { parse_mode: 'Markdown' });
-
- const stats = { success: 0, failed: 0, blocked: 0, deactivated: 0 };
- let index = 0;
-
- const freeCreditMsg = `🎁 1 Free Credit Received / 1 फ्री क्रेडिट प्राप्त हुआ ${CREDIT_EMOJI} 🎁\n` +
- `━━━━━━━━━━━━━━━━━━━━━━━━━━\n` +
- `Admin dwara aapke account me 1 Free Credit ${CREDIT_EMOJI} credit kar diya gaya hai! Abhi search start karein.\n\n` +
- `(1 Free Credit has been credited to your account by the Admin! Start searching now.)\n` +
- `━━━━━━━━━━━━━━━━━━━━━━━━━━\n` +
- ``;
-
- async function sendNext() {
- if (index >= targets.length) {
- const summary = `📊 MANUAL FREE CREDIT BROADCAST REPORT 📊\n` +
- `━━━━━━━━━━━━━━━━━━━━━━━━━━\n` +
- `👤 Total Notified: ${targets.length}\n` +
- `✅ Success: ${stats.success}\n` +
- `❌ Failed: ${stats.failed}\n` +
- `🚫 Blocked (Bot Blocked): ${stats.blocked}\n` +
- `🗑 Deactivated (Deleted Acc): ${stats.deactivated}\n` +
- `━━━━━━━━━━━━━━━━━━━━━━━━━━`;
- const ownerId = process.env.ADMIN_ID || '8439525687';
- await bot.telegram.sendMessage(ownerId, summary, { parse_mode: 'HTML' }).catch(() => {});
- return;
- }
-
- const targetId = targets[index];
- index++;
-
- try {
- await bot.telegram.sendMessage(targetId, freeCreditMsg, { parse_mode: 'HTML' });
- stats.success++;
- } catch (err) {
- stats.failed++;
- const errorMsg = err.message || "";
- if (errorMsg.includes("bot was blocked by the user") || errorMsg.includes("blocked")) {
- stats.blocked++;
- } else if (errorMsg.includes("user is deactivated") || errorMsg.includes("deactivated")) {
- stats.deactivated++;
- }
- }
-
- setTimeout(sendNext, 35);
- }
-
- sendNext();
+ // Reset daily free search flag for all users in DB so everyone can search immediately
+ try {
+ if (pool) {
+ await pool.query(`UPDATE users SET last_free_search_date = NULL`);
+ }
+ if (redisClient) {
+ const todayStr = getTodayISTDateStr();
+ const keys = await redisClient.keys(`daily_free_used:*:${todayStr}`);
+ if (keys && keys.length > 0) {
+ await redisClient.del(keys);
+ }
+ }
+ } catch (e) {
+ console.error("Error resetting daily free search status:", e.message);
+ }
+
+ await ctx.reply("🚀 Daily 1 Free Search reset & broadcast started! All users are being notified. You will receive a summary report once completed.", { parse_mode: 'HTML' });
+ startDailyResetBroadcast(true);
});
// ── BROADCAST SYSTEM STATE & LOGIC ───────────────────────────────────────
[diff_block_end]
Please note that the above snippet only shows the MODIFIED lines from the last change. It shows up to 3 lines of unchanged lines before and after the modified lines. The actual file contents may have many more lines not shown.