Search code examples
javascriptnode.jsdiscord.js

TypeError: Cannot read properties of null (reading 'commands')


I've almost fixed my bot and now its giving this error :-

/Users/roopa/Desktop/projects/LLbot/src/handlers/commandHandler.js:27
    client.application.commands.set(commandsArray);
                       ^

TypeError: Cannot read properties of null (reading 'commands')
    at loadCommands (/Users/roopa/Desktop/projects/LLbot/src/handlers/commandHandler.js:27:24)
    at Object.<anonymous> (/Users/roopa/Desktop/projects/LLbot/src/bot.js:274:1)
    at Module._compile (node:internal/modules/cjs/loader:1255:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1309:10)
    at Module.load (node:internal/modules/cjs/loader:1113:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47

Node.js v20.2.0

My commandHandler.js code :-

function loadCommands(client) {
    const fs = require('fs');
    const ascii = require('ascii-table');
    const table = new ascii().setHeading('Commands', 'Status');

    let commandsArray = [];

    const commandsFolder = fs.readdirSync('/Users/roopa/Desktop/projects/LLbot/src/Commands');
    
    for (const folder of commandsFolder) {
        const commandFiles = fs.readdirSync(`/Users/roopa/Desktop/projects/LLbot/src/Commands/${folder}`).filter((file) => file.endsWith('.js'));

        for (const file of commandFiles) {
            const commandFile = require(`/Users/roopa/Desktop/projects/LLbot/src/Commands/${folder}/${file}`);

            client.commands.set(commandFile.data.name, commandFile);
            commandsArray.push(commandFile.data.toJSON());
            table.addRow(file, '🟩');
            continue;
        }
    }

    client.application.commands.set(commandsArray);

    return console.log(table.toString(), '\nCommands Loaded')
}
module.exports = { loadCommands };

Is it giving me errors because my 'commandsArray' is empty? idk for sure tho.

aaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbb (ignore this , i didnt have much to explain so did this.)


Solution

  • You're trying to set the property commands of an undefined property applications.

    Adding applications.commands under the form of an object should fix it:

    client.application = { commands: commandsArray };