const { Bot, InlineKeyboard } = require("grammy"); const fs = require("fs"); const path = require("path"); const bot = new Bot(""); const DB_PATH = path.join(__dirname, "db.json"); function getDb() { if (!fs.existsSync(DB_PATH)) { return { chats: {} }; } try { const data = JSON.parse(fs.readFileSync(DB_PATH, "utf-8")); if (!data.chats) data.chats = {}; return data; } catch (e) { return { chats: {} }; } } function saveDb(data) { fs.writeFileSync(DB_PATH, JSON.stringify(data, null, 2)); } function getChatUser(db, chatInstance, userId, firstName) { if (!db.chats[chatInstance]) { db.chats[chatInstance] = {}; } if (!db.chats[chatInstance][userId]) { db.chats[chatInstance][userId] = { name: firstName, size: 0, last_grown: 0 }; } if (firstName) db.chats[chatInstance][userId].name = firstName; return db.chats[chatInstance][userId]; } function getTopUsers(db, chatInstance) { if (!db.chats[chatInstance]) return []; return Object.values(db.chats[chatInstance]) .sort((a, b) => b.size - a.size) .slice(0, 10); } bot.on("inline_query", async (ctx) => { const user = ctx.from; await ctx.answerInlineQuery([ { type: "article", id: "grow-pp", title: "Вырастить писю 🍆", description: "Испытать удачу (+1-30 см)", input_message_content: { message_text: `🍆 *${user.first_name}* готовится вырастить свою писю...`, parse_mode: "Markdown" }, reply_markup: new InlineKeyboard().text("🍆 Вырастить", "grow_action") }, { type: "article", id: "top-pp", title: "Топ писи 🏆", description: "Показать лидеров чата", input_message_content: { message_text: "🏆 *Топ писюнов чата*", parse_mode: "Markdown" }, reply_markup: new InlineKeyboard().text("🔄 Загрузить топ", "top_action") } ], { cache_time: 0 }); }); bot.callbackQuery("grow_action", async (ctx) => { // console.log(ctx.update); const chatInstance = ctx.update.callback_query.chat_instance; const userId = ctx.from.id; const firstName = ctx.from.first_name; if (!chatInstance) { return ctx.answerCallbackQuery({ text: "Error: No chat instance found." }); } const db = getDb(); const user = getChatUser(db, chatInstance, userId, firstName); const now = Date.now(); const cooldown = 24 * 60 * 60 * 1000; if (user.last_grown && (now - user.last_grown < cooldown)) { const remaining = cooldown - (now - user.last_grown); const hours = Math.floor(remaining / (1000 * 60 * 60)); const minutes = Math.floor((remaining % (1000 * 60 * 60)) / (1000 * 60)); return ctx.answerCallbackQuery({ text: `⏳ Ты уже растил сегодня! Приходи через ${hours} ч ${minutes} мин.`, show_alert: true }); } const growth = Math.floor(Math.random() * 30) + 1; user.size += growth; user.last_grown = now; saveDb(db); try { await ctx.editMessageText( `🍆 *${user.name}* вырастил писю!\n` + `➕ Прирост: *${growth} см*\n` + `📏 Общий размер: *${user.size} см*`, { parse_mode: "Markdown", reply_markup: new InlineKeyboard().text("🍆 Вырастить ещё", "grow_action") } ); } catch (e) { } await ctx.answerCallbackQuery({ text: `Выросла на ${growth} см!` }); }); bot.callbackQuery("top_action", async (ctx) => { const chatInstance = ctx.update.callback_query.chat_instance; if (!chatInstance) { return ctx.answerCallbackQuery({ text: "Error: No chat instance found." }); } const db = getDb(); const sortedUsers = getTopUsers(db, chatInstance); let topText = "🏆 *Топ писи чата:*\n\n"; if (sortedUsers.length === 0) { topText += "В этом чате еще никто не мерялся..."; } else { sortedUsers.forEach((u, index) => { let prefix = `${index + 1}.`; if (index === 0) prefix = "🥇"; if (index === 1) prefix = "🥈"; if (index === 2) prefix = "🥉"; topText += `${prefix} ${u.name} — *${u.size} см*\n`; }); } try { await ctx.editMessageText( topText, { parse_mode: "Markdown", reply_markup: new InlineKeyboard().text("🔄 Обновить", "top_action") } ); } catch (e) { } await ctx.answerCallbackQuery(); }); bot.command("start", (ctx) => ctx.reply("Напиши @BotName в чате, чтобы играть!")); bot.catch((err) => { console.error("Bot Error:", err); }); bot.start();