-
Notifications
You must be signed in to change notification settings - Fork 3
/
register-commands.go
147 lines (128 loc) · 3.97 KB
/
register-commands.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
135
136
137
138
139
140
141
142
143
144
145
146
147
package corde
import (
"encoding/json"
)
// CreateCommander is a command that can be registered
type CreateCommander interface {
createCommand() CreateCommand
}
// CreateOptioner is an interface for all options
type CreateOptioner interface {
createOption() CreateOption
}
// CreateOption is the base option type for creating any sort of option
type CreateOption struct {
Name string `json:"name"`
Type OptionType `json:"type"`
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Choices []Choice[any] `json:"choices,omitempty"`
Options []CreateOptioner `json:"options,omitempty"`
ChannelTypes []ChannelType `json:"channel_types,omitempty"`
MinValue float64 `json:"min_value,omitempty"`
MaxValue float64 `json:"max_value,omitempty"`
Autocomplete bool `json:"autocomplete,omitempty"`
}
func (c CreateOption) createOption() CreateOption {
return c
}
// CreateCommand is a slash command that can be registered to discord
type CreateCommand struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Type CommandType `json:"type,omitempty"`
Options []CreateOptioner `json:"options,omitempty"`
DefaultPermissionDisabled bool `json:"default_permission"`
}
// MarshalJSON implements json.Marshaler
func (c CreateCommand) MarshalJSON() ([]byte, error) {
type createCommand CreateCommand
return json.Marshal(struct {
createCommand
DefaultPermission bool `json:"default_permission"`
}{
createCommand: createCommand(c),
DefaultPermission: !c.DefaultPermissionDisabled,
})
}
// NewSlashCommand returns a new slash command
func NewSlashCommand(name string, description string, options ...CreateOptioner) CreateCommand {
return CreateCommand{
Name: name,
Description: description,
Options: options,
Type: COMMAND_CHAT_INPUT,
}
}
// NewUserCommand returns a new user command
func NewUserCommand(name string) CreateCommand {
return CreateCommand{
Name: name,
Type: COMMAND_USER,
}
}
// NewMessageCommand returns a new message command
func NewMessageCommand(name string) CreateCommand {
return CreateCommand{
Name: name,
Type: COMMAND_MESSAGE,
}
}
func (c CreateCommand) createCommand() CreateCommand {
return CreateCommand{
Name: c.Name,
Description: c.Description,
Options: c.Options,
Type: c.Type,
}
}
// SubcommandOption is an option that is a subcommand
type SubcommandOption struct {
Name string
Description string
Options []CreateOptioner
}
// NewSubcommand returns a new subcommand
func NewSubcommand(name string, description string, options ...CreateOptioner) *SubcommandOption {
return &SubcommandOption{
Name: name,
Description: description,
Options: options,
}
}
func (o *SubcommandOption) createOption() CreateOption {
return CreateOption{
Options: o.Options,
Name: o.Name,
Description: o.Description,
Type: OPTION_SUB_COMMAND,
}
}
func (o *SubcommandOption) MarshalJSON() ([]byte, error) {
return json.Marshal(o.createOption())
}
// SubcommandGroupOption is an option that is a subcommand group
type SubcommandGroupOption struct {
Name string
Description string
Options []CreateOptioner
}
// NewSubcommandGroup returns a new subcommand group
func NewSubcommandGroup(name string, description string, options ...CreateOptioner) *SubcommandGroupOption {
return &SubcommandGroupOption{
Name: name,
Description: description,
Options: options,
}
}
func (o *SubcommandGroupOption) createOption() CreateOption {
return CreateOption{
Options: o.Options,
Name: o.Name,
Description: o.Description,
Type: OPTION_SUB_COMMAND_GROUP,
}
}
func (o *SubcommandGroupOption) MarshalJSON() ([]byte, error) {
return json.Marshal(o.createOption())
}