Correct 'type' for Interaction Object?

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;
}
},
};
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!
2 Replies
d.js toolkit
d.js toolkit•3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - 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!
ThatSameer
ThatSameer•3mo ago
HTTP Interactions. (sorry for the delay) I could add the options field but would have to overwrite the type to satisfy it For other use cases such as to help with slash command building, registering commands etc. Not using it for gateway client Thought while I have access to the package as it's already installed, I could maybe typeguard my interaction object That is a brilliant idea. Didn't think of that Thank you. I will give that a go tonight once I'm back on 🫡