#enzyy load command files
1 messages · Page 1 of 1 (latest)
hmm, is the console.log above the require() call within the for loop
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]
what should I do? 😁
so there's an issue with one of the entries in the commands array
do both handlers need to be changed
right after the let commands = fs.readdirSync... line, can you console.log(commands) and show the output?
i cant spot the error
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
hm
Alternatively, if you'd like to first store file names, and then store the command data, in the same array, you could use .map()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
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)
lemme test it
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);
gimme a sec
@shy flicker, works but it doesn't register them in the server..
You'll want to store the commands that you retrieved somewhere. Try looking at this https://discordjs.guide/creating-your-bot/command-handling.html#dynamically-executing-commands