const { Events, ChannelType, PermissionFlagsBits } = require("discord.js"); const db = require("../db"); const { sendLog } = require("../fonctions/sendLog"); // Store voice join times and intervals for economy const voiceJoinTimes = new Map(); // guildId_oderId -> timestamp const voiceMoneyIntervals = new Map(); // guildId_userId -> intervalId module.exports = { name: Events.VoiceStateUpdate, async execute(client, oldState, newState) { // ===== PRIVATE ROOM (TEMP VOICE CHANNELS) ===== await handlePrivateRoom(client, oldState, newState); // ===== VOICE LOGS ===== await handleVoiceLogs(client, oldState, newState); if (newState.member.user.bot) return; const guildId = newState.guild.id; const oderId = newState.member.id; const key = `${guildId}_${oderId}`; // ===== TRACK VOICE TIME STATS ===== await trackVoiceTime(guildId, oderId, oldState, newState); // ===== AUTOROLE VOCAL ===== db.get( "SELECT enabled, role_id, exclude_channel_ids FROM autorole_vocal_config WHERE guild_id = ?", [guildId], (err, row) => { if (err || !row || !row.enabled) return; let excludeChannelIds = []; try { excludeChannelIds = row.exclude_channel_ids ? JSON.parse(row.exclude_channel_ids) : []; } catch (err) { console.error("Erreur parsing exclude_channel_ids", err); excludeChannelIds = []; } const role = newState.guild.roles.cache.get(row.role_id); if (!role) return; if (newState.channelId && !excludeChannelIds.includes(newState.channelId) && !newState.member.roles.cache.has(role.id)) { newState.member.roles.add(role); } else if (!newState.channelId || excludeChannelIds.includes(newState.channelId)) { newState.member.roles.remove(role); } } ); // ===== ECONOMY VOICE MONEY ===== db.get( `SELECT enabled, voice_money_enabled, voice_money_min, voice_money_max, voice_money_interval_minutes FROM economy_config WHERE guild_id = ?`, [guildId], (err, ecoRow) => { if (err || !ecoRow || !ecoRow.enabled || !ecoRow.voice_money_enabled) { // Clear interval if economy is disabled if (voiceMoneyIntervals.has(key)) { clearInterval(voiceMoneyIntervals.get(key)); voiceMoneyIntervals.delete(key); voiceJoinTimes.delete(key); } return; } const intervalMs = ecoRow.voice_money_interval_minutes * 60 * 1000; const minMoney = ecoRow.voice_money_min; const maxMoney = ecoRow.voice_money_max; // User joined a voice channel if (newState.channelId && !oldState.channelId) { voiceJoinTimes.set(key, Date.now()); // Start interval for giving money const intervalId = setInterval(() => { // Check if user is still in voice const member = newState.guild.members.cache.get(oderId); if (!member || !member.voice.channelId) { clearInterval(intervalId); voiceMoneyIntervals.delete(key); voiceJoinTimes.delete(key); return; } // Check if user is deafened or muted (optional: don't give money if AFK) // You can customize this behavior const moneyToAdd = Math.floor(Math.random() * (maxMoney - minMoney + 1)) + minMoney; db.get( `SELECT balance FROM user_economy WHERE guild_id = ? AND user_id = ?`, [guildId, oderId], (err, userRow) => { if (err) return; const newBalance = (userRow?.balance || 0) + moneyToAdd; db.run( `INSERT INTO user_economy (guild_id, user_id, balance, bank) VALUES (?, ?, ?, 0) ON CONFLICT(guild_id, user_id) DO UPDATE SET balance = ?`, [guildId, oderId, newBalance, newBalance] ); } ); }, intervalMs); voiceMoneyIntervals.set(key, intervalId); } // User left voice channel else if (!newState.channelId && oldState.channelId) { if (voiceMoneyIntervals.has(key)) { clearInterval(voiceMoneyIntervals.get(key)); voiceMoneyIntervals.delete(key); } voiceJoinTimes.delete(key); } } ); }, }; // ===== PRIVATE ROOM HANDLER ===== async function handlePrivateRoom(client, oldState, newState) { const guildId = newState.guild.id; const member = newState.member; // Récupérer la configuration const config = await db.getAsync( "SELECT enabled, creator_channel_id, category_id, channel_name_format FROM privateroom_config WHERE guild_id = ?", [guildId] ); if (!config || !config.enabled) { // Même si désactivé, vérifier si un salon temp doit être supprimé await checkAndDeleteEmptyTempChannel(oldState); return; } // Utilisateur rejoint le salon créateur if (newState.channelId === config.creator_channel_id) { try { // Formater le nom du salon let channelName = config.channel_name_format || '🔊 Salon de {user}'; channelName = channelName .replace(/{user}/g, member.user.username) .replace(/{displayname}/g, member.displayName); // Créer le salon vocal const newChannel = await newState.guild.channels.create({ name: channelName, type: ChannelType.GuildVoice, parent: config.category_id || null, permissionOverwrites: [ { id: member.id, allow: [ PermissionFlagsBits.ManageChannels, PermissionFlagsBits.MuteMembers, PermissionFlagsBits.DeafenMembers, PermissionFlagsBits.MoveMembers, PermissionFlagsBits.Connect, PermissionFlagsBits.Speak ] } ] }); // Enregistrer le salon dans la base de données await new Promise((resolve, reject) => { db.run( "INSERT INTO temp_voice_channels (channel_id, guild_id, owner_id, created_at) VALUES (?, ?, ?, ?)", [newChannel.id, guildId, member.id, Date.now()], (err) => { if (err) reject(err); else resolve(); } ); }); // Déplacer l'utilisateur dans le nouveau salon await member.voice.setChannel(newChannel); } catch (err) { console.error("Erreur création salon temporaire:", err); } } // Vérifier si l'ancien salon était un salon temporaire vide await checkAndDeleteEmptyTempChannel(oldState); } async function checkAndDeleteEmptyTempChannel(oldState) { if (!oldState.channelId) return; const oldChannel = oldState.guild.channels.cache.get(oldState.channelId); if (!oldChannel) return; // Vérifier si c'est un salon temporaire const tempChannel = await db.getAsync( "SELECT channel_id FROM temp_voice_channels WHERE channel_id = ?", [oldState.channelId] ); if (!tempChannel) return; // Vérifier si le salon est vide if (oldChannel.members.size === 0) { try { // Supprimer le salon await oldChannel.delete("Salon temporaire vide"); // Supprimer de la base de données db.run( "DELETE FROM temp_voice_channels WHERE channel_id = ?", [oldState.channelId] ); } catch (err) { console.error("Erreur suppression salon temporaire:", err); // Si le salon n'existe plus, le supprimer quand même de la DB db.run( "DELETE FROM temp_voice_channels WHERE channel_id = ?", [oldState.channelId] ); } } } // ===== VOICE TIME TRACKING ===== async function trackVoiceTime(guildId, userId, oldState, newState) { const today = new Date().toISOString().split('T')[0]; // YYYY-MM-DD // User joined voice channel if (newState.channelId && !oldState.channelId) { // Save join timestamp db.run( `INSERT OR REPLACE INTO voice_sessions (guild_id, user_id, join_timestamp) VALUES (?, ?, ?)`, [guildId, userId, Date.now()] ); } // User left voice channel else if (!newState.channelId && oldState.channelId) { // Get join timestamp and calculate duration const session = await db.getAsync( "SELECT join_timestamp FROM voice_sessions WHERE guild_id = ? AND user_id = ?", [guildId, userId] ); if (session) { const durationMs = Date.now() - session.join_timestamp; const durationSeconds = Math.floor(durationMs / 1000); // Add voice time to stats (in seconds) db.run( `INSERT INTO user_activity_stats (guild_id, user_id, stat_type, value, date) VALUES (?, ?, 'voice_time', ?, ?) ON CONFLICT(guild_id, user_id, stat_type, date) DO UPDATE SET value = value + ?`, [guildId, userId, durationSeconds, today, durationSeconds] ); // Delete session db.run( "DELETE FROM voice_sessions WHERE guild_id = ? AND user_id = ?", [guildId, userId] ); } } // User switched channels (still in voice) else if (newState.channelId && oldState.channelId && newState.channelId !== oldState.channelId) { // Update session timestamp (continue tracking) // No action needed, session continues } } // ===== VOICE LOGS ===== async function handleVoiceLogs(client, oldState, newState) { // Ignorer les bots if (newState.member?.user?.bot) return; const guild = newState.guild; const member = newState.member; if (!member) return; // Utilisateur rejoint un salon vocal if (newState.channelId && !oldState.channelId) { await sendLog(client, guild.id, 'voice', { action: 'join', title: '🔊 Connexion vocale', description: `**${member.user.tag}** a rejoint un salon vocal.`, fields: [ { name: '👤 Membre', value: `${member} (${member.user.tag})`, inline: true }, { name: '🔊 Salon', value: `${newState.channel}`, inline: true } ], thumbnail: member.user.displayAvatarURL({ size: 128 }), user: member.user }); } // Utilisateur quitte un salon vocal else if (!newState.channelId && oldState.channelId) { await sendLog(client, guild.id, 'voice', { action: 'leave', title: '🔇 Déconnexion vocale', description: `**${member.user.tag}** a quitté un salon vocal.`, fields: [ { name: '👤 Membre', value: `${member} (${member.user.tag})`, inline: true }, { name: '🔊 Salon', value: `${oldState.channel?.name || 'Inconnu'}`, inline: true } ], thumbnail: member.user.displayAvatarURL({ size: 128 }), user: member.user }); } // Utilisateur change de salon else if (newState.channelId && oldState.channelId && newState.channelId !== oldState.channelId) { await sendLog(client, guild.id, 'voice', { action: 'move', title: '🔀 Changement de salon', description: `**${member.user.tag}** a changé de salon vocal.`, fields: [ { name: '👤 Membre', value: `${member} (${member.user.tag})`, inline: true }, { name: '📤 Ancien salon', value: `${oldState.channel?.name || 'Inconnu'}`, inline: true }, { name: '📥 Nouveau salon', value: `${newState.channel}`, inline: true } ], thumbnail: member.user.displayAvatarURL({ size: 128 }), user: member.user }); } // Vérifier les changements de mute/deafen serveur if (oldState.serverMute !== newState.serverMute) { await sendLog(client, guild.id, 'voice', { action: newState.serverMute ? 'timeout' : 'untimeout', title: newState.serverMute ? '🔇 Mute serveur activé' : '🔊 Mute serveur désactivé', description: `**${member.user.tag}** a été ${newState.serverMute ? 'muté' : 'démuté'} par le serveur.`, fields: [ { name: '👤 Membre', value: `${member} (${member.user.tag})`, inline: true }, { name: '🔊 Salon', value: newState.channel ? `${newState.channel}` : 'N/A', inline: true } ], thumbnail: member.user.displayAvatarURL({ size: 128 }), user: member.user }); } if (oldState.serverDeaf !== newState.serverDeaf) { await sendLog(client, guild.id, 'voice', { action: newState.serverDeaf ? 'timeout' : 'untimeout', title: newState.serverDeaf ? '🔇 Sourd serveur activé' : '🔊 Sourd serveur désactivé', description: `**${member.user.tag}** a été rendu ${newState.serverDeaf ? 'sourd' : 'non-sourd'} par le serveur.`, fields: [ { name: '👤 Membre', value: `${member} (${member.user.tag})`, inline: true }, { name: '🔊 Salon', value: newState.channel ? `${newState.channel}` : 'N/A', inline: true } ], thumbnail: member.user.displayAvatarURL({ size: 128 }), user: member.user }); } }