-
Notifications
You must be signed in to change notification settings - Fork 3
/
interactions-api.go
174 lines (147 loc) · 4.56 KB
/
interactions-api.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
package corde
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"github.com/Karitham/corde/internal/rest"
)
// returns the body and its content-type
func toBody(w io.Writer, m *InteractionRespData) (string, error) {
contentType := "application/json"
payloadJSON := &bytes.Buffer{}
err := json.NewEncoder(payloadJSON).Encode(m)
if err != nil {
return "", err
}
if len(m.Attachments) < 1 {
payloadJSON.WriteTo(w)
return contentType, nil
}
mw := multipart.NewWriter(w)
defer mw.Close()
contentType = mw.FormDataContentType()
mw.WriteField("payload_json", payloadJSON.String())
if err := writeAttachments(mw, m.Attachments); err != nil {
return contentType, err
}
return contentType, nil
}
func writeAttachments(mw *multipart.Writer, attachments []Attachment) error {
for i, f := range attachments {
if f.ID == 0 {
f.ID = Snowflake(i)
}
ff, CFerr := mw.CreateFormFile(fmt.Sprintf("files[%d]", i), f.Filename)
if CFerr != nil {
return CFerr
}
if _, CopyErr := io.Copy(ff, f.Body); CopyErr != nil {
return CopyErr
}
}
return nil
}
// GetOriginalInteraction returns the original response to an Interaction
//
// https://discord.com/developers/docs/interactions/receiving-and-responding#get-original-interaction-response
func (m *Mux) GetOriginalInteraction(token string) (*InteractionRespData, error) {
data := &InteractionRespData{}
_, err := rest.DoJSON(m.Client, rest.Req("/webhooks", m.AppID, token, "messages/@original").Get(m.authorize), data)
if err != nil {
return nil, err
}
return data, nil
}
// EditOriginalInteraction to edit your initial response to an Interaction
//
// https://discord.com/developers/docs/interactions/receiving-and-responding#edit-original-interaction-response
func (m *Mux) EditOriginalInteraction(token string, data InteractionResponder) error {
body := &bytes.Buffer{}
contentType, err := toBody(body, data.InteractionRespData())
if err != nil {
return err
}
_, err = m.Client.Do(
rest.Req("/webhooks", m.AppID, token, "messages/@original").
AnyBody(body).Patch(m.authorize, rest.ContentType(contentType)),
)
if err != nil {
return err
}
return nil
}
// DeleteOriginalInteraction to delete your initial response to an Interaction
//
// https://discord.com/developers/docs/interactions/receiving-and-responding#edit-original-interaction-response
func (m *Mux) DeleteOriginalInteraction(token string) error {
_, err := m.Client.Do(
rest.Req("/webhooks", m.AppID, token, "messages/@original").
Delete(m.authorize),
)
if err != nil {
return err
}
return nil
}
// FollowUpInteraction follows up a response to an Interaction
//
// https://discord.com/developers/docs/interactions/receiving-and-responding#followup-messages
func (m *Mux) FollowUpInteraction(token string, data InteractionResponder) error {
body := &bytes.Buffer{}
contentType, err := toBody(body, data.InteractionRespData())
if err != nil {
return err
}
_, err = m.Client.Do(
rest.Req("/webhooks", m.AppID, token).
AnyBody(body).Post(m.authorize, rest.ContentType(contentType)),
)
if err != nil {
return err
}
return nil
}
// GetFollowUpInteraction returns the response to a FollowUpInteraction
//
// https://discord.com/developers/docs/interactions/receiving-and-responding#get-followup-message
func (m *Mux) GetFollowUpInteraction(token string, messageID Snowflake) (*InteractionRespData, error) {
data := &InteractionRespData{}
_, err := rest.DoJSON(m.Client, rest.Req("/webhooks", m.AppID, token, "messages", messageID).Get(m.authorize), data)
if err != nil {
return nil, err
}
return data, nil
}
// EditFollowUpInteraction to edit a response to a FollowUpInteraction
//
// https://discord.com/developers/docs/interactions/receiving-and-responding#edit-followup-message
func (m *Mux) EditFollowUpInteraction(token string, messageID Snowflake, data InteractionResponder) error {
body := &bytes.Buffer{}
contentType, err := toBody(body, data.InteractionRespData())
if err != nil {
return err
}
_, err = m.Client.Do(
rest.Req("/webhooks", m.AppID, token, "messages", messageID).
AnyBody(body).Patch(m.authorize, rest.ContentType(contentType)),
)
if err != nil {
return err
}
return nil
}
// DeleteFollowUpInteraction to delete a response to a FollowUpInteraction
//
// https://discord.com/developers/docs/interactions/receiving-and-responding#delete-followup-message
func (m *Mux) DeleteFollowUpInteraction(token string, messageID Snowflake) error {
_, err := m.Client.Do(
rest.Req("/webhooks", m.AppID, token, "messages", messageID).
Delete(m.authorize),
)
if err != nil {
return err
}
return nil
}