Create First Slash Command
// server.js
// Import dependency
const { ..., SlashCommandBuilder } = require('@discordjs/builders');
const { REST } = require('@discordjs/rest');
// Create new slash command
const commands = [
new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!')
].map(command => command.toJSON());
// Register command
const rest = new REST({ version: '10' }).setToken(process.env.token);
rest.put(Routes.applicationGuildCommands(
process.env.clientId,
process.env.guildId
), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error);
// Reply command
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const { commandName } = interaction;
if (commandName === 'ping') {
const timeTaken = Date.now() - interaction.createdTimestamp;
await interaction.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
}
});


Last updated