The following changes were made by the replace_file_content tool to: /root/aadharbot/modules/commands/start.js. If relevant, proactively run terminal commands to execute this code for the USER. Don't ask for permission. [diff_block_start] @@ -1,4 +1,4 @@ -function registerStartCommand(bot, { isUserNew, registerNewUser, getOrCreateUserThread, checkUserAccess, showAuthorizedWelcomeMenu }) { +function registerStartCommand(bot, { isUserNew, registerNewUser, getOrCreateUserThread, checkUserAccess, showAuthorizedWelcomeMenu, pendingReferrals, redisClient }) { bot.command('start', async (ctx) => { try { const userId = ctx.chat.id; @@ -5,7 +5,17 @@ let referralCode = null; - if (ctx.message && ctx.message.text && ctx.message.text.includes('ref_')) { - referralCode = ctx.message.text.split('ref_')[1]; + const match = ctx.message?.text?.match(/ref_(\d+)/); + if (match) { + referralCode = match[1]; + } + if (!referralCode && pendingReferrals && pendingReferrals[userId]) { + referralCode = pendingReferrals[userId]; + } + if (!referralCode && redisClient) { + referralCode = await redisClient.get(`pending_ref:${userId}`).catch(() => null); + } + if (referralCode && String(referralCode) === String(userId)) { + referralCode = null; } const isNew = await isUserNew(userId); @@ -12,8 +12,10 @@ await registerNewUser(userId, firstName, referralCode); } + if (pendingReferrals) delete pendingReferrals[userId]; + if (redisClient) redisClient.del(`pending_ref:${userId}`).catch(() => {}); if (getOrCreateUserThread) { - await getOrCreateUserThread(ctx, userId, firstName, ctx.from?.username || ''); + await getOrCreateUserThread(ctx, userId, firstName, ctx.from?.username || '').catch(() => {}); } const access = await checkUserAccess(userId); [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.