Skip to content
This repository was archived by the owner on Oct 13, 2022. It is now read-only.

Commit 80fe6a8

Browse files
committed
Add option base with choices
1 parent 08fb815 commit 80fe6a8

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { isArray, isEmpty } from 'lodash';
2+
3+
import {
4+
ApplicationCommandOptionChoiceStructure,
5+
ApplicationCommandOptionStructure,
6+
ApplicationCommandOptionTypes,
7+
Choices,
8+
} from '@src/interfaces';
9+
10+
import SlashCommandOption from '@common/SlashCommandOption';
11+
12+
/*
13+
|--------------------------------------------------------------------------
14+
| SlashCommandBuilder::Commons -> SlashCommandOptionWithChoices
15+
|--------------------------------------------------------------------------
16+
|
17+
| ...
18+
|
19+
*/
20+
21+
export default class SlashCommandOptionWithChoices<A extends number | string> extends SlashCommandOption {
22+
private static readonly MAX_ITEMS: number = 25;
23+
24+
constructor(type: ApplicationCommandOptionTypes) {
25+
super(type);
26+
}
27+
28+
public readonly choices: Array<ApplicationCommandOptionChoiceStructure> = [];
29+
30+
public addChoice(name: string, value: A): this {
31+
const maxItems: number = SlashCommandOptionWithChoices.MAX_ITEMS - 1;
32+
33+
if (this.choices.length > maxItems) {
34+
throw new Error(`Choices cannot exceed ${SlashCommandOptionWithChoices.MAX_ITEMS} items`);
35+
}
36+
37+
Reflect.set(this, 'choices', [
38+
...this.choices,
39+
{ name, value },
40+
]);
41+
42+
return this;
43+
}
44+
45+
public addChoices(choices: Array<Choices<A>>): this {
46+
if (isEmpty(choices) || !isArray(choices)) {
47+
throw new Error('The `value` parameter is not an array or is empty');
48+
}
49+
50+
for (const { name, value } of choices) {
51+
this.addChoice(name, value);
52+
}
53+
54+
return this;
55+
}
56+
57+
public override toJSON(): ApplicationCommandOptionStructure {
58+
return {
59+
...super.toJSON(),
60+
choices: this.choices,
61+
};
62+
}
63+
}

0 commit comments

Comments
 (0)