I am trying to make a discord bot with / commands. I already got a lot working, but if I try to use subcommands it doesnt work. I followed the discordjs guide, even completely copying it doesnt work. I use the event handler and the command handler from the guide. I get the error:
TypeError: command.execute is not a function
index.js code:
console.clear();
const fs = require('fs');
const { Client, Collection ,Intents } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('js'));
for (const file of eventFiles) {
const event = require(`./events/${file}`);
if(event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args))
}
}
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
// Set a new item in the collection
// With the key as the command name and the value as the exported module
client.commands.set(command.data.name, command);
}
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = 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});
}
});
client.login(token)
info.js code:
const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('info')
.setDescription('Get info about a user or the server!')
.addSubcommand(subcommand =>
subcommand
.setName('user')
.setDescription('Info about a user')
.addUserOption(option => option.setName('target').setDescription('The user')))
.addSubcommand(subcommand =>
subcommand
.setName('server')
.setDescription('Info about the server')),
async exexute(interaction) {
if (interaction.options.getSubcommand() === 'user') {
const user = interaction.options.getUser('target');
if (user) {
await interaction.reply(`Username: ${user.username}nID: ${user.id}`);
} else {
await interaction.reply(`Your username: ${interaction.user.username}nYour ID: ${interaction.user.id}`);
}
} else if (interaction.options.getSubcommand() === 'server') {
await interaction.reply(`Server name: ${interaction.guild.name}nTotal members: ${interaction.guild.memberCount}`);
}
}
}