I am currently working on a Discord Bot and I am kinda new to TypeScript and need some help with my command class…
This is my current class:
export class Command {
constructor(
public options: {
...
data: ApplicationCommandData & {
type: ApplicationCommandType;
contexts?: Context[];
integration_types?: IntegrationTypes[];
};
execute: (options: { client: DiscordClient; interaction: CommandInteraction }) => any;
...
}
) {}
}
This is how to use it:
export default new Command({
data: {
name: 'ping',
description: 'Replies with Pong!',
type: ApplicationCommandType.ChatInput,
contexts: [Context.GUILD, Context.BOT_DM, Context.PRIVATE_CHANNEL],
integration_types: [IntegrationTypes.GUILD_INSTALL, IntegrationTypes.USER_INSTALL],
},
execute({ interaction}) {
interaction.reply({ content: 'Pong!', ephemeral: true });
},
});
The problem with this is that no matter what type (User
, Message
or ChatInput
) the command is, the interaction will always be just be the base CommandInteraction
and it’s not possible to get string options (interaction.options.getString()
) from it. I would have to put if (!interaction.isChatInputCommand()) return;
at the top of every ChatInputCommand that uses string options.
Now I need some help to create a generic class (if possible) to solve this issue. I want the type of my class/command to determine the interaction type of the execute function.
Here’s a rough (not working) example of what I have in mind:
export class Command<Type> {
constructor(
public options: {
...
data: ApplicationCommandData & {
type: Type;
contexts?: Context[];
integration_types?: IntegrationTypes[];
};
execute: (options: { client: DiscordClient; interaction: interactions[type] }) => any;
...
}
) {}
}
I want the type to be taken from the command.options.data.type
property and use it to go through an interactions object/array to obtain the corresponding interaction for the type of command!
Please let me know if this is possible.
Otherwise I’ll just have to make 3 classes for each type of command (User
, Message
, ChatInput
)…
I tried lots of different stuff but in the end I could not find a solution.