Home > Blockchain >  Discord.js cannot add option to slash commands
Discord.js cannot add option to slash commands

Time:12-11

I have begun to migrate to discordjs v13 and came across this error: Property 'options' does not exist on type 'Interaction '.

runEvent:

export interface runEvent {
  interaction: Interaction,
  client: Client,
  args: string[],
}

Where it goes wrong

export const execute = ({interaction, client, args}:runEvent) => {
    console.log(interaction.options.getString('name'))
}                          ^^^^^^^^

What causes this to throw this error? When I log interaction itself I can see it has property options. So what throws this error?

As a side note changing interaction: Interaction to interaction: any fixes this issue instantly. I'm just curious what throws this error

CodePudding user response:

This is because Interaction is any type of interaction. This includes buttons, select menus and context menus, which don't have the options property. You can cast it to CommandInteraction, but it's recommended you use the Typeguards (.isCommand()) so that if it is a different type, it will return instead

export const execute = ({ interaction, client, args }: runEvent) => {
    if (!interaction.isCommand()) return;
    console.log(interaction.options.getString('name'))
}  
  • Related