M
M
Maksim Fallen2021-01-02 15:06:45
JavaScript
Maksim Fallen, 2021-01-02 15:06:45

TypeError: Cannot read properties of undefined (reading 'commands'), why does it throw an error?

//  Commands.js //
const { Perms } = require("../Validation/Permission");
const { Client } = require("discord.js");
const { promisify } = require("util");
const { glob } = require("glob");
const PG = promisify(glob);
const { resourceUsage } = require("process");

/**
 * @param {Client} client 
 */
 module.exports = async (client) => {

    commandsArry = [];

    (await PG(`${process.cwd()}/Commands/*/*.js`)).map(async(file) => {
        const command = require(file);

        if(!command.name) return;

            if(command.permission) {
            
            command.defaultPermission = false
            }

        commandsArry.push(command);
        client.commands.set(command.name, command);

        console.log(` Command Loaded ` + command.name);
    });

    client.on("ready", async ()  => {
        const MainGuild = await client.guilds.cache.get("649379388110012445");

        MainGuild.commands.set(commandsArry).then((command) => {
            const Roles = (commandName) => {
                const cmdPerms = commandsArry.find((c) => c.name === commandName).perms;

                if(!cmdPerms) return null;

                return MainGuild.roles.cache.filter((r) => r.permissions.has(cmdPerms) && !r.managed);  
            };

            const fullPermissions = command.reduce((ac, x) => {
                const roles = Roles(x.name);
                if (!roles) return ac;

                const permissions = roles.reduce((a, v) => {
                    return [ ...a, {id: v.id, type: "ROLE", permission: true}];
                }, []);

                return [...ac, {id: x.id, permissions}];
            }, []);

            MainGuild.commands.permissions.set({ fullPermissions });
        });
    });
};

E:\staff\Botdiscord\Handlers\Commands.js:34
        MainGuild.commands.set(commandsArry).then((command) => {
                  ^
TypeError: Cannot read properties of undefined (reading 'commands')       
    at Client.<anonymous> (E:\staff\Botdiscord\Handlers\Commands.js:34:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2021-01-02
@nobodyphilia

client.guilds.cache.get("649379388110012445") - no such number was found in the cache.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question