Skip to content

Built-in interactionCreate Event

This event handles the creation execution of the slash commands, using the built-in slash command executing from the slash properties of a command file and also using the built-in client.commands Collection object. We will also display the slash command Id, incase you decide to delete a slash command.

Using v3.1.0 or later and you decided to disable the built-in IntercationCreate

Please use execute property instead of interactionReply property for running slash commands and make sure for the prefix version of the command use executePrefix property if you choose to use a prefix version of the command.

const { InteractionType, Events } = require("discord.js");

module.exports = {
  name: Events.InteractionCreate,
  async execute(interaction, client) {
    const { commandName, commandId, customId } = interaction;

    const isMyCommand =
      interaction.type === InteractionType.ApplicationCommand
        ? client.commands.has(commandName)
        : false;

    const isMyCustomId = customId
      ? client.commands.some(
          (cmd) => cmd.customIds && cmd.customIds.includes(customId)
        )
      : false;

    if (
      interaction.type === InteractionType.ApplicationCommand &&
      !isMyCommand
    ) {
      console.log(
        "[Interaction]",
        "[SKIP]",
        `Slash command '${commandName}' not found in this bot's commands`
      );
      return;
    }

    if (customId && !isMyCustomId) {
      console.log(
        "[Interaction]",
        "[SKIP]",
        `Custom ID '${customId}' not found in this bot's commands `
      );
      return;
    }

    const level = client.getPermissionsLevel({
      author: interaction.user,
      channel: interaction.channel,
      guild: interaction.guild,
      guildMember: interaction.member,
    });

    try {
      if (interaction.type === InteractionType.ApplicationCommand) {
        const cmd = client.commands.get(commandName);

        if (!cmd) {
          console.error("Unable to find slash command:" + cmd);
          return await interactionErrorReply(interaction, {
            content: `Command not found: ${commandName}`,
            ephemeral: true,
          });
        }

        console.log(
          `[SLASH CMD]`,
          `[${interaction.user.tag}]`,
          `${commandName}`
        );
        console.log("[SLASH CMD]", "[ID]", commandId);

        try {
          return await cmd.execute(interaction, client, level);
        } catch (e) {
          console.error(
            `[SLASH CMD]`,
            "ApplicationCommand interaction (slash command) execution failed",
            e
          );
          return await interactionErrorReply(interaction, {
            content: `An error occurred while processing your command: ${commandName}. Error: ${e}`,
            ephemeral: true,
          });
        }
      }

      const foundCmd = client.commands.find((cmd) => {
        if (!cmd.customIds) return false;

        if (Array.isArray(cmd.customIds)) {
          return cmd.customIds.includes(customId);
        }

        return false;
      });

      if (!foundCmd || !foundCmd.name) {
        console.error("Unable to find command with customId: " + customId);
        return await interactionErrorReply(interaction, {
          content: `Command not found for customId: ${customId}`,
          ephemeral: true,
        });
      }

      if (
        typeof foundCmd.customIdInteraction !== "function" ||
        !foundCmd.customIdInteraction
      ) {
        console.error(
          `[CUSTOM ID CMD]`,
          `Command "${foundCmd.name}" has not implemented customIdInteraction.`
        );
        return await interactionErrorReply(interaction, {
          content: `Command "${foundCmd.name}" has not implemented customIdInteraction.`,
          ephemeral: true,
        });
      }

      console.log(
        `[CUSTOM ID CMD]`,
        `[${interaction.user.tag}]`,
        `${foundCmd.name} CustomID: ${customId}`
      );

      try {
        return await foundCmd.customIdInteraction(interaction, client, level);
      } catch (e) {
        console.error(
          `[CUSTOM ID CMD]`,
          `Error executing customIdInteraction for command "${foundCmd.name}":`,
          e
        );
        return await interactionErrorReply(interaction, {
          content: `An error occurred while processing your interaction: ${foundCmd.name} and customId: ${customId}. Error: ${e}`,
          ephemeral: true,
        });
      }
    } catch (e) {
      console.log("[INTERACTION]", "Interaction execution failed", e);
      return await interactionErrorReply(interaction, {
        content: "An error occurred while processing your interaction: " + e,
        ephemeral: true,
      });
    }
  },
};

async function interactionErrorReply(interaction, options) {
  try {
    await interaction.reply(options);
  } catch (error) {
    console.error("[DFH] Failed to reply to interaction:", error.message);
    // Don't throw - just log and continue
  }
}