pkgs/router/src/options.ts (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
import {
type AutocompleteInteraction,
type User,
type Attachment,
type Role,
type GuildBasedChannel,
type ApplicationCommandOptionChoiceData,
type LocalizationMap,
} from "discord.js";
export const OPTION_TYPES = {
subcommand: 1,
subcommandGroup: 2,
string: 3,
integer: 4,
boolean: 5,
user: 6,
channel: 7,
role: 8,
mentionable: 9,
number: 10,
attachment: 11,
} as const;
type OptionType = keyof typeof OPTION_TYPES;
export interface OptionChoice<T> {
name: string;
nameLocalizations?: LocalizationMap;
value: T;
}
export interface Option<T, Req extends boolean> {
type: OptionType;
description?: string;
required: Req;
autocomplete?: (
interaction: AutocompleteInteraction,
value: T,
) => Promise<ApplicationCommandOptionChoiceData[]> | ApplicationCommandOptionChoiceData[];
choices?: OptionChoice<T>[];
nameLocalizations?: LocalizationMap;
descriptionLocalizations?: LocalizationMap;
__type?: T;
}
export interface CommandOption<T, Req extends boolean> extends Option<T, Req> {
description: string;
}
export type InferOptions<O> = {
[K in keyof O]: O[K] extends Option<infer Type, infer Required>
? Required extends true
? Type
: Type | undefined
: never;
};
export type AutocompleteFn<T> = (
interaction: AutocompleteInteraction,
value: T,
) => Promise<ApplicationCommandOptionChoiceData[]> | ApplicationCommandOptionChoiceData[];
export interface OptionBuilder<T> {
(
description: string,
config: {
required: true;
autocomplete?: AutocompleteFn<T>;
choices?: OptionChoice<T>[];
nameLocalizations?: LocalizationMap;
descriptionLocalizations?: LocalizationMap;
},
): CommandOption<T, true>;
(
description: string,
config?: {
required?: false;
autocomplete?: AutocompleteFn<T>;
choices?: OptionChoice<T>[];
nameLocalizations?: LocalizationMap;
descriptionLocalizations?: LocalizationMap;
},
): CommandOption<T, false>;
(config: {
required: true;
autocomplete?: AutocompleteFn<T>;
choices?: OptionChoice<T>[];
nameLocalizations?: LocalizationMap;
descriptionLocalizations?: LocalizationMap;
}): Option<T, true>;
(config?: {
required?: false;
autocomplete?: AutocompleteFn<T>;
choices?: OptionChoice<T>[];
nameLocalizations?: LocalizationMap;
descriptionLocalizations?: LocalizationMap;
}): Option<T, false>;
required(
description: string,
config?: {
autocomplete?: AutocompleteFn<T>;
choices?: OptionChoice<T>[];
nameLocalizations?: LocalizationMap;
descriptionLocalizations?: LocalizationMap;
},
): CommandOption<T, true>;
optional(
description: string,
config?: {
autocomplete?: AutocompleteFn<T>;
choices?: OptionChoice<T>[];
nameLocalizations?: LocalizationMap;
descriptionLocalizations?: LocalizationMap;
},
): CommandOption<T, false>;
}
const createOption = <T>(type: OptionType): OptionBuilder<T> => {
const fn = (
descriptionOrConfig?:
| string
| {
required?: boolean;
autocomplete?: AutocompleteFn<T>;
choices?: OptionChoice<T>[];
nameLocalizations?: LocalizationMap;
descriptionLocalizations?: LocalizationMap;
},
config?: {
required?: boolean;
autocomplete?: AutocompleteFn<T>;
choices?: OptionChoice<T>[];
nameLocalizations?: LocalizationMap;
descriptionLocalizations?: LocalizationMap;
},
): any => {
const hasDescription = typeof descriptionOrConfig === "string";
const description = hasDescription ? descriptionOrConfig : undefined;
const actualConfig = hasDescription ? config : descriptionOrConfig;
return {
type,
description,
required: actualConfig?.required ?? false,
autocomplete: actualConfig?.autocomplete,
choices: actualConfig?.choices,
nameLocalizations: actualConfig?.nameLocalizations,
descriptionLocalizations: actualConfig?.descriptionLocalizations,
};
};
fn.required = (
description: string,
config?: {
autocomplete?: AutocompleteFn<T>;
choices?: OptionChoice<T>[];
nameLocalizations?: LocalizationMap;
descriptionLocalizations?: LocalizationMap;
},
) => {
return fn(description, {
required: true,
autocomplete: config?.autocomplete,
choices: config?.choices,
nameLocalizations: config?.nameLocalizations,
descriptionLocalizations: config?.descriptionLocalizations,
});
};
fn.optional = (
description: string,
config?: {
autocomplete?: AutocompleteFn<T>;
choices?: OptionChoice<T>[];
nameLocalizations?: LocalizationMap;
descriptionLocalizations?: LocalizationMap;
},
) => {
return fn(description, {
required: false,
autocomplete: config?.autocomplete,
choices: config?.choices,
nameLocalizations: config?.nameLocalizations,
descriptionLocalizations: config?.descriptionLocalizations,
});
};
return fn as OptionBuilder<T>;
};
export const option = {
string: createOption<string>("string"),
integer: createOption<number>("integer"),
number: createOption<number>("number"),
boolean: createOption<boolean>("boolean"),
user: createOption<User>("user"),
channel: createOption<GuildBasedChannel>("channel"),
role: createOption<Role>("role"),
mentionable: createOption<User | Role>("mentionable"),
attachment: createOption<Attachment>("attachment"),
};
|