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