#enzyy load command files

1 messages · Page 1 of 1 (latest)

shy flicker
#

no, it wouldnt change the error. what does it log?

main hazel
shy flicker
#

hmm, is the console.log above the require() call within the for loop

main hazel
#

yeah

shy flicker
#

ah! there it is

#

that object right before the error is thrown

#

its trying to concatenate
"./SlashCommands/Division 3/Functions/" and the actual object

{
  name: "create",
  description: "...",
  options: [],
  default_permission: false
}
#

that's the path that it's trying to load (and can't find)

#

./SlashCommands/Division 3/Functions/[object Object]

main hazel
#

what should I do? 😁

shy flicker
#

so there's an issue with one of the entries in the commands array

main hazel
#

do both handlers need to be changed

shy flicker
#

right after the let commands = fs.readdirSync... line, can you console.log(commands) and show the output?

#

i cant spot the error

main hazel
#

alright

shy flicker
#

actually, i found the issue!

#

ok, one sec

#

using this image as a reference so that i have line numbers to refer to

#

on line 10 (in image above), you load the file names of all the js files in the (server)/(dir) directory

#

that file name array is stored in the variable called commands

#

that array contains only strings (and should only contain strings)

#

on line 16, you push an object onto the commands array

#

you no longer have only strings in the array

#

the for loop then tries to iterate over that item in the array, and then you run into the issue you're getting

#

you probably want to create a new array to store the items you're pushing on line 16

#

maybe insert it between lines 10 and 11

main hazel
#

hm

shy flicker
#

something like

// ▬▬▬▬▬▬▬▬▬▬ Handling ▬▬▬▬▬▬▬▬▬▬ //
const table = new ascii("Bot Commands")

table.setHeading("Interaction", "Load Status")
fs.readdirSync("./SlashCommands").forEach(server => {
    fs.readdirSync(`./SlashCommands/${server}`).forEach(dir => {
        let commands = fs.readdirSync(`./SlashCommands/${server}/${dir}/`).filter(file => file.endsWith(".js"));
        
        commands = commands.map((file) => {
            let pull = require (`./SlashCommands/${server}/${dir}/${file}`);
            if (pull.data.name) {
                table.addRow(file.split(".js")[0], "Ready!");
                
                return pull.data.toJSON();
            } else {
                table.addRow(file.split(".js")[0], "Error!");
                
                return null;
            }
        }).filter((command) => {
            return command !== null;
        });
    });
});
            
console.log(commands);
#

(edited example for readability)

main hazel
#

lemme test it

shy flicker
#

i just realized an issue with my example

#

depending on what you're trying to do, you might be better off just having another variable

#
// ▬▬▬▬▬▬▬▬▬▬ Handling ▬▬▬▬▬▬▬▬▬▬ //
const table = new ascii("Bot Commands");

table.setHeading("Interaction", "Load Status");
const allCommands = [];
fs.readdirSync("./SlashCommands").forEach((server) => {
    fs.readdirSync(`./SlashCommands/${server}`).forEach((dir) => {
        const commands = fs.readdirSync(`./SlashCommands/${server}/${dir}/`).filter((file) => {
            return file.endsWith(".js");
        });
        for (const file of commands) {
            const pull = require(`./SlashCommands/${server}/${dir}/${file}`);
            if (pull.data.name) {
                table.addRow(file.split(".js")[0], "Ready!");
                allCommands.push(pull.data.toJSON());
            } else {
                table.addRow(file.split(".js")[0], "Error!");
            }
        }
    });
});
console.log(allCommands);
main hazel
#

gimme a sec

main hazel
#

@shy flicker, works but it doesn't register them in the server..

shy flicker