javascript - 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);
}
}
})
解决方案
所以你做错的是,你想在你的代码中包含 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);
}
}
})
推荐阅读
- algorithm - 是否有用于将线列表更改为点列表的 log(n) 算法?
- java - 如何将文本附加到文件?
- rtsp - 如何在 android 上的 Exoplayer RTSP 直播中获得低延迟?
- python - 如何从文本文件中拆分(和存储)数据?
- macos - Mac OS X 应用程序运行 shell 脚本但没有终端窗口
- indexing - 无效的输入“)”:预期的空白或变量正在创建索引
- reactjs - 无法在 React Native 项目(功能组件)中重新渲染视图
- c# - 如何在另一个 IOptions<> 重新加载时动态重新加载 IOptions<>
- python - 如何将 20 多个图形从 jupyter notebook 绘制到 html?
- python - 如何为字典中创建的每个键创建随机值