forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_command.go
216 lines (187 loc) · 6.88 KB
/
application_command.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package disgord
import (
"context"
"fmt"
"net/http"
"github.com/andersfylling/disgord/internal/httd"
)
type ApplicationCommandType int
const (
_ ApplicationCommandType = iota
ApplicationCommandChatInput
ApplicationCommandUser
ApplicationCommandMessage
)
type ApplicationCommandOptionChoice struct {
Name string `json:"name"`
Value interface{} `json:"value"`
}
type ApplicationCommandOption struct {
Type OptionType `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Required bool `json:"required"`
Choices []*ApplicationCommandOptionChoice `json:"choices"`
Options []*ApplicationCommandOption `json:"options"`
ChannelTypes []ChannelType `json:"channel_types"`
MinValue float64 `json:"min_value"`
MaxValue float64 `json:"max_value"`
Autocomplete bool `json:"autocomplete"`
}
type ApplicationCommandDataOption struct {
Name string `json:"name"`
Type OptionType `json:"type"`
Value interface{} `json:"value"`
Options []*ApplicationCommandDataOption `json:"options"`
}
type ApplicationCommandPermissionType int
const (
_ ApplicationCommandPermissionType = iota
ApplicationCommandPermissionRole
ApplicationCommandPermissionUser
)
type ApplicationCommandPermissions struct {
ID Snowflake `json:"id"`
Type ApplicationCommandPermissionType `json:"type"`
Permission bool `json:"permission"`
}
type GuildApplicationCommandPermissions struct {
ID Snowflake `json:"id"`
ApplicationID Snowflake `json:"application_id"`
GuildID Snowflake `json:"guild_id"`
Permissions []*ApplicationCommandPermissions `json:"permissions"`
}
type ApplicationCommand struct {
ID Snowflake `json:"id"`
Type ApplicationCommandType `json:"type"`
ApplicationID Snowflake `json:"application_id"`
GuildID Snowflake `json:"guild_id"`
Name string `json:"name"`
Description string `json:"description"`
Options []*ApplicationCommandOption `json:"options"`
DefaultPermission bool `json:"default_permission,omitempty"`
}
type CreateApplicationCommand struct {
Name string `json:"name"`
Description string `json:"description"`
Type ApplicationCommandType `json:"type,omitempty"`
Options []*ApplicationCommandOption `json:"options,omitempty"`
DefaultPermission bool `json:"default_permission,omitempty"`
}
type UpdateApplicationCommand struct {
Name *string `json:"name,omitempty"`
DefaultPermission *bool `json:"default_permission,omitempty"`
Description *string `json:"description,omitempty"`
Options *[]*ApplicationCommandOption `json:"options,omitempty"`
}
type ApplicationCommandQueryBuilder interface {
WithContext(ctx context.Context) ApplicationCommandQueryBuilder
Global() ApplicationCommandFunctions
Guild(guildID Snowflake) ApplicationCommandFunctions
}
type ApplicationCommandFunctions interface {
Delete(commandId Snowflake) error
Create(command *CreateApplicationCommand) error
Update(commandId Snowflake, command *UpdateApplicationCommand) error
}
type applicationCommandFunctions struct {
appID Snowflake
flags Flag
client *Client
guildID Snowflake
ctx context.Context
}
func (c *applicationCommandFunctions) applicationID() Snowflake {
appID := c.appID
if appID.IsZero() {
c.client.mu.Lock()
appID = c.client.applicationID
c.client.mu.Unlock()
}
return appID
}
func applicationCommandFactory() interface{} {
return &ApplicationCommand{}
}
func (c *applicationCommandFunctions) Create(command *CreateApplicationCommand) error {
var endpoint string
if c.guildID == 0 {
endpoint = fmt.Sprintf("/applications/%d/commands", c.applicationID())
} else {
endpoint = fmt.Sprintf("/applications/%d/guilds/%d/commands", c.applicationID(), c.guildID)
}
ctx := c.ctx
req := &httd.Request{
Endpoint: endpoint,
Method: http.MethodPost,
Body: command,
Ctx: ctx,
ContentType: httd.ContentTypeJSON,
}
r := c.client.newRESTRequest(req, c.flags)
r.factory = applicationCommandFactory
_, err := r.Execute()
return err
}
func (c *applicationCommandFunctions) Update(commandID Snowflake, command *UpdateApplicationCommand) error {
var endpoint string
ctx := c.ctx
if c.guildID == 0 {
endpoint = fmt.Sprintf("applications/%d/commands/%d", c.applicationID(), commandID)
} else {
endpoint = fmt.Sprintf("applications/%d/guilds/%d/commands/%d", c.applicationID(), c.guildID, commandID)
}
req := &httd.Request{
Endpoint: endpoint,
Method: http.MethodPatch,
Body: command,
Ctx: ctx,
ContentType: httd.ContentTypeJSON,
}
r := c.client.newRESTRequest(req, c.flags)
r.factory = applicationCommandFactory
_, err := r.Execute()
return err
}
func (c *applicationCommandFunctions) Delete(commandID Snowflake) error {
var endpoint string
ctx := c.ctx
if c.guildID == 0 {
endpoint = fmt.Sprintf("applications/%d/commands/%d", c.applicationID(), commandID)
} else {
endpoint = fmt.Sprintf("applications/%d/guilds/%d/commands/%d", c.applicationID(), c.guildID, commandID)
}
req := &httd.Request{
Endpoint: endpoint,
Method: http.MethodDelete,
Ctx: ctx,
ContentType: httd.ContentTypeJSON,
}
r := c.client.newRESTRequest(req, c.flags)
r.factory = applicationCommandFactory
_, err := r.Execute()
return err
}
type applicationCommandQueryBuilder struct {
ctx context.Context
client *Client
appID Snowflake
flags Flag
}
func (q applicationCommandQueryBuilder) Guild(guildId Snowflake) ApplicationCommandFunctions {
return &applicationCommandFunctions{appID: q.appID, ctx: q.ctx, guildID: guildId, client: q.client, flags: q.flags}
}
func (q applicationCommandQueryBuilder) Global() ApplicationCommandFunctions {
return &applicationCommandFunctions{appID: q.appID, ctx: q.ctx, client: q.client, flags: q.flags}
}
func (q applicationCommandQueryBuilder) WithFlags(flags ...Flag) ApplicationCommandQueryBuilder {
q.flags = mergeFlags(flags)
return &q
}
func (q applicationCommandQueryBuilder) WithContext(ctx context.Context) ApplicationCommandQueryBuilder {
q.ctx = ctx
return &q
}
func (c clientQueryBuilder) ApplicationCommand(id Snowflake) ApplicationCommandQueryBuilder {
return &applicationCommandQueryBuilder{client: c.client, appID: id}
}