From 0c0661768555512ca0abb9faee8b9aa620de7974 Mon Sep 17 00:00:00 2001 From: Degradin Date: Thu, 20 Feb 2025 09:04:38 +0300 Subject: [PATCH] add votes --- json/votes.json | 7 +++++++ rpg.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ votes.json | 3 +++ 3 files changed, 64 insertions(+) create mode 100644 json/votes.json create mode 100644 votes.json diff --git a/json/votes.json b/json/votes.json new file mode 100644 index 0000000..99b45a1 --- /dev/null +++ b/json/votes.json @@ -0,0 +1,7 @@ +{ + "275416286": 10000, + "805814188": 1000, + "1045983457": 2500, + "1458518723": 3000, + "5193964398": 5000 +} \ No newline at end of file diff --git a/rpg.js b/rpg.js index 6646b5c..6149b86 100644 --- a/rpg.js +++ b/rpg.js @@ -55,6 +55,60 @@ rpg.use(async (ctx, next) => { await next(); // Передаем управление следующему middleware }); +const votesFile = path.join(__dirname, '/json/votes.json'); + +// Функция для загрузки данных из JSON +function loadVotes() { + if (!fs.existsSync(votesFile)) return {}; + return JSON.parse(fs.readFileSync(votesFile, 'utf8')); +} + +// Функция для сохранения данных в JSON +function saveVotes(votes) { + fs.writeFileSync(votesFile, JSON.stringify(votes, null, 2), 'utf8'); +} + +rpg.command('offer', async (ctx) => { + try { + const args = ctx.message.text.split(' ').slice(1); + const amount = parseInt(args[0]); + + if (!amount || amount <= 0) { + return await ctx.reply('❌ Введите корректную сумму (например: /offer 5000)'); + } + + let votes = loadVotes(); + votes[ctx.from.id] = amount; // Обновление суммы пользователя + saveVotes(votes); + + return await ctx.reply(`✅ Вы предложили сумму: ${amount}₽`); + } catch (error) { + console.error(error); + return await ctx.reply('❌ Произошла ошибка при обработке команды.'); + } +}); + +rpg.command('voteinfo', async (ctx) => { + if (ctx.from.id != 275416286) return; + + let users = await UserModel.findAll({ attributes: ['telegram_id', 'username'] }); + let userMap = Object.fromEntries(users.map(user => [user.telegram_id, user.username || `ID: ${user.telegram_id}`])); + + let votes = loadVotes(); + if (Object.keys(votes).length === 0) { + return await ctx.reply('🔹 Пока никто не предложил сумму.'); + } + + let total = Object.values(votes).reduce((sum, amount) => sum + amount, 0); + let details = Object.entries(votes) + .map(([id, amount]) => `👤 ${userMap[id] || `ID: ${id}`}: ${utils.spaces(amount)}₽`) + .join('\n'); + + await ctx.replyWithHTML(`📊 Общий банк: ${utils.spaces(total)}₽\n\n${details}`); +}); + + + rpg.action('rpg_profile', async (ctx) => { const telegramId = ctx.from.id; diff --git a/votes.json b/votes.json new file mode 100644 index 0000000..b4714f5 --- /dev/null +++ b/votes.json @@ -0,0 +1,3 @@ +{ + "275416286": 7000 +} \ No newline at end of file