codinglab-io / discord-bot

Discord bot for Codinglab's server.
https://discord.com/invite/codinglab-174169014568878080
MIT License
19 stars 14 forks source link

fix: slash command handler subcommands keys aren't type-safe #56

Open neolectron opened 1 year ago

neolectron commented 1 year ago

Problem

Subcommands keys aren't typechecked.

{
  schema: new SlashCommandBuilder()
    .setName('myCommand')
    .addSubcommand((subcommand) =>
      subcommand.setName('subcommand1').setDescription('do 1'),
    )
    .addSubcommand((subcommand) =>
      subcommand
        .setName('subcommand2')
        .setDescription('do 2'),
    )
    .toJSON(),
  handler: {
    somethingNotLegit: async (interaction) => {},
    anyKeyCanBeHere: async (interaction) => {},
    // ^ ------------ Not legit
  },
}

Solution

Override the type of schema with subcommands strings as const (or check if they aren't in the type already). Then retrieve the subcommands keys with a type helper.