From e631389fffd9d87ec1d473362fc30156344e3660 Mon Sep 17 00:00:00 2001 From: Nova Coder Date: Mon, 25 May 2026 07:34:26 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20/mijn=20command=20=E2=80=94=20DM-only?= =?UTF-8?q?=20stats/PIN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Voegt /mijn command toe dat in DM: - Player stats toont (matches gewonnen/verloren) - Totale uren toont - PIN, niveau, positie toont - Link naar profielpagina /beschikbaar blijft redirect naar web. --- bot.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/bot.js b/bot.js index d28eef1..eabd861 100644 --- a/bot.js +++ b/bot.js @@ -197,14 +197,75 @@ bot.onText(/\/start(@padel_nl_bot)?/, async (msg) => { sendMsg(chatId, 'Hallo ' + name + '! 🏓\n\nWat is je volledige naam?'); }); -// /beschikbaar, /mijn — redirect naar web -bot.onText(/\/beschikbaar(@padel_nl_bot)?|\/mijn(@padel_nl_bot)?/, async (msg) => { +// /beschikbaar — redirect naar web +bot.onText(/\/beschikbaar(@padel_nl_bot)?/, async (msg) => { const chatId = msg.chat.id; - sendMsg(chatId, '🌐 **Ga naar de webpagina** voor beschikbaarheid en profiel:\nhttps://padel.iamdoingthings.com\n\nLog in met je PIN (die je kreeg bij registratie).', + sendMsg(chatId, '🌐 **Ga naar de webpagina** voor beschikbaarheid:\nhttps://padel.iamdoingthings.com', { parse_mode: 'Markdown', disable_web_page_preview: true } ); }); +// /mijn — DM-only stats + PIN +bot.onText(/\/mijn(@padel_nl_bot)?/, async (msg) => { + const chatId = msg.chat.id; + if (!isDM(msg)) { + return sendMsg(chatId, '📊 **Jouw stats** zijn alleen in DM zichtbaar.\n\nStuur /mijn naar [@padel_nl_bot](https://t.me/padel_nl_bot)', + { parse_mode: 'Markdown' } + ); + } + try { + const players = await api('/players'); + if (!Array.isArray(players)) throw new Error('Invalid players response'); + const player = players.find(p => p.telegram_id == chatId); + if (!player) return sendMsg(chatId, 'Je bent niet geregistreerd. Stuur /start om te beginnen.'); + + const matches = await api('/matches'); + if (!Array.isArray(matches)) throw new Error('Invalid matches response'); + + const playerMatches = matches.filter(m => + m.players && m.players.some(pid => pid == player.id) && + (m.status === 'completed' || m.status === 'confirmed') + ); + + const won = playerMatches.filter(m => { + if (!m.score || !m.score.winner) return false; + return m.score.winner === 1 ? + (m.score.team1 || []).some(pid => pid == player.id) : + (m.score.team2 || []).some(pid => pid == player.id); + }).length; + const played = playerMatches.length; + const lost = played - won; + + const sessions = await api('/sessions') || []; + const playerSessions = (sessions || []).filter(s => + s.players && s.players.some(pid => pid == player.id || pid === player.id) + ); + const totalHours = playerSessions.reduce((sum, s) => { + const start = s.start || (s.data || {}).start || "00:00"; + const end = s.end || (s.data || {}).end || "00:00"; + const [sh, sm] = start.split(":").map(Number); + const [eh, em] = end.split(":").map(Number); + return sum + ((eh + em / 60) - (sh + sm / 60)); + }, 0); + + const level = player.level || 'onbekend'; + const position = player.position || 'onbekend'; + + let msg = '📊 **Jouw Statistieken**\n\n'; + msg += '👤 ' + player.name + '\n'; + msg += '🎯 Niveau: ' + level + ' | Positie: ' + position + '\n'; + msg += '🔑 PIN: `' + player.pin + '`\n\n'; + msg += '🎾 Wedstrijden: ' + played + ' (' + won + '⚡ win / ' + lost + '❌ verlies)\n'; + msg += '⏱️ Totaal uren: ' + Math.round(totalHours * 10) / 10 + 'h\n'; + msg += '\n[Wijzig profiel](https://padel.iamdoingthings.com) — je PIN is je inlogcode.'; + + sendMsg(chatId, msg, { parse_mode: 'Markdown' }); + } catch (e) { + logError('/mijn error:', e.message); + sendMsg(chatId, '❌ Kon stats niet laden. Probeer later opnieuw.'); + } +}); + // /matches — openstaande matches bot.onText(/\/matches(@padel_nl_bot)?/, async (msg) => { const chatId = msg.chat.id;