TypeError: Cannot read properties of undefined (reading 'get')

const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const Utils = require('../../utils');

module.exports = {
data: new SlashCommandBuilder()
.setName('ban')
.setDescription('Ban a player from the server')
.addUserOption(option =>
option.setName('user')
.setDescription('The user to ban')
.setRequired(true))
.addStringOption(option =>
option.setName('time')
.setDescription('The time to ban the user for'))
.addStringOption(option =>
option.setName('reason')
.setDescription('The reason to ban the user for')),
async execute(interaction) {
const user = interaction.options.getUser('user');
const time = interaction.options.getUser('time');
const reason = interaction.options.getUser('reason');

const channel = await client.channels.fetch("1198299044582281216", { force: true });
const embed = new EmbedBuilder()
embed.setTitle(channel.name)
await interaction.reply({ embeds: [embed] })
},
};
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const Utils = require('../../utils');

module.exports = {
data: new SlashCommandBuilder()
.setName('ban')
.setDescription('Ban a player from the server')
.addUserOption(option =>
option.setName('user')
.setDescription('The user to ban')
.setRequired(true))
.addStringOption(option =>
option.setName('time')
.setDescription('The time to ban the user for'))
.addStringOption(option =>
option.setName('reason')
.setDescription('The reason to ban the user for')),
async execute(interaction) {
const user = interaction.options.getUser('user');
const time = interaction.options.getUser('time');
const reason = interaction.options.getUser('reason');

const channel = await client.channels.fetch("1198299044582281216", { force: true });
const embed = new EmbedBuilder()
embed.setTitle(channel.name)
await interaction.reply({ embeds: [embed] })
},
};
the error is at line 3 here
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 });
}
}
});
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 });
}
}
});
5 Replies
d.js toolkit
d.js toolkit9mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - 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! - Marked as resolved by OP
Squid
Squid9mo ago
<Client>.commands is not a native property, so you need to define it yourself If you're following the discord.js official guide, you forgot to add the line client.commands = new Collection() in your index.js
Tissemyren
TissemyrenOP9mo ago
ReferenceError: Collection is not defined
Squid
Squid9mo ago
import it from discord.js
Tissemyren
TissemyrenOP9mo ago
Thanks, that worked!
Want results from more Discord servers?
Add your server