I am trying to make this work, but it keeps saying that my directory
for the folder is wrong, when it is correct. I had help from another who couldn't solve this. Can anyone help?
Code:
const { Client, Intents, Collection } = require('discord.js')
const { REST } = require('@discordjs/rest')
const { Routes } = require('discord-api-types/v9')
const Discord = require('discord.js')
const fs = require('fs')
const intents = new Discord.Intents(32767);
const client = new Discord.Client({ intents });
const config = require('./Data/config.json')
const versionNumber = "V1.0.0"
client.once("ready", () => {
console.log('-------------------------------------------');
console.log(`| Successfully logged in as Logic RP#7590 |`);
console.log('-------------------------------------------');
const commands = []
const commands_information = new Collection();
const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js"))
for (const file of commandFiles) {
const command = require(`./commands/${file}`)
console.log(`Command loaded: ${command.data.name}`)
commands.push(command.data.toJSON())
commands_information.set(command.data.name, command);
}
const rest = new REST({ version: '9' }).setToken(config.token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(config.botConfiguration.client, config.botConfiguration.guild),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (!commands_information.has(commandName)) return;
try {
await commands_information.get(commandName).execute(client, interaction, config);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
})
})
client.on("guildMemberAdd", async (member) => {
let members = client.guilds.cache.reduce((a, g) => a + g.memberCount, 0);
console.log(`${member.user.tag} has joined Logic RP`);
console.log('-------------------------------------------');
const embed = new Discord.MessageEmbed()
.setTitle(`Welcome to the ${member.guild.name} Discord server!`)
.setThumbnail(member.user.displayAvatarURL({ dynamic: true }))
.setColor('GREEN')
.addFields(
{
name: "✅ Have fun!",
value: "The most important thing for us is that YOU have fun! You can chat with others in <#898349995315576883> and have all the fun that you want or talk to the community.",
inline: false
},
{
name: "📘 Check out our rules!",
value: "If you'd like to stay in this server, we ask that you read and follow all of our rules in <#898346754926338048>. This is important.",
inline: false
},
{
name: "🔔 Get your roles!",
value: "If you'd like to get notified for certain events or things related to this server, please check out <#898350339571462155> and get the roles that you want.",
inline: false
},
)
.setFooter(`Logic RP | ${versionNumber} - Member #${members}`)
.setTimestamp()
client.channels.cache.get('898346590245363772').send({embeds: [embed]});
})
client.login(config.token);
//npm run dev
Error:
-------------------------------------------
| Successfully logged in as Logic RP#7590 |
-------------------------------------------
node:internal/fs/utils:344
throw err;
^
Error: ENOENT: no such file or directory, scandir './commands'
at Object.readdirSync (node:fs:1390:3)
at Client.<anonymous> (C:\Users\name\Desktop\Logic RP Discord Bot\src\index.js:21:29)
at Object.onceWrapper (node:events:510:26)
at Client.emit (node:events:390:28)
at WebSocketManager.triggerClientReady (C:\Users\name\Desktop\Logic RP Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:383:17)
at WebSocketManager.checkShardsReady (C:\Users\name\Desktop\Logic RP Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:366:10)
at WebSocketShard.<anonymous> (C:\Users\name\Desktop\Logic RP Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:188:14)
at WebSocketShard.emit (node:events:390:28)
at WebSocketShard.checkReady (C:\Users\name\Desktop\Logic RP Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:474:12)
at WebSocketShard.onPacket (C:\Users\name\Desktop\Logic RP Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:446:16) {
errno: -4058,
syscall: 'scandir',
code: 'ENOENT',
path: './commands'
}
The problem here is fs
takes the directory where package.json
is present as its base. and from your code fs
is trying to find a folder named commands
in the same level as the src
folder which doesn't exist.
so your corrected directory would be
fs.readdirSync("./src/commands");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.