#Correct 'type' for Interaction Object?

12 messages · Page 1 of 1 (latest)

quick jay
#

Hi, I'm trying to find the correct type/interface for the Interaction object which is sent by Discord to my endpoint using the types found in the DiscordJS library.

I'm not sure if this is the correct one but I'm trying with APIInteraction from discord.js and can kind of scope the interface to give somewhat correct typing depending if it's an application command or message component like this:

import { APIInteraction, InteractionType } from 'discord.js';

export interface Interaction {
  run: (interaction: APIInteraction) => Promise<void>;
}

const testCommand: Interaction = {
  run: async (interaction) => {
    if (interaction.type === InteractionType.ApplicationCommand) {
      interaction.data.name;
    } else if (interaction.type === InteractionType.MessageComponent) {
      interaction.data.custom_id;
    }
  },
};

With this, the "somewhat" correct typing for interaction.data. show up depending on the if scope.

The issue I'm having right now is getting the application command if statement recognising that the interaction object may be one with an options field, used for subcommand interactions.

Is there anything I can do to scope the APIInteraction type to recognise the options field in application commands or do I need to readdress this approach with other typings (if so which typings)?

Thanks!

full elkBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
alpine radish
#

by "your endpoint" do you mean HTTP interactions or interactionCreate gateway event?

quick jay
wintry whale
#

Not all API interactions have that field. The union is as follows:

export type APIInteraction =
    | APIPingInteraction
    | APIApplicationCommandInteraction
    | APIMessageComponentInteraction
    | APIApplicationCommandAutocompleteInteraction
    | APIModalSubmitInteraction;

Just doing "options" in interaction would probably work, or importing isChatInputApplicationCommandInteraction()... depends on your use case.

By the way, discord.js isn't really built for HTTP interactions. discord-interactions is, however

alpine radish
quick jay
quick jay
#

Thought while I have access to the package as it's already installed, I could maybe typeguard my interaction object

alpine radish
#

And for your initial question: compare interaction.data.type against ApplicationCommandType enum, but at that point importing the typeguard mentioned by jiralite would be the easier solution

quick jay