Vegert
Vegert

Reputation: 21

const commandFolders = readdirSync('./commands'); ReferenceError: Cannot access 'readdirSync' before initialization at Object.<anonymous> error

I got this error today trying to change a little bit of my command handler from an youtube video.The video is from april and it seems to work for the guy but for me it doesnt.I tried many things but i didnt get any clue.Im not pretty good with this but i want to have an start like many others and this comunity is so good an helps me so much, thank you so much for the support you offer to me!!I hope i will get past this problem. enter image description here





const Timeout = new Discord.Collection();


const bot = new Discord.Client(); 


const prefix = '$';

bot.commands = new Discord.Collection();
const commandFolders = readdirSync('./commands');


const ms = require('ms');

const {readdirSync , read} = require('fs');

for(const folder of commandsFolders){
    const commandFiles  = readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'));
    for(const file of commandFiles){
        const command = require(`./commands/${folder}/${files}`);
        bot.commands.set(command.name, command);
    }
}

bot.on("error" , console.error);


bot.once('ready' , () => {
    console.log('M-am trezit din morti!');
});

bot.on("message" , async (message) =>{
     if(message.author.bot) return;
     if(message.channel.type === 'dm') return;

     if(message.content.startsWith(prefix)){
         const args = message.content.slice(prefix.length).trim().split(/ +/);

         const commandName = args.shift().toLowerCase();

         const command = bot.command.get(commandName) || bot.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
         if(!command) return;

         if (command) {
             if(command.cooldown){
                 if(Timeout.has(`${command.name}${message.author.id}`)) return  message.channel.send(`Te rog asteapta! \`${ms(Timeout.get (`${command.name}${message.author.id}`) - Date.now(), {long: true})}\`Pana folosesti comanda din nou!`);
                 command.run(bot, message, args)
                 Timeout.set(`${command.name}${message.author.id}` , Date.now() + command.cooldown)
                 setTimeout(() =>{
                     Timeout.delete(`${coomand.name}${message.author.id}`)
                 }, command.cooldown)
             } else command.run(bot, message, args); 
         }
     }
})

Upvotes: 0

Views: 98

Answers (1)

WindxwsXP
WindxwsXP

Reputation: 36

So what you did wrong is, that you want to use readdirSync before you included the fs library in your code. Just move the require statement over the readdirSync and it should work:

const bot = new Discord.Client(); 


const prefix = '$';

const {readdirSync , read} = require('fs');

bot.commands = new Discord.Collection();
const commandFolders = readdirSync('./commands');


const ms = require('ms');


for(const folder of commandsFolders){
    const commandFiles  = readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'));
    for(const file of commandFiles){
        const command = require(`./commands/${folder}/${files}`);
        bot.commands.set(command.name, command);
    }
}

bot.on("error" , console.error);


bot.once('ready' , () => {
    console.log('M-am trezit din morti!');
});

bot.on("message" , async (message) =>{
    if(message.author.bot) return;
    if(message.channel.type === 'dm') return;

    if(message.content.startsWith(prefix)){
        const args = message.content.slice(prefix.length).trim().split(/ +/);

        const commandName = args.shift().toLowerCase();

        const command = bot.command.get(commandName) || bot.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
         if(!command) return;

         if (command) {
             if(command.cooldown){
                 if(Timeout.has(`${command.name}${message.author.id}`)) return  message.channel.send(`Te rog asteapta! \`${ms(Timeout.get (`${command.name}${message.author.id}`) - Date.now(), {long: true})}\`Pana folosesti comanda din nou!`);
                 command.run(bot, message, args)
                 Timeout.set(`${command.name}${message.author.id}` , Date.now() + command.cooldown)
                 setTimeout(() =>{
                     Timeout.delete(`${coomand.name}${message.author.id}`)
                 }, command.cooldown)
             } else command.run(bot, message, args); 
         }
     }
})

Upvotes: 2

Related Questions