I'm currently making a bot for my friend's server, but I have discovered an error which is
TypeError: Cannot read properties of undefined (reading 'set') at Client.client.handleCommands (C:\Users\Admin\Documents\galax-bot\functions\handlers\handleCommands.js:19:37) at Object. (C:\Users\Admin\Documents\galax-bot\index.js:19:8) I have read the documents and searched for multiple resources but I still cannot solve the problem
// handleCommands.js
const { ClientUser } = require("discord.js");
const { REST } = require("@discordjs/rest")
const { Routes } = require("discord-api-types/v9")
const fs = require("fs")
module.exports = (client) => {
client.handleCommands = async () => {
const commandFolders = fs.readdirSync(`./commands`);
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./commands/${folder}`)
.filter((file) => file.endsWith(".js"));
const { commands, commandArray } = client;
for (const file of commandFiles) {
const command = require(`../../commands/${folder}/${file}`)
if (command) {
console.log(command.data)
client.commands.set(command.name, command);
commandArray.push(command.data.toJSON());
console.log(`Command: ${command.data.name} has been passed through the
handler`)
}
}
}
}
const clientId = '1033991303165390878';
const guildId = '905393827488071701';
const rest = new REST({ version: "9"
}).setToken("Not letting you see the token");
try {
console.log('Started refreshing application (/) commands.');
rest.put(Routes.applicationCommands(clientId, guildId), {
body: client.commandArray,
});
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
}
// index.js
require("dotenv").config
const { Client, Collection , GatewayIntentBits, EmbedBuilder, PermissionsBitField,
Permissions } = require("discord.js");
const Token = process.env.token
const prefix = '^';
const fs = require('fs')
const client = new Client({ intents: [GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
client.commads = new Collection();
client.commandArray = []
const functionFolders = fs.readdirSync(`./functions`)
for (const folder of functionFolders) {
const functionFiles = fs
.readdirSync(`./functions/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of functionFiles)
require(`./functions/${folder}/${file}`)(client);
}
client.handleEvents();
client.handleCommands();
client.login(Token)
Yeah, as @Andreas said, client.commands
is undefined, and with it client.commands.set
.
This error happens always when a variable whose value should be passed, is undefined.