ReferenceError: client is not defined

I have followed the guide on "https://discordjs.guide/#before-you-begin" and when I run the code "node deploy-commands.js" I am getting this error: ReferenceError: client is not defined at Object.<anonymous> (C:\Users\Tiaan\OneDrive\Desktop\Mario\commands\fun\wahoo.js:12:1) at Module._compile (node:internal/modules/cjs/loader:1256:14) at Module._extensions..js (node:internal/modules/cjs/loader:1310:10) at Module.load (node:internal/modules/cjs/loader:1119:32) at Module._load (node:internal/modules/cjs/loader:960:12) at Module.require (node:internal/modules/cjs/loader:1143:19) at require (node:internal/modules/cjs/helpers:110:18) at Object.<anonymous> (C:\Users\Tiaan\OneDrive\Desktop\Mario\deploy-commands.js:18:19) at Module._compile (node:internal/modules/cjs/loader:1256:14) at Module._extensions..js (node:internal/modules/cjs/loader:1310:10) Is it possible I have the latest version installed but I'm using an old guide?
13 Replies
d.js toolkit
d.js toolkit2y ago
• 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.
MastaYoda
MastaYodaOP2y ago
My version of discord.js is 14.11.0, and my node version is v18.16.1. here is my code for index.js: const fs = require('node:fs'); const path = require('node:path'); const { Client, Collection, Events, GatewayIntentBits } = require('discord.js'); const { token } = require('./config.json'); const client = new Client({ intents: [GatewayIntentBits.Guilds] }); client.commands = new Collection(); const commandsPath = path.join(dirname, 'commands'); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); // Set a new item in the Collection with the key as the command name and the value as the exported module if ('data' in command && 'execute' in command) { client.commands.set(command.data.name, command); } else { console.log([WARNING] The command at ${filePath} is missing a required "data" or "execute" property.); } } client.on(Events.InteractionCreate, interaction => { if (!interaction.isChatInputCommand()) return; console.log(interaction); }); // When the client is ready, run this code (only once) // We use 'c' for the event parameter to keep it separate from the already defined 'client' client.once(Events.ClientReady, c => { console.log(Ready! Logged in as ${c.user.tag}); }); // Log in to Discord with your client's token client.login(token); deploy-commands.js: const { REST, Routes } = require('discord.js'); const { clientId, guildId, token } = require('./config.json'); const fs = require('node:fs'); const path = require('node:path'); const commands = []; // Grab all the command files from the commands directory you created earlier const foldersPath = path.join(dirname, 'commands'); const commandFolders = fs.readdirSync(foldersPath); for (const folder of commandFolders) { // Grab all the command files from the commands directory you created earlier const commandsPath = path.join(foldersPath, folder); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); if ('data' in command && 'execute' in command) { commands.push(command.data.toJSON()); } else { console.log([WARNING] The command at ${filePath} is missing a required "data" or "execute" property.); } } } // Construct and prepare an instance of the REST module const rest = new REST().setToken(token); // and deploy your commands! (async () => { try { console.log(Started refreshing ${commands.length} application (/) commands.); // The put method is used to fully refresh all commands in the guild with the current set const data = await rest.put( Routes.applicationGuildCommands(clientId, guildId), { body: commands }, ); console.log(Successfully reloaded ${data.length} application (/) commands.); } catch (error) { // And of course, make sure you catch and log any errors! console.error(error); } })(); config.json: { "token": "not including my token here for obvious reasons", "clientId": "1127710363786420255", "guildId": "1090048142864568350" } here is my command: const { SlashCommandBuilder } = require('discord.js'); module.exports = { data: new SlashCommandBuilder() .setName('wahoo') .setDescription('Replies with wahoo!'), async execute(interaction) { await interaction.reply('WAHOOOOOOO!'); }, }; client.on(Events.InteractionCreate, async interaction => { if (!interaction.isChatInputCommand()) return; const command = interaction.client.commands.get(interaction.commandName); if (!command) { console.error(No command matching ${interaction.commandName} was found.); return; } try { await command.execute(interaction); } catch (error) { console.error(error); if (interaction.replied || interaction.deferred) { await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); } else { await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } } });
MrMythical
MrMythical2y ago
wahoo.js line 12 references non-existent client variable
MastaYoda
MastaYodaOP2y ago
starting from line 12 I moved all of that to index.js assuming that's where it goes and when I ran the code it worked, however when I run my command I get "The application did not respond"
MrMythical
MrMythical2y ago
Then it isn't responding to the interaction
MastaYoda
MastaYodaOP2y ago
okay, so what do I do? I'm not a programmer
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Pooyan
Pooyan2y ago
I think this should fix it
MrMythical
MrMythical2y ago
Do not spoonfeed code
Pooyan
Pooyan2y ago
Oh yeah sorry, I've forgot it
MastaYoda
MastaYodaOP2y ago
I get you want me to learn javascript but I’m not interested in learning the basics of a coding language, I’m just trying to follow the guide and make a simple bot that responds to commands, I’d appreciate it if you just told me why I’m getting the error and how to fix it
treble/luna
treble/luna2y ago
#rules 3. Hire someone if you're not interested in coding. You're expected to have a solid js understanding
MastaYoda
MastaYodaOP2y ago
Okay, can you at least help me understand the problem? I need help.
Want results from more Discord servers?
Add your server