-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
134 lines (116 loc) · 3.27 KB
/
options.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
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
package corde
import (
"encoding/json"
)
type OptionType int
const (
OPTION_SUB_COMMAND OptionType = iota + 1
OPTION_SUB_COMMAND_GROUP
OPTION_STRING
OPTION_INTEGER
OPTION_BOOLEAN
OPTION_USER
OPTION_CHANNEL
OPTION_ROLE
OPTION_MENTIONABLE
OPTION_NUMBER
)
type CommandType int
const (
COMMAND_CHAT_INPUT CommandType = iota + 1
COMMAND_USER
COMMAND_MESSAGE
)
// Command is a Discord application command
type Command struct {
Name string `json:"name,omitempty"`
ID Snowflake `json:"id,omitempty"`
Type CommandType `json:"type,omitempty"`
ApplicationID Snowflake `json:"application_id,omitempty"`
GuildID Snowflake `json:"guild_id,omitempty"`
Description string `json:"description,omitempty"`
Options []Option `json:"options,omitempty"`
DefaultPermission bool `json:"default_permission,omitempty"`
Version Snowflake `json:"version,omitempty"`
}
// Option is an option for an application Command
type Option struct {
Name string `json:"name"`
Type OptionType `json:"type"`
Value JsonRaw `json:"value"`
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Options []Option `json:"options,omitempty"`
Choices []Choice[any] `json:"choices,omitempty"`
Focused bool `json:"focused,omitempty"`
}
// Choice is an application Command choice
type Choice[T any] struct {
Name string `json:"name"`
Value T `json:"value"`
}
// OptionsInteractions is the options for an Interaction
type OptionsInteractions map[string]JsonRaw
// UnmarshalJSON implements json.Unmarshaler
func (o *OptionsInteractions) UnmarshalJSON(b []byte) error {
type opt struct {
Name string `json:"name"`
Value JsonRaw `json:"value"`
Type OptionType `json:"type"`
Options []opt `json:"options"`
Focused bool `json:"focused"`
}
var opts []opt
if err := json.Unmarshal(b, &opts); err != nil {
return err
}
// max is 3 deep, as per discord's docs
m := make(map[string]JsonRaw)
for _, opt := range opts {
switch {
case OPTION_SUB_COMMAND_GROUP == opt.Type:
opt.Value = []byte(opt.Name)
opt.Name = RouteInteractionSubcommandGroup
case OPTION_SUB_COMMAND == opt.Type:
opt.Value = []byte(opt.Name)
opt.Name = RouteInteractionSubcommand
case opt.Focused:
m[RouteInteractionFocused] = []byte(opt.Name)
}
m[opt.Name] = opt.Value
for _, opt2 := range opt.Options {
switch {
case OPTION_SUB_COMMAND == opt2.Type:
opt2.Value = []byte(opt2.Name)
opt2.Name = RouteInteractionSubcommand
case opt2.Focused:
m[RouteInteractionFocused] = []byte(opt2.Name)
}
m[opt2.Name] = opt2.Value
for _, opt3 := range opt2.Options {
if opt3.Focused {
m[RouteInteractionFocused] = []byte(opt3.Name)
}
m[opt3.Name] = opt3.Value
}
}
}
*o = m
return nil
}
// MarshalJSON implements json.Marshaler
func (o OptionsInteractions) MarshalJSON() ([]byte, error) {
type opt struct {
Name string `json:"name"`
Value any `json:"value"`
}
opts := make([]opt, len(o))
for k, v := range o {
opts = append(opts, opt{k, v})
}
b, err := json.Marshal(&opts)
if err != nil {
return nil, err
}
return b, nil
}