[英]Javascript Discord Bot Error (Function Error)
I always get an error message after adding the reload command.添加重新加载命令后,我总是收到一条错误消息。 Without the Reload Command, the bot works with all commands without any problems Without the Reload Command, the bot works with all commands without any problems I just can't find the mistake
没有重新加载命令,机器人可以毫无问题地处理所有命令 没有重新加载命令,机器人可以毫无问题地处理所有命令 我只是找不到错误
The error message:错误信息:
undefined commands found. /home/runner/Discord-BreakpointBot/index.js:19 delete require.cache[require.resolve(`./commands/${f}`)]; ^ TypeError: require.resolve is not a function
Code:代码:
function loadCmds () {
fs.readdir('./commands/', (err, files) => {
if(err) console.error(err);
var jsfiles = files.filter(f => f.split('.').pop() === 'js');
if (jsfiles.length <=0) {return console.log('No commands found...')}
else {console.log(jsfiles.lenght + ' commands found.')}
jsfiles.forEach((f,i) => {
delete require.cache[require.resolve(`./commands/${f}`)];
var cmds = require(`./commands/${f}`);
console.log(`Command ${f} loading...`);
bot.commands.set(cmds.config.command, cmds);
})
})
}
bot.on('message', message => {
var sender = message.author;
var msg = message.content.toUpperCase();
var prefix ='>'
var cont = message.content.slice(prefix.length).split(" ");
var args = cont.slice(1);
if (!message.content.startsWith(prefix)) return;
var cmd = bot.commands.get(cont[0])
if (cmd) cmd.run(bot, message, args);
if (msg === prefix + 'RELOAD') {
message.channel.send({embed:{description:"All Commands Reloaded"}})
message.channel.send('All Commands Reloaded')
loadCmds()
}
});
loadCmds();
// Bot Launched
bot.on('ready', () => {
console.log('Bot Launched...')
bot.user.setStatus('Online')
bot.user.setActivity('https://www.twitch.tv');
});
I hope someone can help, thanks我希望有人可以帮助,谢谢
I'm not 100% sure what causes your error
but I can offer you my method of retrieving comamnds, which works pretty well:我不是 100% 确定是什么导致了您的
error
,但我可以为您提供检索命令的方法,该方法效果很好:
const fileArray = [];
function readCommands(dir) {
const __dirname = rootDir;
// Read out all command files
const files = fs.readdirSync(path.join(__dirname, dir));
// Loop through all the files in ./commands
for (const file of files) {
// Get the status of 'file' (is it a file or directory?)
const stat = fs.lstatSync(path.join(__dirname, dir, file));
// If the 'file' is a directory, call the 'readCommands' function
// again with the path of the subdirectory
if (stat.isDirectory()) {
readCommands(path.join(dir, file));
}
else {
const fileDir = dir.replace('\\', '/');
fileArray.push(fileDir + '/' + file);
// fs.readdirSync(dir).filter(cmdFile => cmdFile.endsWith('.js'));
}
}
}
readCommands('commands');
for(const file of fileArray) {
const command = require(`../${file}`);
if(command.name) {
client.commands.set(command.name, command);
}
else {
continue;
}
}
This will recursively search the folder you specify when calling the function and then store it in fileArray
.这将递归搜索您在调用 function 时指定的文件夹,然后将其存储在
fileArray
中。
This solution is more an alternative than an exact solution to the error you are experiencing此解决方案比您遇到的错误的确切解决方案更多的是替代方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.