discord.js icon indicating copy to clipboard operation
discord.js copied to clipboard

Generic types for SlashCommand Options

Open joaisa17 opened this issue 3 years ago • 3 comments
trafficstars

Which package is the feature request for?

discord.js

Feature

I'm obsessed with autocomplete, and would like to pass an interface into any of the SlashCommand Option classes, to provide autocomplete when adding e.g choices:

Suggested usage:

import { SlashCommandBuilder, SlashCommandStringOption, SlashCommandIntegerOption } from 'discord.js';

interface Options {
    somestring: string;
    somestringchoice: 'foo'|'bar';

    somenumber: number;
}

new SlashCommandBuilder()
    .setName('example')
    .setDescription('Example command')

    .addStringOption(
        new SlashCommandStringOption<Options>()
            .setName('somestring') // Provide autocomplete for keyof Options (filter string types)
            .setDescription('Any string')
    )

    .addStringOption(
        new SlashCommandStringOption<Options>()
            .setName('somestringchoice') // Provide autocomplete for keyof Options (filter literal string types if possible)
            .setDescription('String choice')
            .setChoices(
                { name: 'foo', value: 'foo' }, // Provide autocomplete for Options[name]
                { name: 'bar', value: 'bar' }
            )
    )

    .addIntegerOption(
        new SlashCommandIntegerOption<Options>()
            .setName('somenumber') // Provide autocomplete for keyof Options (filter number types)
            .setDescription('Number choice')
    )

Ideal solution or implementation

Add an optional generic to each of the Option classes

Alternative solutions or implementations

No response

Other context

Adding a generic to the SlashCommandBuilder class itself would probably mean that you will have to rewrite the entire usage of the SlashCommandBuilder, as I do not believe a passed class can use generics from another, e.g you pass the Options to the Builder itself, in which case the StringOption class won't resolve the options.

joaisa17 avatar Oct 14 '22 12:10 joaisa17

Duplicate of #8218

cobaltt7 avatar Oct 14 '22 12:10 cobaltt7

This isn't a duplicate of #8218 as that issue is for getting type in received values, not in the builder methods. And I don't see why you want a autocomplete in builders. Both interface and builders are defined by you so why you want to duplicate your codes with a interface as well as builder

imranbarbhuiya avatar Oct 14 '22 13:10 imranbarbhuiya