I am trying to make a slash command system for my discord.js bot, but it does not show up in the app
Here is the code I am using
const {
Client,
Collection,
Intents
} = require('discord.js');
const client = new Client({
intents: [Intents.FLAGS.GUILDS]
});
const fs = require('fs');
const {
Routes
} = require('discord-api-types/v9');
const { REST } = require('@discordjs/rest');
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const commands = [];
// Creating a collection for commands in client
client.commands = new Collection();
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
client.commands.set(command.data.name, command);
}
client.on('ready', () => {
console.log(`Ready! Logged in as ${client.user.tag}`)
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
console.log(`Loaded slash command /${command.data.name}.`)
}
const CLIENT_ID = client.user.id;
const rest = new REST({
version: '9'
}).setToken("myToken");
(async () => {
try {
await rest.put(
Routes.applicationCommands(CLIENT_ID), {
body: commands
},
);
console.log('Successfully registered all application commands globally');
} catch (error) {
if (error) console.error(error);
}
})();
})
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
const { options } = interaction
try {
await command.execute(interaction, options, client);
} catch (error) {
console.error(error);
await interaction.reply({
content: `ERROR: There was a problem executing the **${command.name}** command. Please try again later.`,
ephemeral: true
});
}
});
client.login("myToken")
This is my command file (My ping.js file)
const { SlashCommandBuilder } = require("@discordjs/builders");
module.exports = {
data: new SlashCommandBuilder()
.setName("ping")
.setDescription("Ping Pong!"),
async execute(interaction, options, client) {
interaction.reply("Pong!")
}
}
Whenever I launch my bot, no slash commands appear. All the strings are logged to the console though.
I have given the bot application.commands scope.