#commandArray is not defined but it is defined.

19 messages · Page 1 of 1 (latest)

hearty seal
#
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?

neat heraldBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • 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!
bitter stump
#

...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

hearty seal
#

How would I change that?

bitter stump
#

Im not sure why it's attached to your client in the first place

kindred dirgeBOT
#

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

hearty seal
#

Im going off of yt video from Fusion 😓

#

I may have done it wrong

bitter stump
#

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

hearty seal
#

I see on the guild that it doesnt have version: "9" for the set token do i NEED that?

bitter stump
#

Nope, latest version is 10 anyway

#

And it will default to that if you dont specify

hearty seal
#

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?