From 1a223ae207a1f1951eb87a280e7a3faf914a53e0 Mon Sep 17 00:00:00 2001 From: milo Date: Sat, 10 May 2025 22:37:51 +0200 Subject: [PATCH 1/3] some sockets changes --- index.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ef93530..ed96859 100644 --- a/index.js +++ b/index.js @@ -692,6 +692,7 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun const { name } = data; console.log(name) + // 'timeout' command if (name === 'timeout') { // Interaction context @@ -758,6 +759,7 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun if (!poll) { clearInterval(countdownInterval); + io.emit('new-poll', { action: 'timeout cleared' }); return; } @@ -803,6 +805,7 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun console.log('clear poll') clearInterval(countdownInterval); delete activePolls[id]; + io.emit('new-poll', { action: 'timeout cleared' }); return; } @@ -863,6 +866,9 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun const seconds = remaining % 60; const countdownText = `**${minutes}m ${seconds}s** restantes`; + // web site update + io.emit('new-poll', { action: 'timeout command' }); + return res.send({ type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE, data: { @@ -2460,11 +2466,13 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun return res.status(400).json({ error: 'unknown interaction type' }); }); +// Get all users ordered by coins app.get('/users', (req, res) => { const users = getAllUsers.all(); res.json(users); }); +// Get user's avatar app.get('/user/:id/avatar', async (req, res) => { try { const userId = req.params.id; // Get the ID from route parameters @@ -2483,12 +2491,54 @@ app.get('/user/:id/avatar', async (req, res) => { } }) +// Get user's inventory +app.get('/user/:id/inventory', async (req, res) => { + try { + const userId = req.params.id; // Get the ID from route parameters + const user = await client.users.fetch(userId); + + if (!user) { + return res.status(404).json({ error: 'User not found' }); + } + + const inventory = getUserInventory.all({user_id: userId}); + res.json({ inventory }); + + } catch (error) { + console.error('Error fetching user avatar'); + res.status(500).json({ error: 'Failed to fetch avatar' }); + } +}) + +// Get active polls +app.get('/polls', async (req, res) => { + try { + res.json({ activePolls }); + + } catch (error) { + console.error('Error fetching active polls'); + res.status(500).json({ error: 'Failed to fetch active polls' }); + } +}) + app.post('/send-message', (req, res) => { - const { channelId, message } = req.body; + const { userId, channelId, message } = req.body; const channel = client.channels.cache.get(channelId); + const user = getUser.get(userId); + + if (!user) return res.status(404).json({ error: 'User not found' }); + if (!channel) return res.status(404).json({ error: 'Channel not found' }); + if (user.coins < 10) return res.status(403).json({ error: 'Pas assez de coins' }); + + updateUserCoins.run({ + id: userId, + coins: user.coins - 10, + }) + io.emit('data-updated', { table: 'users', action: 'update' }); + channel.send(message) .then(() => res.json({ success: true })) .catch(err => res.status(500).json({ error: err.message })); From 306cdae771b41c42a4652aaf68a5e49d649e8497 Mon Sep 17 00:00:00 2001 From: milo Date: Sun, 11 May 2025 02:58:33 +0200 Subject: [PATCH 2/3] socket emit on TO votes --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index ed96859..53ab878 100644 --- a/index.js +++ b/index.js @@ -1540,6 +1540,8 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun poll.against++; } + io.emit('new-poll', { action: 'new vote' }); + // Retrieve online eligible users (ensure your bot has the necessary intents) const guildId = req.body.guild_id; const roleId = process.env.VOTING_ROLE_ID; // Set this in your .env file From acadd2f6e0b5e33662349589b630137243fe1414 Mon Sep 17 00:00:00 2001 From: milo Date: Sun, 11 May 2025 18:07:02 +0200 Subject: [PATCH 3/3] /check, /change-nickname, /add-coins --- index.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/index.js b/index.js index 53ab878..07d3200 100644 --- a/index.js +++ b/index.js @@ -2468,6 +2468,11 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun return res.status(400).json({ error: 'unknown interaction type' }); }); +// Check flAPI +app.get('/check', (req, res) => { + res.status(200).json({ check: true, status: 'OK' }); +}); + // Get all users ordered by coins app.get('/users', (req, res) => { const users = getAllUsers.all(); @@ -2523,6 +2528,7 @@ app.get('/polls', async (req, res) => { } }) +// Send a custom message in the admin command channel app.post('/send-message', (req, res) => { const { userId, channelId, message } = req.body; const channel = client.channels.cache.get(channelId); @@ -2546,6 +2552,50 @@ app.post('/send-message', (req, res) => { .catch(err => res.status(500).json({ error: err.message })); }); +// Change user's server specific username +app.post('/change-nickname', async (req, res) => { + const { userId, nickname, commandUserId } = req.body; + + const commandUser = getUser.get(commandUserId); + + if (!commandUser) return res.status(404).json({ message: 'Oups petit soucis' }); + + if (commandUser.coins < 1000) return res.status(403).json({ message: 'Pas assez de coins' }); + + try { + const guild = await client.guilds.fetch(process.env.GUILD_ID); + const member = await guild.members.fetch(userId); + await member.setNickname(nickname); + let message = nickname ? `Le pseudo de '${member.user.tag}' a été changé en '${nickname}'` : `Le pseudo de '${member.user.tag}' a été remis par défaut` + res.status(200).json({ message : message }); + updateUserCoins.run({ + id: commandUserId, + coins: commandUser.coins - 1000, + }) + io.emit('data-updated', { table: 'users', action: 'update' }); + } catch (error) { + res.status(500).json({ message : `J'ai pas réussi à changer le pseudo` }); + } +}) + +// ADMIN Add coins +app.post('/add-coins', (req, res) => { + const { commandUserId } = req.body; + + const commandUser = getUser.get(commandUserId); + + if (!commandUser) return res.status(404).json({ error: 'User not found' }); + if (commandUserId !== process.env.DEV_ID) return res.status(404).json({ error: 'Not admin' }); + + updateUserCoins.run({ + id: commandUserId, + coins: commandUser.coins + 100, + }) + io.emit('data-updated', { table: 'users', action: 'update' }); + + res.status(200).json({ message : `+100` }); +}); + import http from 'http'; import { Server } from 'socket.io'; const server = http.createServer(app);