Essentials
Ping / Pong
Hello-world slash command
Minimal slash command that replies ephemerally. The standard first bot from the discord.js slash-command flow.
Based on discord.js guide — deploying commands
/ping
Source preview
import {
Client,
Events,
GatewayIntentBits,
REST,
Routes,
SlashCommandBuilder,
MessageFlags,
} from "discord.js";
const client = new Client({
intents: [GatewayIntentBits.Guilds],
});
const commands = [
new SlashCommandBuilder()
.setName("ping")
.setDescription("Replies with pong"),
].map((c) => c.toJSON());
client.once(Events.ClientReady, async () => {
const rest = new REST({ version: "10" }).setToken(process.env.DISCORD_TOKEN!);
await rest.put(Routes.applicationCommands(client.user!.id), { body: commands });
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName !== "ping") return;
await interaction.reply({
content: "Pong!",
flags: MessageFlags.Ephemeral,
});
});
client.login(process.env.DISCORD_TOKEN);