1- const { REST , Routes } = require ( 'discord.js' ) ;
2- const config = require ( '../dist/config.js' ) ;
3- const fs = require ( 'node:fs' ) ;
4- const path = require ( 'node:path' ) ;
1+ import { REST , Routes } from 'discord.js' ;
2+ import { config } from "./config.js" ; // Not ready to do this yet
3+ import fs from 'node:fs' ;
4+ import path from 'node:path' ;
5+ import dotenv from 'dotenv' ;
6+ dotenv . config ( { path : path . join ( __dirname , '..' , '.env' ) } ) ;
7+
58
69const commands = [ ] ;
7- // Grab all the command files from the commands directory you created earlier
8- const foldersPath = path . join ( __dirname , 'commands' ) ;
9- const commandFolders = fs . readdirSync ( foldersPath ) ;
10-
11- for ( const folder of commandFolders ) {
12- // Grab all the command files from the commands directory you created earlier
13- const commandsPath = path . join ( foldersPath , folder ) ;
14- const commandFiles = fs . readdirSync ( commandsPath ) . filter ( file => file . endsWith ( '.js' ) ) ;
15- // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
16- for ( const file of commandFiles ) {
17- const filePath = path . join ( commandsPath , file ) ;
18- const command = require ( filePath ) ;
19- commands . push ( command . SlashCommand . command . toJSON ( ) ) ;
20- }
21- }
10+ const commandsDirectory = path . join ( __dirname , "commands" ) ;
2211
23- // Construct and prepare an instance of the REST module
24- const rest = new REST ( ) . setToken ( config . config . token ) ;
12+ const filterNonJsFiles = ( items : string [ ] ) => {
13+ return items . filter ( ( files ) => files . split ( "." ) . pop ( ) === "js" ) ;
14+ } ;
2515
26- // and deploy your commands!
27- ( async ( ) => {
28- try {
29- console . log ( `Started refreshing ${ commands . length } application (/) commands.` ) ;
16+ if ( ! fs . existsSync ( commandsDirectory ) ) {
17+ fs . mkdirSync ( commandsDirectory ) ;
18+ }
3019
31- // The put method is used to fully refresh all commands in the guild with the current set
32- const data = await rest . put (
33- config . config . production ? Routes . applicationCommands ( config . config . clientID ) : Routes . applicationGuildCommands ( config . config . clientID , config . config . guildID ) ,
34- { body : commands } ,
35- ) ;
20+ const items = fs . readdirSync ( commandsDirectory ) ;
21+ const baseFiles = filterNonJsFiles ( items ) ;
22+ const folders = items . filter ( ( files : string ) => files . split ( "." ) . pop ( ) != "js" ) ;
3623
37- console . log ( `Successfully reloaded ${ data . length } application (/) commands.` ) ;
38- } catch ( error ) {
39- // And of course, make sure you catch and log any errors!
40- console . error ( error ) ;
24+ for ( const baseFile of baseFiles ) {
25+ let properties = require ( path . join ( commandsDirectory , baseFile ) ) ;
26+ commands . push ( properties . SlashCommand . command . toJSON ( ) ) ;
27+ }
28+
29+ for ( const folderName of folders ) {
30+ const folderItems = fs . readdirSync ( path . join ( commandsDirectory , folderName ) ) ;
31+ const files = filterNonJsFiles ( folderItems ) ;
32+ for ( const file of files ) {
33+ let properties = require ( path . join ( commandsDirectory , folderName , file ) ) ;
34+ commands . push ( properties . SlashCommand . command . toJSON ( ) ) ;
4135 }
42- } ) ( ) ;
36+ }
37+
38+ const rest = new REST ( ) . setToken ( process . env . BOT_TOKEN ) ;
39+
40+ try {
41+ console . log ( `Started refreshing ${ commands . length } application (/) commands.` ) ;
42+ const data = rest . put (
43+ Routes . applicationGuildCommands ( process . env . CLIENT_ID , "926369143186403329" ) ,
44+ { body : commands } ,
45+ ) ;
46+ console . log ( `Successfully reloaded ${ commands . length } application (/) commands.` ) ;
47+ } catch ( error ) {
48+ console . error ( error ) ;
49+ }
0 commit comments