mirror of
https://github.com/arthur-pbty/LazyBot.git
synced 2026-06-03 15:07:29 +02:00
225 lines
7.4 KiB
JavaScript
225 lines
7.4 KiB
JavaScript
const { Events, ChannelType, PermissionFlagsBits } = require("discord.js");
|
|
const db = require("../db");
|
|
|
|
// 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);
|
|
|
|
if (newState.member.user.bot) return;
|
|
|
|
const guildId = newState.guild.id;
|
|
const oderId = newState.member.id;
|
|
const key = `${guildId}_${oderId}`;
|
|
|
|
// ===== 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]
|
|
);
|
|
}
|
|
}
|
|
}
|