| 28 | initialized: false, |
| 29 | |
| 30 | async handler(sock: any, message: any, args: any, _context: BotContext) { |
| 31 | const { chatId } = _context; |
| 32 | |
| 33 | if (mathGames[chatId]) { |
| 34 | return sock.sendMessage(chatId, { text: '⚠️ Solve the current problem first!' }, { quoted: mathGames[chatId].msg }); |
| 35 | } |
| 36 | |
| 37 | const mode = args[0]?.toLowerCase(); |
| 38 | if (!mode || !(mode in modes)) { |
| 39 | return sock.sendMessage(chatId, { |
| 40 | text: `🧮 *Available Difficulties:*\n\n${Object.keys(modes).join(' | ')}\n\n_Example: .math normal_` |
| 41 | }, { quoted: message }); |
| 42 | } |
| 43 | |
| 44 | const math = genMath(mode); |
| 45 | const text = `▢ HOW MUCH IS IT *${math.str}*=\n\n_Time:_ ${(math.time / 1000).toFixed(2)} seconds`; |
| 46 | |
| 47 | const sentMsg = await sock.sendMessage(chatId, { text }, { quoted: message }); |
| 48 | |
| 49 | mathGames[chatId] = { |
| 50 | msg: sentMsg, |
| 51 | math, |
| 52 | attempts: 4, |
| 53 | timeout: setTimeout(() => { |
| 54 | if (mathGames[chatId]) { |
| 55 | sock.sendMessage(chatId, { text: `⏳ *Time is up!*\nThe answer was: *${math.result}*` }, { quoted: mathGames[chatId].msg }); |
| 56 | delete mathGames[chatId]; |
| 57 | } |
| 58 | }, math.time) |
| 59 | }; |
| 60 | |
| 61 | if (!this.initialized) { |
| 62 | this.initialized = true; |
| 63 | sock.ev.on('messages.upsert', async (upsert: any) => { |
| 64 | const m = upsert.messages[0]; |
| 65 | if (!m.message || m.key.fromMe) return; |
| 66 | |
| 67 | const chat = m.key.remoteJid; |
| 68 | if (!mathGames[chat]) return; |
| 69 | |
| 70 | const body = (m.message.conversation || m.message.extendedTextMessage?.text || "").trim(); |
| 71 | if (!/^-?[0-9]+(\.[0-9]+)?$/.test(body)) return; |
| 72 | |
| 73 | const quoted = m.message.extendedTextMessage?.contextInfo?.quotedMessage; |
| 74 | const quotedText = quoted?.conversation || quoted?.extendedTextMessage?.text || ""; |
| 75 | if (!/^▢ HOW MUCH IS IT/i.test(quotedText)) return; |
| 76 | |
| 77 | const game = mathGames[chat]; |
| 78 | if (body === game.math.result) { |
| 79 | clearTimeout(game.timeout); |
| 80 | delete mathGames[chat]; |
| 81 | await sock.sendMessage(chat, { text: `✅ *Correct answer!*\n\nYou won the game.` }, { quoted: m }); |
| 82 | } else { |
| 83 | game.attempts--; |
| 84 | if (game.attempts <= 0) { |
| 85 | clearTimeout(game.timeout); |
| 86 | delete mathGames[chat]; |
| 87 | await sock.sendMessage(chat, { text: `❌ *Game Over!*\n\nThe correct answer was: *${game.math.result}*` }, { quoted: m }); |