From 33fcc6edf54f0a6ec6f29b632d458f99ec20dcdd Mon Sep 17 00:00:00 2001 From: milo Date: Wed, 18 Jun 2025 18:47:22 +0200 Subject: [PATCH] daily reward --- index.js | 70 +++++++++++++++++++++++++----------------------- init_database.js | 5 +++- 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index 2696715..a2b39d3 100644 --- a/index.js +++ b/index.js @@ -45,7 +45,7 @@ import { getUserInventory, getTopSkins, updateUserCoins, insertLog, stmtLogs, - getLogs, getUserLogs, getUserElo, getUserGames, getUsersByElo, + getLogs, getUserLogs, getUserElo, getUserGames, getUsersByElo, resetDailyReward, queryDailyReward, } from './init_database.js'; import { getValorantSkins, getSkinTiers } from './valo.js'; import {sleep} from "openai/core"; @@ -672,39 +672,13 @@ client.once('ready', async () => { todaysHydrateCron = `${randomMinute} ${randomHour} * * *` console.log(todaysHydrateCron) - const guild = await client.guilds.fetch(process.env.GUILD_ID); - const roleId = process.env.VOTING_ROLE_ID; // Set this in your .env file - const members = await getOnlineUsersWithRole(process.env.GUILD_ID, roleId); - - const prob = Math.random(); - if (members.size === 0 || prob > process.env.CHAOS_PROB) { - console.log(`No roulette tonight ${prob}`) - return - } - - const randomMember = members[Math.floor(Math.random() * members.size)]; - - const timeoutUntil = new Date(Date.now() + 12 * 60 * 60 * 1000).toISOString(); - try { - await guild.members.edit(randomMember.user.id, { - communication_disabled_until: timeoutUntil, - reason: 'Roulette Russe 🔔', - }); - - const generalChannel = guild.channels.cache.find( - ch => ch.name === 'gĂ©nĂ©ral' || ch.name === 'general' - ); - - if (generalChannel && generalChannel.isTextBased()) { - generalChannel.send( - `🎯 <@${randomMember.user.id}> ça dĂ©gage, Ă  mimir ! (jusqu'Ă  12h00)` - ); - } - - console.log(`${randomMember.user.username} has been timed out until ${timeoutUntil}`); - } catch (err) { - console.error('Failed to timeout random member:', err); + const akhys = getAllUsers.all() + akhys.forEach((akhy) => { + resetDailyReward.run(akhy); + }) + } catch (e) { + console.log(e) } }); @@ -2781,6 +2755,36 @@ app.get('/user/:id/inventory', async (req, res) => { } }) +app.get('/user/:id/daily', async (req, res) => { + const userId = req.params.id + + const akhy = getUser.get(userId) + + if (!akhy) return res.status(404).send({ message: 'Utilisateur introuvable'}) + + if (akhy.dailyQueried) return res.status(403).send({ message: 'RĂ©compense dĂ©jĂ  rĂ©cupĂ©rĂ©e'}) + + const amount = 200 + const coins = akhy.coins + + queryDailyReward.run(userId) + updateUserCoins.run({ + id: userId, + coins: coins + amount, + }) + insertLog.run({ + id: userId + '-' + Date.now(), + user_id: userId, + action: 'DAILY_REWARD', + target_user_id: null, + coins_amount: amount, + user_new_amount: coins + amount, + }) + io.emit('data-updated', { table: 'users', action: 'update' }); + + return res.status(200).send({ message: 'RĂ©compense rĂ©cupĂ©rĂ©e !' }) +}) + // Get active polls app.get('/polls', async (req, res) => { try { diff --git a/init_database.js b/init_database.js index fc0f18e..daaec0b 100644 --- a/init_database.js +++ b/init_database.js @@ -12,7 +12,8 @@ export const stmtUsers = flopoDB.prepare(` warns INTEGER DEFAULT 0, allTimeWarns INTEGER DEFAULT 0, totalRequests INTEGER DEFAULT 0, - coins INTEGER DEFAULT 0 + coins INTEGER DEFAULT 0, + dailyQueried BOOLEAN DEFAULT 0 ) `); stmtUsers.run(); @@ -37,6 +38,8 @@ stmtSkins.run() export const insertUser = flopoDB.prepare('INSERT INTO users (id, username, globalName, warned, warns, allTimeWarns, totalRequests) VALUES (@id, @username, @globalName, @warned, @warns, @allTimeWarns, @totalRequests)'); export const updateUser = flopoDB.prepare('UPDATE users SET warned = @warned, warns = @warns, allTimeWarns = @allTimeWarns, totalRequests = @totalRequests WHERE id = @id'); +export const queryDailyReward = flopoDB.prepare(`UPDATE users SET dailyQueried = 1 WHERE id = ?`); +export const resetDailyReward = flopoDB.prepare(`UPDATE users SET dailyQueried = 0 WHERE id = ?`); export const updateUserCoins = flopoDB.prepare('UPDATE users SET coins = @coins WHERE id = @id'); export const getUser = flopoDB.prepare('SELECT * FROM users WHERE id = ?'); export const getAllUsers = flopoDB.prepare('SELECT * FROM users ORDER BY coins DESC');