diff --git a/commands/afficher-lies.js b/commands/afficher-lies.js new file mode 100644 index 0000000..5da0d96 --- /dev/null +++ b/commands/afficher-lies.js @@ -0,0 +1,35 @@ +const { EmbedBuilder } = require('discord.js'); +const { getAllLinks } = require('../database.js'); + +module.exports = { + data: { + name: 'afficher-lies', + description: 'Affiche la liste des comptes liés', + }, + async execute(interaction) { + const links = await getAllLinks(); + + if (!links || links.length === 0) { + return interaction.reply({ + content: 'Aucun compte lié trouvé.', + ephemeral: true + }); + } + + const embed = new EmbedBuilder() + .setColor('#00FF00') + .setTitle('👥 Liste des comptes liés') + .setDescription(links.map((link, index) => { + const lastConn = link.lastConnection + ? `📅 Dernière connexion: ` + : '📅 Dernière connexion: Jamais vu'; + + return `**${index + 1}.** <@${link.discordId}> ${link.discordUsername ? `(${link.discordUsername})` : ''}\n` + + `└ 🎮 **${link.palworldName}** (Steam: \`${link.steamId}\`)\n` + + `└ ${lastConn}`; + }).join('\n\n')) + .setTimestamp(); + + return interaction.reply({ embeds: [embed] }); + }, +}; \ No newline at end of file diff --git a/consoleMonitor.js b/consoleMonitor.js index 2f44caa..8640721 100644 --- a/consoleMonitor.js +++ b/consoleMonitor.js @@ -1,6 +1,6 @@ const axios = require('axios'); const WebSocket = require('ws'); -const { verifyLinkCode, updateUserLinkWithUsername } = require('./database.js'); +const { verifyLinkCode, updateUserLinkWithUsername, updateLastConnection } = require('./database.js'); let ws = null; let reconnectTimeout = null; @@ -25,12 +25,30 @@ const parseLogMessage = (log) => { console.log(`✅ Commande !lier détectée: ${playerName} avec le code ${code}`); return { + type: 'link', playerName: playerName, code: code, needsSteamId: true }; } + // Détecter les déconnexions: [2025-12-09 18:55:19] [LOG] Nami left the server. (User id: gdk_2535420062888893) + const disconnectRegex = /\[.*?\]\s*\[LOG\]\s*(.+?)\s+left the server\.\s*\(User id:\s*(.+?)\)/i; + const disconnectMatch = log.match(disconnectRegex); + + if (disconnectMatch) { + const playerName = disconnectMatch[1].trim(); + const userId = disconnectMatch[2].trim(); + + console.log(`👋 Déconnexion détectée: ${playerName} (${userId})`); + + return { + type: 'disconnect', + playerName: playerName, + userId: userId + }; + } + return null; }; @@ -211,13 +229,27 @@ const connectWebSocket = async (pterodactylToken, serverId) => { const linkData = parseLogMessage(log); if (linkData) { - const playerData = await getSteamIdFromPlayerName(linkData.playerName); - - if (playerData) { - await handleLinkCommand(linkData.playerName, playerData, linkData.code); - setTimeout(() => checkAndManageWebSocket(), 2000); - } else { - console.log(`❌ Impossible de trouver le Steam ID pour ${linkData.playerName}`); + if (linkData.type === 'link') { + const playerData = await getSteamIdFromPlayerName(linkData.playerName); + + if (playerData) { + await handleLinkCommand(linkData.playerName, playerData, linkData.code); + setTimeout(() => checkAndManageWebSocket(), 2000); + } else { + console.log(`❌ Impossible de trouver le Steam ID pour ${linkData.playerName}`); + } + } else if (linkData.type === 'disconnect') { + // Extraire le Steam ID du userId (format: gdk_STEAMID) + const steamIdMatch = linkData.userId.match(/gdk_(\d+)/); + if (steamIdMatch) { + const steamId = steamIdMatch[1]; + try { + await updateLastConnection(steamId); + console.log(`✅ Dernière connexion mise à jour pour ${linkData.playerName} (${steamId})`); + } catch (error) { + console.error('Erreur lors de la mise à jour de la dernière connexion:', error); + } + } } } } diff --git a/database.js b/database.js index 22def01..561f1cf 100644 --- a/database.js +++ b/database.js @@ -57,6 +57,7 @@ const createTables = async () => { player_id VARCHAR(50), palworld_username VARCHAR(100) NOT NULL, linked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + lastConnection TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, INDEX idx_discord_id (discord_id), INDEX idx_steam_id (steam_id), INDEX idx_player_id (player_id) @@ -165,6 +166,14 @@ const cleanExpiredCodes = async () => { ); }; +const updateLastConnection = async (steamId) => { + const connection = getConnection(); + await connection.execute( + 'UPDATE user_links SET lastConnection = NOW() WHERE steam_id = ?', + [steamId] + ); +}; + module.exports = { initDatabase, getConnection, @@ -177,5 +186,6 @@ module.exports = { deleteUserLink, getPendingPlayer, hasActiveLinkCodes, - cleanExpiredCodes + cleanExpiredCodes, + updateLastConnection };