I’ve been learning discord.js following the official docs, but I’m unable to get the bot to reply to the slash ( / ) commands
//This is the ping.js command file
const { SlashCommandBuilder } = require("discord.js");
module.exports = {
data: new SlashCommandBuilder()
.setName('pings')
.setDescription('Replies with Pong!'),
async execute(interaction) {
await interaction.reply('Pong!');
},
};
//This is the command handling / interaction handling
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
//construct and prepare an instance for the REST module
const rest = new REST().setToken(discordToken);
//deploy the commands
(async () =>{
try{
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
Routes.applicationGuildCommands(discordClientID),
{body:commands},
)
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (err){
console.error(err);
}
})
client.on(Events.InteractionCreate, async interaction =>{
if(!interaction.isChatInputCommand()) return;
console.log(interaction);
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 (err){
console.error(err);
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});
}
}
});
Sorry if the code is long, but this is all I have. So far I know that whenever I type /pings the bot should response with “Pong!” but all I see is the slash commands of the other bots I have in the server. This is not comming from the app itself, since I have added the bot with this two scopes “bot” “application.commands” as well as the “slash commands” so I guess this should be working just fine…
I was trying to find a useful answer here, but all of them are outdated, and are no longer working.