const { Client, GatewayIntentBits, Collection, ActivityType, REST, Routes } = require("discord.js") const fs = require("node:fs") const path = require("path") const { Manager } = require("erela.js") const { __glob } = require("./global-variables") const { LogType } = require("../modules/sub-log") const { List } = require("./sub-list") const nodeFinder = require("./nodes-finder") const { platform } = require("node:os") const client = new Client({ intents:[GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMembers], }) const membersVoices = new Map() module.exports.DiscordBot = class { constructor(config, dlog) { dlog.step.init("d_init", "Démarrage du Bot Discord") init(dlog, config) } } function init(dlog, config) { client.commands = new Collection() client.dictator = false; dlog.step.init("d_get_commands", "Récupération des commandes en local depuis : " + __glob.COMMANDS) const commands = []; // Grab all the command files from the commands directory you created earlier const commandsPath = __glob.COMMANDS const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); for (const file of commandFiles) { const command = require(commandsPath + path.sep + file); client.commands.set(command.data.name, command) commands.push(command.data.toJSON()); } dlog.step.end("d_get_commands") const rest = new REST().setToken(config.token); (async () => { try { dlog.step.init("d_commands_refresh", `Refreshing ${commands.length} application (/) commands.`); // The put method is used to fully refresh all commands in the guild with the current set const data = await rest.put( Routes.applicationGuildCommands("1094727789682380922", "137291455336022018"), { body: commands }, ); dlog.log("COMMANDS : Sended to Discord [" + data.length + "]") dlog.step.end("d_commands_refresh") } catch (error) { // And of course, make sure you catch and log any errors! dlog.error(error) } })(); rest.on("rateLimited", (datawarn) => { dlog.warn("REST - Limite de requête atteinte ! TimeToReset : " + datawarn.timeToReset); }) client.once("ready", () => { dlog.log("Connexion au Bot Discord réussi ! Connecté à : " + client.user.username + "#" + client.user.discriminator) client.user.setPresence({ activities: [{ name: `toutes les musiques possible !`, type: ActivityType.Listening }], status: 'online', }); client.manager.init(client.user.id); const commandManager = client.application.commands; if (!commandManager) { dlog.error('Command manager not available.'); } else { commandManager.set([]); } dlog.step.end("d_init") }) client.on("interactionCreate", (interaction) => { if(!interaction.isCommand()) return; const command = client.commands.get(interaction.commandName) try { dlog.log(interaction.member.user.username + "-> /" + interaction.commandName) command.execute(client, interaction) } catch(error) { dlog.error(interaction.member.user.username + "-> /" + interaction.commandName + " : ERREUR RENCONTRE") dlog.error(error) interaction.reply({content:"Erreur lors de l'éxécution de la commande !", ephemeral: true}) } }) startErelaManager(dlog, config) client.login(config.token) } function startErelaManager(dlog, config) { const elog = new LogType("Lavalink-Manager") client.on("voiceStateUpdate", (oldMember, newMember) => { membersVoices.set(newMember.id, newMember.channelId) let player = client.manager.players.get(oldMember.guild.id) if(player) { client.channels.fetch(player.options.voiceChannel).then(channel => { if(channel.members.size <= 1) { player.destroy() } }) } }) const nodes = nodeFinder.getNodes() client.manager = new Manager({ // The nodes to connect to, optional if using default lavalink options nodes, // Method to send voice data to Discord send: (id, payload) => { const guild = client.guilds.cache.get(id); // NOTE: FOR ERIS YOU NEED JSON.stringify() THE PAYLOAD if (guild) guild.shard.send(payload); } }); const plog = new LogType("Lavalink-Player") client.on("voiceStateUpdate", (oldMember, newMember) => { membersVoices.set(newMember.id, newMember.channelId) let player = client.manager.players.get(oldMember.guild.id) if(player) { client.channels.fetch(player.options.voiceChannel).then(channel => { if(channel.members.size <= 1) { player.destroy() plog.log("[Automatic Task] Player supprimé dans : " + channel.name) } }) } }) const list = new List() client.manager.on("playerCreate", (player) => { client.channels.fetch(player.options.voiceChannel).then(channel => { plog.log("Nouveau Player instancié dans : " + channel.name) }) }) client.manager.on("playerDestroy", (player) => { list.destroy() client.channels.fetch(player.options.voiceChannel).then(channel => { plog.log("Player supprimé dans : " + channel.name) }) }) client.manager.on("trackStart", (song) => { plog.log("Lecture de '" + song.queue.current.title + "' de '" + song.queue.current.author + "'") list.setCurrent(song) }) client.manager.on("queueEnd", () => { let player = client.manager.players.get("137291455336022018") if(player) { list.passCurrent() if(list.haveSongs()) { player.play(list.next()) } } }) // Emitted whenever a node connects client.manager.on("nodeConnect", node => { elog.log(`Connecté au serveur Lavalink : "${node.options.identifier}"` ) }) // Emitted whenever a node encountered an error client.manager.on("nodeError", (node, error) => { elog.warn(`Node "${node.options.identifier}" encountered an error: ${error.message}.`) }) // THIS IS REQUIRED. Send raw events to Erela.js client.on("raw", d => client.manager.updateVoiceState(d)); }