-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinteraction.go
40 lines (33 loc) · 1.05 KB
/
interaction.go
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
package tools
import (
"github.com/switchupcb/disgo"
)
// OptionsToMap parses an array of options and suboptions into an OptionMap.
func OptionsToMap(
optionMap map[string]*disgo.ApplicationCommandInteractionDataOption,
options []*disgo.ApplicationCommandInteractionDataOption,
amount int,
) map[string]*disgo.ApplicationCommandInteractionDataOption {
if optionMap == nil {
optionMap = make(map[string]*disgo.ApplicationCommandInteractionDataOption, amount)
}
// add suboptions (slice by value is the most performant)
for _, option := range options {
optionMap[option.Name] = option
if len(option.Options) != 0 {
OptionsToMap(optionMap, option.Options, amount)
}
}
return optionMap
}
// NumOptions determines the amount of options (and suboptions) in a given array of options.
func NumOptions(options []*disgo.ApplicationCommandInteractionDataOption) int {
amount := len(options)
// count the amount of suboptions.
for _, option := range options {
if len(option.Options) != 0 {
amount += NumOptions(option.Options)
}
}
return amount
}