commandArray is not defined but it is defined.

const fs = require("fs");
require("dotenv").config();
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");

module.exports = (client) => {
client.handleCommands = async () => {
const commandFolders = fs.readdirSync("./src/commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./src/commands/${folder}`)
.filter((file) => file.endsWith(".js"));

const { commands, commandArray } = client;
for (const file of commandFiles) {
const command = require(`../../commands/${folder}/${file}`);
commands.set(command.data.name, command);
commandArray.push(command.data.toJSON());
console.log(`Command loaded: ${command.data.name}`);
}
}

const clientId = process.env.clientId;

const rest = new REST({ version: "9" }).setToken(process.env.token);
try {
console.log("Started refreshing application (/) commands.");

await rest.put(Routes.applicationCommands(process.env.clientId), {
body: commandArray,
});

console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
};
};
const fs = require("fs");
require("dotenv").config();
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");

module.exports = (client) => {
client.handleCommands = async () => {
const commandFolders = fs.readdirSync("./src/commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./src/commands/${folder}`)
.filter((file) => file.endsWith(".js"));

const { commands, commandArray } = client;
for (const file of commandFiles) {
const command = require(`../../commands/${folder}/${file}`);
commands.set(command.data.name, command);
commandArray.push(command.data.toJSON());
console.log(`Command loaded: ${command.data.name}`);
}
}

const clientId = process.env.clientId;

const rest = new REST({ version: "9" }).setToken(process.env.token);
try {
console.log("Started refreshing application (/) commands.");

await rest.put(Routes.applicationCommands(process.env.clientId), {
body: commandArray,
});

console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
};
};
It is defined at line 14. But it claims line 30 commandArray is not defined. Any help?
11 Replies
d.js toolkit
d.js toolkit•16mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
monbrey
monbrey•16mo ago
...no it isnt? I dont see where you define it for line 14 either oh nvm Its a scoping issue The definition is inside your for loop
Mr. Void
Mr. VoidOP•16mo ago
How would I change that?
monbrey
monbrey•16mo ago
Im not sure why it's attached to your client in the first place
d.js docs
d.js docs•16mo ago
guide Creating Your Bot: Registering slash commands The command deployment script, to register your slash commands with Discord so they appear in the interface. read more
Mr. Void
Mr. VoidOP•16mo ago
Im going off of yt video from Fusion 😓 I may have done it wrong
monbrey
monbrey•16mo ago
Cant say, we don't really follow or support third party youtube tutorials But yes, its possible that it doesn't fit with something else
Mr. Void
Mr. VoidOP•16mo ago
I see on the guild that it doesnt have version: "9" for the set token do i NEED that?
monbrey
monbrey•16mo ago
Nope, latest version is 10 anyway And it will default to that if you dont specify
Mr. Void
Mr. VoidOP•16mo ago
ah ok so like when its specified (like in my code) it will use that version but if nothing is there it does the latest?
monbrey
monbrey•16mo ago
yeah
Want results from more Discord servers?
Add your server