-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
SlashOption.ts
54 lines (42 loc) · 1.64 KB
/
SlashOption.ts
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
import { of } from "case"
import { SlashOption as SlashOptionX, SlashOptionOptions as SlashOptionOptionsX, VerifyName } from "discordx"
import { InvalidOptionName } from "@errors"
import { constantPreserveDots, sanitizeLocales, setOptionsLocalization } from "@utils/functions"
/**
* Add a slash command option
*
* @param options - Slash option options
* ___
*
* [View Documentation](https://discordx.js.org/docs/decorators/commands/slash-option)
*
* @category Decorator
*/
export const SlashOption = (options: SlashOptionOptions) => {
let localizationSource: TranslationsNestedPaths | null = null
if (options.localizationSource) localizationSource = constantPreserveDots(options.localizationSource) as TranslationsNestedPaths
if (localizationSource) {
options = setOptionsLocalization({
target: 'description',
options,
localizationSource,
})
options = setOptionsLocalization({
target: 'name',
options,
localizationSource,
})
}
options = sanitizeLocales(options)
if (!isValidOptionName(options.name)) throw new InvalidOptionName(options.name)
if (options.nameLocalizations) {
for (const name of Object.values(options.nameLocalizations)) {
if (!isValidOptionName(name)) throw new InvalidOptionName(name)
}
}
if (!options.description) options.description = 'No description provided'
return SlashOptionX(options as SlashOptionOptionsX<VerifyName<string>, string>)
}
const isValidOptionName = (name: string) => {
return ['lower', 'snake'].includes(of(name)) && !name.includes(' ')
}