【问题标题】:Javascript Discord Bot Error (Function Error)Javascript Discord Bot 错误(功能错误)
【发布时间】:2021-07-15 23:19:15
【问题描述】:

添加重新加载命令后,我总是收到一条错误消息。 如果没有重新加载命令,机器人可以使用所有命令而不会出现任何问题 如果没有重新加载命令,机器人可以使用所有命令而不会出现任何问题 我就是找不到错误

错误信息:

undefined commands found.
/home/runner/Discord-BreakpointBot/index.js:19
   delete require.cache[require.resolve(`./commands/${f}`)]; 
                               ^

TypeError: require.resolve is not a function

代码:

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

希望有人能帮忙,谢谢

【问题讨论】:

  • 您确定您的脚本中有require 依赖项吗?
  • 是的,它已添加到包中
  • 根据错误,require 没有resolve 函数。

标签: javascript discord discord.js


【解决方案1】:

我不能 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;
        }
    }

这将递归地搜索你在调用函数时指定的文件夹,然后将其存储在fileArray中。

对于您遇到的错误,此解决方案比精确解决方案更像是一种替代方案

【讨论】:

    猜你喜欢
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 2019-11-10
    • 2018-05-02
    • 2021-07-04
    • 2019-02-03
    • 2018-03-12
    相关资源
    最近更新 更多