首页 > 解决方案 > const commandFolders = readdirSync('./commands'); ReferenceError:在对象初始化之前无法访问“readdirSync”。错误

问题描述

我今天遇到了这个错误,试图从 youtube 视频中更改我的命令处理程序的一点点。视频来自 4 月,它似乎对这个人有用,但对我来说却没有。我尝试了很多东西,但我没有得到任何线索。我对此不太好,但我想像许多其他人一样开始,这个社区非常好,对我帮助很大,非常感谢你为我提供的支持!!我希望我能克服这个问题。 在此处输入图像描述





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); 
         }
     }
})

标签: javascriptnpmdiscordbots

解决方案


所以你做错的是,你想在你的代码中包含 fs 库之前使用 readdirSync 。只需将 require 语句移到 readdirSync 上,它应该可以工作:

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); 
         }
     }
})

推荐阅读