Command handler error

Hi, I started coding this new bot with Typescript yesterday. I touched something and now my .ts file to deploy my commands won't work. The error doesn't happen in deploy_commands.ts actually but in my index.ts. Here's the code so far: deploy_commands.js
const fs = require('node:fs');
const path = require('node:path');
const { REST, Routes } = require('discord.js');
require("dotenv").config();

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

for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
commands.push(command.data.toJSON());
}

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

rest.put(Routes.applicationCommands(process.env.CLIENT_ID), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error);
const fs = require('node:fs');
const path = require('node:path');
const { REST, Routes } = require('discord.js');
require("dotenv").config();

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

for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
commands.push(command.data.toJSON());
}

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

rest.put(Routes.applicationCommands(process.env.CLIENT_ID), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error);
index.ts (interested part only)
client.commands = new Collection();

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

for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
//The line above is 51
}
client.commands = new Collection();

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

for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
//The line above is 51
}
The error I get is as follows:
C:\mydir\index.ts:51
client.commands.set(command.data.name, command);
^
TypeError: Cannot read properties of undefined (reading 'name')
at Object.<anonymous> (C:\mydir\index.ts:51:35)
...
(I've reached max text)
C:\mydir\index.ts:51
client.commands.set(command.data.name, command);
^
TypeError: Cannot read properties of undefined (reading 'name')
at Object.<anonymous> (C:\mydir\index.ts:51:35)
...
(I've reached max text)
1 Reply
maybebas
maybebasOP3y ago
it may be my ping.ts command as if I remove it the deploy works i'll take a look at it thanks <3 Ok so, I used Typescript's import and export features. The problem should be at ping.ts:1:1?
at Object.<anonymous> (C:\mydir\commands\ping.ts:1:1)
at Object.<anonymous> (C:\mydir\commands\ping.ts:1:1)
import { SlashCommandBuilder, client } from "../index";


module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!'),
async execute(interaction) {
await interaction.reply(`:ping_pong: Pong! ${client.w.ping}ms`);
},
};
import { SlashCommandBuilder, client } from "../index";


module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!'),
async execute(interaction) {
await interaction.reply(`:ping_pong: Pong! ${client.w.ping}ms`);
},
};
I don't know how to solve it honestly, I can't const as it would be an existing declaration I do some projects as hobby and I'm learning online, without courses, just for fun. Thanks for the correction tho
Want results from more Discord servers?
Add your server