|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { Client, Collection, GatewayIntentBits, REST, Routes } = require('discord.js'); |
| 4 | +require('dotenv').config(); |
| 5 | +const { guildId } = require('./config.json'); |
| 6 | + |
| 7 | +// Initialisation du client |
| 8 | +const client = new Client({ intents: [GatewayIntentBits.Guilds] }); |
| 9 | + |
| 10 | +// Collection des commandes |
| 11 | +client.commands = new Collection(); |
| 12 | + |
| 13 | +// Chargement des commandes |
| 14 | +const commandsPath = path.join(__dirname, 'commands'); |
| 15 | +const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); |
| 16 | + |
| 17 | +for (const file of commandFiles) { |
| 18 | + const filePath = path.join(commandsPath, file); |
| 19 | + const command = require(filePath); |
| 20 | + client.commands.set(command.data.name, command); |
| 21 | +} |
| 22 | + |
| 23 | +// Chargement des événements |
| 24 | +const eventsPath = path.join(__dirname, 'events'); |
| 25 | +const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); |
| 26 | + |
| 27 | +for (const file of eventFiles) { |
| 28 | + const filePath = path.join(eventsPath, file); |
| 29 | + const event = require(filePath); |
| 30 | + if (event.once) { |
| 31 | + client.once(event.name, (...args) => event.execute(...args)); |
| 32 | + } else { |
| 33 | + client.on(event.name, (...args) => event.execute(...args)); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Enregistrement des commandes après l'événement 'ready' |
| 38 | +client.once('ready', async () => { |
| 39 | + console.log(`Connecté en tant que ${client.user.tag}`); |
| 40 | + |
| 41 | + const rest = new REST({ version: '10' }).setToken(process.env.TOKEN); |
| 42 | + |
| 43 | + try { |
| 44 | + console.log('Mise à jour des commandes slash...'); |
| 45 | + const commands = client.commands.map(command => command.data.toJSON()); |
| 46 | + |
| 47 | + await rest.put( |
| 48 | + Routes.applicationGuildCommands(client.user.id, guildId), |
| 49 | + { body: commands } |
| 50 | + ); |
| 51 | + |
| 52 | + console.log('Commandes slash enregistrées avec succès.'); |
| 53 | + } catch (error) { |
| 54 | + console.error('Erreur lors de l\'enregistrement des commandes slash :', error); |
| 55 | + } |
| 56 | +}); |
| 57 | + |
| 58 | +// Connexion du bot |
| 59 | +client.login(process.env.TOKEN); |
0 commit comments