DiscordAPIError

I've recently begun programming using discord.js and when I try to deploy my commands I get this error:
"C:\Program Files\nodejs\node.exe" C:\Users\user\IdeaProjects\bot\src\deploy-commands.js
node:fs:1507
const result = binding.readdir(
^

Error: ENOTDIR: not a directory, scandir 'C:\Users\user\IdeaProjects\bot\src\commands\loop.command.js'
at Object.readdirSync (node:fs:1507:26)
at Object.<anonymous> (C:\Users\user\IdeaProjects\bot\src\deploy-commands.js:14:29)
at Module._compile (node:internal/modules/cjs/loader:1358:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
at Module.load (node:internal/modules/cjs/loader:1208:32)
at Module._load (node:internal/modules/cjs/loader:1024:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
at node:internal/main/run_main_module:28:49 {
errno: -4052,
code: 'ENOTDIR',
syscall: 'scandir',
path: 'C:\\Users\\user\\IdeaProjects\\bot\\src\\commands\\loop.command.js'
Node.js v20.16.0

Process finished with exit code 1
"C:\Program Files\nodejs\node.exe" C:\Users\user\IdeaProjects\bot\src\deploy-commands.js
node:fs:1507
const result = binding.readdir(
^

Error: ENOTDIR: not a directory, scandir 'C:\Users\user\IdeaProjects\bot\src\commands\loop.command.js'
at Object.readdirSync (node:fs:1507:26)
at Object.<anonymous> (C:\Users\user\IdeaProjects\bot\src\deploy-commands.js:14:29)
at Module._compile (node:internal/modules/cjs/loader:1358:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
at Module.load (node:internal/modules/cjs/loader:1208:32)
at Module._load (node:internal/modules/cjs/loader:1024:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
at node:internal/main/run_main_module:28:49 {
errno: -4052,
code: 'ENOTDIR',
syscall: 'scandir',
path: 'C:\\Users\\user\\IdeaProjects\\bot\\src\\commands\\loop.command.js'
Node.js v20.16.0

Process finished with exit code 1
5 Replies
d.js toolkit
d.js toolkit4mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Trolli
TrolliOP4mo ago
my code (directly copied from the discord.js guide website):
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 folders 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.applicationCommands(clientId),
{ 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 folders 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.applicationCommands(clientId),
{ 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);
}
})();
discord js version: 14.15.3 node version: 20.16.0
NyR
NyR4mo ago
You are reading a file as a directory. Do note that if you are following our guides, the guide expects the commands file to be inside subdirectories in the "commands" folder
Trolli
TrolliOP4mo ago
this is my folder layout
No description
Trolli
TrolliOP4mo ago
now thers the error:
DiscordAPIError[20012]: You are not authorized to perform this action on this application
at handleErrors (C:\Users\docto\IdeaProjects\drip.bot\node_modules\@discordjs\rest\dist\index.js:730:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (C:\Users\docto\IdeaProjects\drip.bot\node_modules\@discordjs\rest\dist\index.js:1133:23)
at async SequentialHandler.queueRequest (C:\Users\docto\IdeaProjects\drip.bot\node_modules\@discordjs\rest\dist\index.js:963:14)
at async _REST.request (C:\Users\docto\IdeaProjects\drip.bot\node_modules\@discordjs\rest\dist\index.js:1278:22)
at async C:\Users\docto\IdeaProjects\drip.bot\src\deploy-commands.js:36:22 {
requestBody: {
files: undefined,
json: [ [Object], [Object], [Object], [Object] ]
},
rawError: {
message: 'You are not authorized to perform this action on this application',
code: 20012
},
code: 20012,
status: 403,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/952185851218120804/commands'
}

Process finished with exit code 0
DiscordAPIError[20012]: You are not authorized to perform this action on this application
at handleErrors (C:\Users\docto\IdeaProjects\drip.bot\node_modules\@discordjs\rest\dist\index.js:730:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (C:\Users\docto\IdeaProjects\drip.bot\node_modules\@discordjs\rest\dist\index.js:1133:23)
at async SequentialHandler.queueRequest (C:\Users\docto\IdeaProjects\drip.bot\node_modules\@discordjs\rest\dist\index.js:963:14)
at async _REST.request (C:\Users\docto\IdeaProjects\drip.bot\node_modules\@discordjs\rest\dist\index.js:1278:22)
at async C:\Users\docto\IdeaProjects\drip.bot\src\deploy-commands.js:36:22 {
requestBody: {
files: undefined,
json: [ [Object], [Object], [Object], [Object] ]
},
rawError: {
message: 'You are not authorized to perform this action on this application',
code: 20012
},
code: 20012,
status: 403,
method: 'PUT',
url: 'https://discord.com/api/v10/applications/952185851218120804/commands'
}

Process finished with exit code 0
Want results from more Discord servers?
Add your server