【问题标题】:const commandFolders = readdirSync('./commands'); ReferenceError: Cannot access 'readdirSync' before initialization at Object.<anonymous> errorconst commandFolders = readdirSync('./commands'); ReferenceError:在 Object.<anonymous> 初始化之前无法访问“readdirSync”错误
【发布时间】:2021-08-17 07:53:57
【问题描述】:

我今天遇到了这个错误,试图从一个 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); 
         }
     }
})

【问题讨论】:

    标签: javascript npm discord bots


    【解决方案1】:

    所以你做错了,你想在你的代码中包含 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); 
             }
         }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-29
      • 2021-09-10
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多