Error: ENOTDIR: not a directory, scandir 'C:\Users\k_der\developing\JS\bots\commands\ping.js'

Hi, so i'm making my first bot in JS. Now i want to deploy the slash commands inside the DIR commands. But it gives the error "Error: ENOTDIR: not a directory, scandir 'C:\Users\k_der\developing\JS\bots\commands\ping.js'" When i'm running deploy-commands.js. Who can help me? Script i use:
const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
// Grab all the command files from the commands directory you created earlier
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken(token);

// and deploy your commands!
(async () => {
try {
console.log(`Started 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(clientId, guildId),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
// Grab all the command files from the commands directory you created earlier
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken(token);

// and deploy your commands!
(async () => {
try {
console.log(`Started 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(clientId, guildId),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
22 Replies
d.js toolkit
d.js toolkit14mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
KelvinCodes
KelvinCodes14mo ago
discord.js@14.9.0 Node v18.16.0 Full stack trace:
PS C:\Users\k_der\developing\JS\bots> node deploy-commands.js
node:internal/fs/utils:347
throw err;
^

Error: ENOTDIR: not a directory, scandir 'C:\Users\k_der\developing\JS\bots\commands\ping.js'
at Object.readdirSync (node:fs:1452:3)
at Object.<anonymous> (C:\Users\k_der\developing\JS\bots\deploy-commands.js:14:26)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47 {
errno: -4052,
syscall: 'scandir',
code: 'ENOTDIR',
path: 'C:\\Users\\k_der\\developing\\JS\\bots\\commands\\ping.js'
}
PS C:\Users\k_der\developing\JS\bots> node deploy-commands.js
node:internal/fs/utils:347
throw err;
^

Error: ENOTDIR: not a directory, scandir 'C:\Users\k_der\developing\JS\bots\commands\ping.js'
at Object.readdirSync (node:fs:1452:3)
at Object.<anonymous> (C:\Users\k_der\developing\JS\bots\deploy-commands.js:14:26)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47 {
errno: -4052,
syscall: 'scandir',
code: 'ENOTDIR',
path: 'C:\\Users\\k_der\\developing\\JS\\bots\\commands\\ping.js'
}
Kinect3000
Kinect300014mo ago
You tried to call readdir on commands/ping.js, which is a file, not a directory
KelvinCodes
KelvinCodes14mo ago
That part i get
KelvinCodes
KelvinCodes14mo ago
But why does the official code do that? I got the deploy-commands.js from: https://discordjs.guide/creating-your-bot/command-deployment.html#global-commands
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
KelvinCodes
KelvinCodes14mo ago
Or need to be a folder inside the commands dir?
Kinect3000
Kinect300014mo ago
The official guide doesn’t put commands into categories like that Or ig it does Thinkeng
KelvinCodes
KelvinCodes14mo ago
That's why i'm a lil bit confused
Kinect3000
Kinect300014mo ago
If you don’t want that, you can just reduce it to a single readdir Remove the const commands and for (const folder of …) and define const folder = "<the commands folder>"
KelvinCodes
KelvinCodes14mo ago
I have also const commandsPath = path.join(foldersPath, folder); I think i can remove that also right? Because i dont need to join aditional folders
Kinect3000
Kinect300014mo ago
Define the commands folder path there instead Remove the const folder That should work
KelvinCodes
KelvinCodes14mo ago
Hmm okey!
KelvinCodes
KelvinCodes14mo ago
I'm now getting another error "missing access" But i have enabled the "application.commands" scope and reinvited the Discord bot
[Running] node "c:\Users\k_der\developing\JS\bots\deploy-commands.js"
Started refreshing 3 application (/) commands.
DiscordAPIError[50001]: Missing Access
at handleErrors (c:\Users\k_der\developing\JS\node_modules\@discordjs\rest\dist\index.js:640:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (c:\Users\k_der\developing\JS\node_modules\@discordjs\rest\dist\index.js:1021:23)
at async SequentialHandler.queueRequest (c:\Users\k_der\developing\JS\node_modules\@discordjs\rest\dist\index.js:862:14)
at async REST.request (c:\Users\k_der\developing\JS\node_modules\@discordjs\rest\dist\index.js:1387:22)
at async c:\Users\k_der\developing\JS\bots\deploy-commands.js:31:16 {
requestBody: { files: undefined, json: [ [Object], [Object], [Object] ] },
rawError: { message: 'Missing Access', code: 50001 },
code: 50001,
status: 403,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/1099710952766705794/guilds/002208148930691172/commands'
}
[Running] node "c:\Users\k_der\developing\JS\bots\deploy-commands.js"
Started refreshing 3 application (/) commands.
DiscordAPIError[50001]: Missing Access
at handleErrors (c:\Users\k_der\developing\JS\node_modules\@discordjs\rest\dist\index.js:640:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (c:\Users\k_der\developing\JS\node_modules\@discordjs\rest\dist\index.js:1021:23)
at async SequentialHandler.queueRequest (c:\Users\k_der\developing\JS\node_modules\@discordjs\rest\dist\index.js:862:14)
at async REST.request (c:\Users\k_der\developing\JS\node_modules\@discordjs\rest\dist\index.js:1387:22)
at async c:\Users\k_der\developing\JS\bots\deploy-commands.js:31:16 {
requestBody: { files: undefined, json: [ [Object], [Object], [Object] ] },
rawError: { message: 'Missing Access', code: 50001 },
code: 50001,
status: 403,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/1099710952766705794/guilds/002208148930691172/commands'
}
Do you know what i can check more?
Kinect3000
Kinect300014mo ago
Use the url that was generated and authorize the bot to the guild
KelvinCodes
KelvinCodes14mo ago
I did do that xd
Kinect3000
Kinect300014mo ago
Show bot in the integrations tab of the guild
KelvinCodes
KelvinCodes14mo ago
I did kicked bot, copy this url in the browser
KelvinCodes
KelvinCodes14mo ago
Kinect3000
Kinect300014mo ago
Is the guild id correct?
KelvinCodes
KelvinCodes14mo ago
Good one, gonna check
Kinect3000
Kinect300014mo ago
It’s not common to have a guild id that starts w/ 00
KelvinCodes
KelvinCodes14mo ago
Uhhm no 🥺 whoops That was the issue! Thank you so mutch! I did make that he goed trough every file tho, for ez use:
const { REST, Routes } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];

const foldersPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(foldersPath).filter(file => file.endsWith('.js'));


// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(foldersPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken("T");

// and deploy your commands!
(async () => {
try {
console.log(`Started 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("1099710952766705794", "1002208148930691172"),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
const { REST, Routes } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];

const foldersPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(foldersPath).filter(file => file.endsWith('.js'));


// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(foldersPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken("T");

// and deploy your commands!
(async () => {
try {
console.log(`Started 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("1099710952766705794", "1002208148930691172"),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
I got: Started refreshing 3 application (/) commands. Successfully reloaded 3 application (/) commands. Jeeej You're a hero for your help!