-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinteractions.go
213 lines (187 loc) · 5.79 KB
/
interactions.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
package rest
import (
"bytes"
"context"
"encoding/json"
"fmt"
"mime/multipart"
"net/http"
"net/url"
"github.com/Postcord/objects"
"github.com/google/go-querystring/query"
)
func (c *Client) CreateInteractionResponse(ctx context.Context, interactionID objects.SnowflakeObject, token string, response *objects.InteractionResponse) error {
var contentType string
var body []byte
if len(response.Data.Files) > 0 {
buffer := new(bytes.Buffer)
m := multipart.NewWriter(buffer)
for n, file := range response.Data.Files {
a, err := file.GenerateAttachment(objects.Snowflake(n+1), m)
if err != nil {
continue
}
response.Data.Attachments = append(response.Data.Attachments, a)
}
if w, err := m.CreateFormField("payload_json"); err != nil {
return err
} else {
if err := json.NewEncoder(w).Encode(response); err != nil {
return err
}
}
contentType = m.FormDataContentType()
if err := m.Close(); err != nil {
return err
}
body = buffer.Bytes()
} else {
contentType = JsonContentType
var err error
if body, err = json.Marshal(response); err != nil {
return err
}
}
return NewRequest().
Method(http.MethodPost).
WithContext(ctx).
Path(fmt.Sprintf(CreateInteractionResponseFmt, interactionID.GetID(), token)).
Body(body).
ContentType(contentType).
OmitAuth().
Send(c)
}
func (c *Client) GetOriginalInteractionResponse(ctx context.Context, applicationID objects.SnowflakeObject, token string) (*objects.Message, error) {
msg := &objects.Message{}
err := NewRequest().
Method(http.MethodGet).
WithContext(ctx).
Path(fmt.Sprintf(OriginalInteractionResponseFmt, applicationID.GetID(), token)).
Bind(msg).
OmitAuth().
Send(c)
return msg, err
}
func (c *Client) EditOriginalInteractionResponse(ctx context.Context, applicationID objects.SnowflakeObject, token string, params *EditWebhookMessageParams) (*objects.Message, error) {
data, err := json.Marshal(params)
if err != nil {
return nil, err
}
msg := &objects.Message{}
err = NewRequest().
Method(http.MethodPatch).
WithContext(ctx).
Path(fmt.Sprintf(OriginalInteractionResponseFmt, applicationID.GetID(), token)).
Body(data).
ContentType(JsonContentType).
Bind(msg).
OmitAuth().
Send(c)
return msg, err
}
func (c *Client) DeleteOriginalInteractionResponse(ctx context.Context, applicationID objects.SnowflakeObject, token string) error {
return NewRequest().
Method(http.MethodDelete).
WithContext(ctx).
Path(fmt.Sprintf(OriginalInteractionResponseFmt, applicationID.GetID(), token)).
OmitAuth().
Send(c)
}
type CreateFollowupMessageParams struct {
Content string `json:"content,omitempty" url:"-"`
TTS bool `json:"tts,omitempty" url:"-"`
Files []*objects.DiscordFile `json:"-" url:"-"`
Embeds []*objects.Embed `json:"embeds,omitempty" url:"-"`
AllowedMentions *objects.AllowedMentions `json:"allowed_mentions,omitempty" url:"-"`
Components []*objects.Component `json:"components,omitempty" url:"-"`
Attachments []*objects.Attachment `json:"attachments,omitempty" url:"-"`
Flags objects.MessageFlag `json:"flags,omitempty" url:"-"`
}
func (c *Client) CreateFollowupMessage(ctx context.Context, applicationID objects.SnowflakeObject, token string, params *CreateFollowupMessageParams) (*objects.Message, error) {
var contentType string
var body []byte
if len(params.Files) > 0 {
buffer := new(bytes.Buffer)
m := multipart.NewWriter(buffer)
for n, file := range params.Files {
a, err := file.GenerateAttachment(objects.Snowflake(n+1), m)
if err != nil {
continue
}
params.Attachments = append(params.Attachments, a)
}
if w, err := m.CreateFormField("payload_json"); err != nil {
return nil, err
} else {
if err := json.NewEncoder(w).Encode(params); err != nil {
return nil, err
}
}
contentType = m.FormDataContentType()
if err := m.Close(); err != nil {
return nil, err
}
body = buffer.Bytes()
} else {
contentType = JsonContentType
var err error
if body, err = json.Marshal(params); err != nil {
return nil, err
}
}
u, err := url.Parse(fmt.Sprintf(WebhookWithTokenFmt, applicationID.GetID(), token))
if err != nil {
return nil, err
}
v, err := query.Values(params)
if err != nil {
return nil, err
}
u.RawQuery = v.Encode()
msg := &objects.Message{}
err = NewRequest().
Method(http.MethodPost).
WithContext(ctx).
Path(u.String()).
ContentType(contentType).
Body(body).
Bind(msg).
OmitAuth().Send(c)
return msg, err
}
func (c *Client) GetFollowupMessage(ctx context.Context, applicationID objects.SnowflakeObject, token string, messageID objects.SnowflakeObject) (*objects.Message, error) {
msg := &objects.Message{}
err := NewRequest().
Method(http.MethodGet).
WithContext(ctx).
Path(fmt.Sprintf(WebhookMessageFmt, applicationID.GetID(), token, messageID.GetID())).
Bind(msg).
OmitAuth().
Send(c)
return msg, err
}
func (c *Client) EditFollowupMessage(ctx context.Context, applicationID objects.SnowflakeObject, token string, messageID objects.SnowflakeObject, params *EditWebhookMessageParams) (*objects.Message, error) {
data, err := json.Marshal(params)
if err != nil {
return nil, err
}
msg := &objects.Message{}
err = NewRequest().
Method(http.MethodPatch).
WithContext(ctx).
Path(fmt.Sprintf(WebhookMessageFmt, applicationID.GetID(), token, messageID.GetID())).
Body(data).
ContentType(JsonContentType).
Bind(msg).
OmitAuth().
Send(c)
return msg, err
}
func (c *Client) DeleteFollowupMessage(ctx context.Context, applicationID objects.SnowflakeObject, token string, messageID objects.SnowflakeObject) error {
return NewRequest().
Method(http.MethodDelete).
WithContext(ctx).
Path(fmt.Sprintf(WebhookMessageFmt, applicationID.GetID(), token, messageID.GetID())).
OmitAuth().
Send(c)
}