Flepix
DIAdiscord.js - Imagine an app
•Created by Flepix on 4/18/2024 in #djs-questions
files not reading based on command handling (not a discord.js issue)
I'm new to djs and never made a bot with discord.js I'm facing an error with the code can anyone help me here's the code:-
Error:-
I code in replit I made the required folder and files but it still showing this error!
js
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const isDirectory = fs.lstatSync(foldersPath).isDirectory();
if (!isDirectory) {
console.error(`${foldersPath} is not a directory`);
process.exit(1);
}
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
// Log in to Discord with your client's token
client.login(token);
js
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const isDirectory = fs.lstatSync(foldersPath).isDirectory();
if (!isDirectory) {
console.error(`${foldersPath} is not a directory`);
process.exit(1);
}
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
// Log in to Discord with your client's token
client.login(token);
node:fs:1515
const result = binding.readdir(
Error: ENOTDIR: not a directory, scandir /home/runner/Nodejs-3/commands/ping.js' at Object.readdirSync (node:fs: 1515:26
)
at Object.<anonymous> (/home/runner/No dejs-3/index.js:15:26)
at Module._compile (node: internal/modu les/cjs/loader: 1376:14)
at Module._extensions..js (node:intern al/modules/cjs/loader: 1435:10)
at Module.load (node: internal/modules/ cjs/loader: 1207:32)
at Module._load (node:internal/modules /cjs/loader: 1023:12)
at Function.executeUserEntryPoint [as runMain] (node: internal/modules/run_main:1 35:12)
at node: internal/main/run_main_module: 28:49 {
errno: -20,
code: 'ENOTDIR',
syscall: 'scandir',
path: '/home/runner/Nodejs-3/commands/ping.js'
}
node:fs:1515
const result = binding.readdir(
Error: ENOTDIR: not a directory, scandir /home/runner/Nodejs-3/commands/ping.js' at Object.readdirSync (node:fs: 1515:26
)
at Object.<anonymous> (/home/runner/No dejs-3/index.js:15:26)
at Module._compile (node: internal/modu les/cjs/loader: 1376:14)
at Module._extensions..js (node:intern al/modules/cjs/loader: 1435:10)
at Module.load (node: internal/modules/ cjs/loader: 1207:32)
at Module._load (node:internal/modules /cjs/loader: 1023:12)
at Function.executeUserEntryPoint [as runMain] (node: internal/modules/run_main:1 35:12)
at node: internal/main/run_main_module: 28:49 {
errno: -20,
code: 'ENOTDIR',
syscall: 'scandir',
path: '/home/runner/Nodejs-3/commands/ping.js'
}
5 replies