#Unable to define commandFiles

66 messages · Page 1 of 1 (latest)

cosmic oriole
#
const fs = require('node:fs');
const path = require('node:path');
const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');

const commands = [];
const commandsPath = path.join(__dirname, 'Commands');
const commandFiles = fs.readdirSync('./Commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    commands.push(command.data.toJSON());
}

const rest = new REST({ version: '10' }).setToken(token);

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
    .then(data => console.log(`Successfully registered ${data.length} application commands.`))
    .catch(console.error);

Hello, I'm quite new & understand the basics of JavaScript. I am confused on how commandFiles isn't being declared? I was following the documentation.

whole spoke
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

cosmic oriole
# fallen slate What's the error
ReferenceError: commandFiles is not defined
    at Object.<anonymous> (C:\Users\XXX\Documents\Discord Bots\JavaScript Bot\index.js:7:20)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47
cosmic oriole
#

Trying to make a command handler

fallen slate
#

then it's irrelevant

#

commandFiles is defined in deploy-commands, but not index.

cosmic oriole
#

It's stopping me from running the bot though?

#

I'm quite confused on what you mean, so what's the exact issue here?

fallen slate
#

That's the issue

cosmic oriole
#

ah

#

I see

fallen slate
#

you are accessing it in index

cosmic oriole
#

So I remove it from index.js, and it should be good?

fallen slate
#

I guess

cosmic oriole
#

What would your method of doing it would be?

fallen slate
#

idk

#

cause idk the code you have

cosmic oriole
#
// Require the necessary discord.js classes
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
    // Set a new item in the Collection

// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// New Command Collection
client.commands = new Collection();

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');
});

const commands = [];

client.on('interactionCreate', async interaction => {
    if (!interaction.isChatInputCommand()) return;

    const command = interaction.client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});

const rest = new REST({ version: '10' }).setToken(token);

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
    .then(data => console.log(`Successfully registered ${data.length} application commands.`))
    .catch(console.error);

// Login to Discord with your client's token
client.login(token);
#

That's my index.js

junior sable
#

You'd have to export the deploy-commands file and then call it in your index

#

Oh your index is messy lol

cosmic oriole
#

yeah

#

You can tell I am new

#

I understand the basics of JS but that's about it

junior sable
#

You should follow the guide though, it's a better start

cosmic oriole
#

I have been following the documentation, putting the necessary code it tells me to.

#

Unless I've been misunderstood

junior sable
#

Yes the rest.put lines should remain in your deploy-commands file and you'd run that whenever you add/change your cmds. If you want to call it every time your index runs, you'd export it so you can call it. But it's best to do it separately as there can be delays in updating the commands if done too often.

#

So in the end once it's separated, you'd run deploy file first to register new/update commands, then run the index

cosmic oriole
#

Oh I see, it works now well thank you for the information. It definitely helps!

junior sable
#

Awesome coolpika

cosmic oriole
#

One small thing though before I leave though.

#

So in my deploy file, it's suppose to log the commands that are in /Commands folder. However, it hasn't output anything. Which I'm assuming it meant that it failed?

junior sable
#

I don't know if it matters with case-sensitivity as it was the first thing I noticed, but you have upper Commands and lower-case commands in the last for loop

cosmic oriole
#

Lemme make it lower-case to see if anything changes

#

Hmm.. yeah nothing

#
const fs = require('node:fs');
const path = require('node:path');
const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');

const commands = [];
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    commands.push(command.data.toJSON());
}

const rest = new REST({ version: '10' }).setToken(token);

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
    .then(data => console.log(`Successfully registered ${data.length} application commands.`))
    .catch(console.error);

Here's the code for reference

junior sable
#

Did you actually make a command file?

cosmic oriole
#

Yes I have ping.js

#
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Replies with Pong!'),
    async execute(interaction) {
        await interaction.reply('Pong!');
    }
};
#

Could something possibly be wrong in here?

#

That I am not aware of

junior sable
#

No, as long as that's in the folder, it's fine, so when you ran the deploy script, did it log anything?

cosmic oriole
#

Oh now it registers, lemme check if the command works now.

#

Application doesn't respond

junior sable
#

You have to run index now

cosmic oriole
#

yeah still nothing

#

weird

#

no errors at all

junior sable
#

Application not responding? You're sure it's online? And you see the ping command when you type slash

cosmic oriole
#

Yep, all of that is correct.

#

I type in node . to start it up, and type in /ping.

junior sable
#

Hm. What's your index file now? And can you show your console logs doing deploy then index after?

cosmic oriole
#
// Require the necessary discord.js classes
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');

// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// New Command Collection
client.commands = new Collection();

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');
});

const commands = [];

client.on('interactionCreate', async interaction => {
    if (!interaction.isChatInputCommand()) return;

    const command = interaction.client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});
// Login to Discord with your client's token
client.login(token);

Here's the Index File

junior sable
#

Anf you're sure the /ping is under your bot in the server?

cosmic oriole
#

Mhm.

junior sable
# cosmic oriole Mhm.

Oh duh lol, you're not adding anything to your commands array Collection that you declared in your index.js

cosmic oriole
#

Oh-

junior sable
#

Sorry the client.commands collection, you don't need that extra commands array.

cosmic oriole