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] @@ -558,12 +558,18 @@ const userId = ctx.from.id; const now = Date.now(); - // Capture referral code if present in /start command before blocking - if (ctx.message && ctx.message.text && ctx.message.text.includes('/start ref_')) { - const refCode = ctx.message.text.split('ref_')[1]; - if (refCode) { - pendingReferrals[userId] = refCode; - console.log(`[REFERRAL CAPTURE] Captured pending referral for user ${userId} from referrer ${refCode}`); + // Capture referral code if present in /start command before blocking or proceeding + if (ctx.message && ctx.message.text) { + const refMatch = ctx.message.text.match(/ref_(\d+)/); + if (refMatch) { + const refCode = refMatch[1]; + if (refCode && String(refCode) !== String(userId)) { + pendingReferrals[userId] = refCode; + if (redisClient) { + redisClient.set(`pending_ref:${userId}`, refCode, 'EX', 86400 * 7).catch(() => {}); + } + console.log(`[REFERRAL CAPTURE] Captured pending referral for user ${userId} from referrer ${refCode}`); + } } } @@ -627,9 +627,32 @@ return; } else { if (ctx.callbackQuery && ctx.callbackQuery.data === 'check_joined') { - await ctx.answerCbQuery('Thank you for joining! You can now use the bot.', { show_alert: true }); - await ctx.deleteMessage().catch(() => {}); - await ctx.reply('✅ Access granted! Send /start to begin.', { parse_mode: 'HTML' }); + await ctx.answerCbQuery('✅ Channels verified successfully! Welcome to Aadhaar VIP Hub.', { show_alert: false }).catch(() => {}); + + let refCode = pendingReferrals[userId]; + if (!refCode && redisClient) { + refCode = await redisClient.get(`pending_ref:${userId}`).catch(() => null); + } + if (refCode && String(refCode) === String(userId)) { + refCode = null; + } + + const firstName = ctx.from?.first_name || 'User'; + const isNew = await isUserNew(userId); + if (isNew) { + await registerNewUser(userId, firstName, refCode); + } + delete pendingReferrals[userId]; + if (redisClient) { + redisClient.del(`pending_ref:${userId}`).catch(() => {}); + } + + if (getOrCreateUserThread) { + await getOrCreateUserThread(ctx, userId, firstName, ctx.from?.username || '').catch(() => {}); + } + + const access = await checkUserAccess(userId); + await showAuthorizedWelcomeMenu(ctx, userId, firstName, isNew, access); return; } } @@ -664,7 +664,9 @@ registerNewUser, getOrCreateUserThread, showAuthorizedWelcomeMenu, - sessions + sessions, + pendingReferrals, + redisClient }); bot.command(['ping', 'Ping'], async (ctx) => { [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.