add votes

This commit is contained in:
Degradin 2025-02-20 09:04:38 +03:00
parent d07ade36a3
commit 0c06617685
3 changed files with 64 additions and 0 deletions

7
json/votes.json Normal file
View File

@ -0,0 +1,7 @@
{
"275416286": 10000,
"805814188": 1000,
"1045983457": 2500,
"1458518723": 3000,
"5193964398": 5000
}

54
rpg.js
View File

@ -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(`📊 <b>Общий банк:</b> ${utils.spaces(total)}\n\n${details}`);
});
rpg.action('rpg_profile', async (ctx) => {
const telegramId = ctx.from.id;

3
votes.json Normal file
View File

@ -0,0 +1,3 @@
{
"275416286": 7000
}