#get the subcommand options

19 messages · Page 1 of 1 (latest)

icy goblet
#

hi, how can i get the options that were entered of a subcommand?

viscid flint
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

icy goblet
#

import {
  ChannelType,
  CommandInteraction,
  SlashCommandBuilder,
  SlashCommandSubcommandGroupBuilder,
} from 'discord.js'
// import { CreateShoe, GetShoes } from '../api/api'

export default {
  data: new SlashCommandBuilder()
    .setName('inventory')
    .setDescription('List or show your inventory')
    .addSubcommand(subcommand =>
      subcommand
        .setName('add')
        .setDescription('Add a product to your inventory')
        .addStringOption(option =>
          option
            .setName('sku')
            .setDescription('This is a required option')
            .setRequired(true)
        )
        .addStringOption(o => o.setName('skuuu').setDescription('Skusku'))
    )
    .addSubcommand(subcommand =>
      subcommand.setName('server').setDescription('Info about the server')
    ),
  async execute(interaction: CommandInteraction) {
    // CreateShoe({
    //     Name: 'Valles shoe',
    //     PriceBuy: 12,
    //     PriceSell: 100,
    //     Quantity: 1,
    //     Region: 'de',
    //     Size: '10',
    //     Style: 'test',
    //     Type: 'buy',
    //     UserID: 12,
    //     LocalOnly: false,
    // })

    // Print inputted string options to console choices 
    
    console.log(interaction.options.getString('sku'))

    console.log(interaction.options)

    interaction.reply({
      content: 'This is a created shoe command',
      ephemeral: true,
    })
  },
}```
#

return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/commands/inventory.ts:44:37 - error TS2339: Property 'getString' does not exist on type 'Omit<CommandInteractionOptionResolver<CacheType>, "getMessage" | "getFocused" | "getMentionable" | "getRole" | "getAttachment" | ... 6 more ... | "getSubcommand">'.

44     console.log(interaction.options.getString('sku'))
                                       ~~~~~~~~~

    at createTSError (/Users/vallezw/projects/autopilot/bot/node_modules/.pnpm/[email protected]_lwgqdwokjtwlohdqtbb6s252kq/node_modules/ts-node/src/index.ts:859:12)
    at reportTSError (/Users/vallezw/projects/autopilot/bot/node_modules/.pnpm/[email protected]_lwgqdwokjtwlohdqtbb6s252kq/node_modules/ts-node/src/index.ts:863:19)
    at getOutput (/Users/vallezw/projects/autopilot/bot/node_modules/.pnpm/[email protected]_lwgqdwokjtwlohdqtbb6s252kq/node_modules/ts-node/src/index.ts:1077:36)
    at Object.compile (/Users/vallezw/projects/autopilot/bot/node_modules/.pnpm/[email protected]_lwgqdwokjtwlohdqtbb6s252kq/node_modules/ts-node/src/index.ts:1433:41)
    at Module.m._compile (/Users/vallezw/projects/autopilot/bot/node_modules/.pnpm/[email protected]_lwgqdwokjtwlohdqtbb6s252kq/node_modules/ts-node/src/index.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/vallezw/projects/autopilot/bot/node_modules/.pnpm/[email protected]_lwgqdwokjtwlohdqtbb6s252kq/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1133:32)
    at Function.Module._load (node:internal/modules/cjs/loader:972:12)
    at Module.require (node:internal/modules/cjs/loader:1157:19) {
  diagnosticCodes: [ 2339 ]
}
 ELIFECYCLE  Command failed with exit code 1.```
silver shoal
#

You’ll want to either typecast interaction as a ChatInputCommandInteraction since getString isn’t really possible on a context menu. CommandInteraction has context menu interactions and chat input interactions together since they’re both application commands.

you could also type guard and check interaction.isChatInputCommand()

silver shoal
#

Great 😊

icy goblet
#

how would ya recommend dealing with subcommands and handling interactions uniquely for each subcommand?

#

what do ya think would be the best way to do that

silver shoal
#

you can check for the subcommand used with interaction.options.getSubcommand(), but this doesn’t take params inside the method so you can just check if it’s equal to the sub cmd name

icy goblet
#

but is that the only way to catch interactions for multiple sub cmds?

#

doesnt seem clean at all dead

silver shoal
#

unfortunately yes

icy goblet
#

aight

#

thank you

#

will have to deal with it somehow dead

silver shoal
#

I’m sure you’ll figure it out

uncut sparrow