Home > Blockchain >  How do I fix this error: "TypeError: Cannot read properties of undefined (reading 'toLower
How do I fix this error: "TypeError: Cannot read properties of undefined (reading 'toLower

Time:10-26

This error wasn't a problem until today. Every time I would run my discordjs bot, it gives me a TypeError saying "Cannot read properties of undefined (reading 'toLowerCase')"

Heres my code

const { readdirSync } = require('fs');
const { Collection } = require('discord.js');



client.commands = new Collection();

const events = readdirSync('./events/').filter(file => file.endsWith('.js'));

console.log(`Loading events...`);

for (const file of events) {
    const event = require(`../events/${file}`);
    console.log(`-> Loaded event ${file.split('.')[0]}`);
    client.on(file.split('.')[0], event.bind(null, client));
    delete require.cache[require.resolve(`../events/${file}`)];
};

console.log(`Loading commands...`);

readdirSync('./commands/').forEach(dirs => {
    const commands = readdirSync(`./commands/${dirs}`).filter(files => files.endsWith('.js'));

    for (const file of commands) {
        const command = require(`../commands/${dirs}/${file}`);
        console.log(`-> Loaded command ${command.name.toLowerCase()}`);
        client.commands.set(command.name.toLowerCase(), command);
        delete require.cache[require.resolve(`../commands/${dirs}/${file}`)];
    };
});

when I run the bot, the output goes like this

Loading events...
-> Loaded event interactionCreate
-> Loaded event messageCreate
-> Loaded event ready
Loading commands...
TypeError: Cannot read properties of undefined (reading 'toLowerCase')
    at /home/runner/Saeki-Miyako-Bot/src/loader.js:26:55
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/home/runner/Saeki-Miyako-Bot/src/loader.js:21:28)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/home/runner/Saeki-Miyako-Bot/main.js:43:1)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! music-bot@12.22.6 start: `node main.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the music-bot@12.22.6 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2021-10-26T03_01_19_546Z-debug.log
exit status 1

CodePudding user response:

That means your commands (importing files) not all have the same structure (with property 'name') in it.

If the commands without name are not critical, you can filter out them:

for (const file of commands) {
  const command = require(`../commands/${dirs}/${file}`);
  if (!command || !command.name) {
    console.warn(`Name is missing in 'commands/${dirs}/${file}'`)
    continue;
  }

  console.log(`-> Loaded command ${command.name.toLowerCase()}`);
  client.commands.set(command.name.toLowerCase(), command);
  delete require.cache[require.resolve(`../commands/${dirs}/${file}`)];
};

Since I don't have an access to your file systems I can not see what content of the files is. But I assume the name is just missing in some of the file (command).

  • Related