← Back to lessons

Lesson 11 — Error Handling

Goal: Avoid crashes and handle failures safely.

Video

Step-by-step

Step 1 — Add try/catch in a command

Open a command (example: ping) and wrap logic:

async execute(interaction) {
  try {
    // your logic
    await interaction.reply("Works!");
  } catch (err) {
    console.error(err);
    if (!interaction.replied) {
      await interaction.reply({ content: "⚠️ Something went wrong.", ephemeral: true });
    }
  }
}
Step 2 — Make an intentional error

Add a line like throw new Error("test"); and see how it behaves.

Step 3 — Remove the error, keep the protection

Now your bot is safer.

← Lesson 10 Next → Lesson 12