@@ -4,57 +4,77 @@ require('dotenv').config();
44
55const path = require ( 'path' ) ;
66const fs = require ( 'fs' ) ;
7- const { REST , Routes } = require ( 'discord.js' ) ;
8-
9- const commands = [ ] ;
10-
11- const commandDirs = [
12- path . join ( __dirname , 'commands' , 'player' ) ,
13- path . join ( __dirname , 'commands' , 'inventory' ) ,
14- path . join ( __dirname , 'commands' , 'dungeon' ) ,
15- path . join ( __dirname , 'commands' , 'admin' ) ,
16- ] ;
17-
18- for ( const dir of commandDirs ) {
19- if ( ! fs . existsSync ( dir ) ) continue ;
20- const files = fs . readdirSync ( dir ) . filter ( ( f ) => f . endsWith ( '.js' ) ) ;
21- for ( const file of files ) {
22- const command = require ( path . join ( dir , file ) ) ;
23- if ( command . data ) {
24- commands . push ( command . data . toJSON ( ) ) ;
25- console . log ( `[Deploy] Préparé: ${ command . data . name } ` ) ;
7+ const { REST , Routes, Client, GatewayIntentBits } = require ( 'discord.js' ) ;
8+
9+ function loadCommands ( ) {
10+ const commands = [ ] ;
11+ const commandDirs = [
12+ path . join ( __dirname , 'commands' , 'player' ) ,
13+ path . join ( __dirname , 'commands' , 'inventory' ) ,
14+ path . join ( __dirname , 'commands' , 'dungeon' ) ,
15+ path . join ( __dirname , 'commands' , 'admin' ) ,
16+ ] ;
17+
18+ for ( const dir of commandDirs ) {
19+ if ( ! fs . existsSync ( dir ) ) continue ;
20+ const files = fs . readdirSync ( dir ) . filter ( ( f ) => f . endsWith ( '.js' ) ) ;
21+ for ( const file of files ) {
22+ const command = require ( path . join ( dir , file ) ) ;
23+ if ( command . data ) {
24+ commands . push ( command . data . toJSON ( ) ) ;
25+ console . log ( `[Deploy] Préparé: ${ command . data . name } ` ) ;
26+ }
2627 }
2728 }
29+ return commands ;
2830}
2931
30- const rest = new REST ( ) . setToken ( process . env . DISCORD_TOKEN ) ;
31-
32- // Guild IDs for instant deploy (development). Leave empty to deploy globally.
33- const GUILD_IDS = ( process . env . DEPLOY_GUILD_IDS || '' ) . split ( ',' ) . map ( ( s ) => s . trim ( ) ) . filter ( Boolean ) ;
34-
35- ( async ( ) => {
36- try {
37- if ( GUILD_IDS . length > 0 ) {
38- // Guild-specific deploy: instantaneous, perfect for development
39- for ( const guildId of GUILD_IDS ) {
40- console . log ( `[Deploy] Publication sur le serveur ${ guildId } ...` ) ;
41- const data = await rest . put (
42- Routes . applicationGuildCommands ( process . env . DISCORD_CLIENT_ID , guildId ) ,
43- { body : commands } ,
44- ) ;
45- console . log ( `[Deploy] ${ data . length } commande(s) publiée(s) sur ${ guildId } .` ) ;
46- }
47- } else {
48- // Global deploy: up to 1 hour to propagate — use for production
49- console . log ( `[Deploy] Publication globale de ${ commands . length } commande(s)...` ) ;
50- const data = await rest . put (
51- Routes . applicationCommands ( process . env . DISCORD_CLIENT_ID ) ,
52- { body : commands } ,
53- ) ;
54- console . log ( `[Deploy] ${ data . length } commande(s) publiée(s) avec succès (propagation jusqu'à 1h).` ) ;
55- }
56- } catch ( error ) {
57- console . error ( '[Deploy] Erreur:' , error ) ;
58- process . exit ( 1 ) ;
32+ async function deployCommands ( client ) {
33+ const commands = loadCommands ( ) ;
34+ const rest = new REST ( ) . setToken ( process . env . DISCORD_TOKEN ) ;
35+ const guildIds = client . guilds . cache . map ( ( g ) => g . id ) ;
36+
37+ console . log ( `[Deploy] Déploiement sur ${ guildIds . length } serveur(s)...` ) ;
38+
39+ for ( const guildId of guildIds ) {
40+ const data = await rest . put (
41+ Routes . applicationGuildCommands ( process . env . DISCORD_CLIENT_ID , guildId ) ,
42+ { body : commands } ,
43+ ) ;
44+ console . log ( `[Deploy] ${ data . length } commande(s) publiée(s) sur ${ guildId } .` ) ;
5945 }
60- } ) ( ) ;
46+
47+ console . log ( '[Deploy] Déploiement terminé sur tous les serveurs.' ) ;
48+ }
49+
50+ // Standalone mode: node src/deploy-commands.js
51+ if ( require . main === module ) {
52+ ( async ( ) => {
53+ try {
54+ const client = new Client ( { intents : [ GatewayIntentBits . Guilds ] } ) ;
55+ await new Promise ( ( resolve , reject ) => {
56+ client . once ( 'ready' , resolve ) ;
57+ client . once ( 'error' , reject ) ;
58+ client . login ( process . env . DISCORD_TOKEN ) ;
59+ } ) ;
60+
61+ await deployCommands ( client ) ;
62+ await client . destroy ( ) ;
63+ } catch ( error ) {
64+ console . error ( '[Deploy] Erreur:' , error ) ;
65+ process . exit ( 1 ) ;
66+ }
67+ } ) ( ) ;
68+ }
69+
70+ async function deployToGuild ( guildId ) {
71+ const commands = loadCommands ( ) ;
72+ const rest = new REST ( ) . setToken ( process . env . DISCORD_TOKEN ) ;
73+ const data = await rest . put (
74+ Routes . applicationGuildCommands ( process . env . DISCORD_CLIENT_ID , guildId ) ,
75+ { body : commands } ,
76+ ) ;
77+ console . log ( `[Deploy] ${ data . length } commande(s) publiée(s) sur ${ guildId } .` ) ;
78+ }
79+
80+ module . exports = { deployCommands, deployToGuild } ;
0 commit comments