Merge pull request #15 from cassoule/active-commands-route

Active commands route
This commit is contained in:
Milo Gourvest
2025-05-11 18:07:30 +02:00
committed by GitHub

104
index.js
View File

@@ -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: {
@@ -1534,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
@@ -2460,11 +2468,18 @@ 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();
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,17 +2498,104 @@ 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' });
}
})
// Send a custom message in the admin command channel
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 }));
});
// 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);