Essentials
Deferred reply
Long-running slash commands
Defers the interaction before editing the reply — required for anything that takes more than a few seconds.
Based on discord.js guide — deferred responses
/slow
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("slow")
.setDescription("Defers then edits the reply"),
].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 !== "slow") return;
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
await interaction.editReply({ content: "Done!" });
});
client.login(process.env.DISCORD_TOKEN);