Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit 77175ef

Browse files
author
jonas747
committed
add file support to interaction follow ups and webhooks
1 parent 434deda commit 77175ef

File tree

2 files changed

+97
-7
lines changed

2 files changed

+97
-7
lines changed

restapi.go

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,93 @@ func (s *Session) WebhookExecute(webhookID int64, token string, wait bool, data
21942194
return
21952195
}
21962196

2197+
// WebhookExecuteComplex executes a webhook.
2198+
// webhookID: The ID of a webhook.
2199+
// token : The auth token for the webhook
2200+
func (s *Session) WebhookExecuteComplex(webhookID int64, token string, wait bool, data *WebhookParams) (m *Message, err error) {
2201+
uri := EndpointWebhookToken(webhookID, token)
2202+
2203+
if wait {
2204+
uri += "?wait=true"
2205+
}
2206+
2207+
endpoint := uri
2208+
2209+
// TODO: Remove this when compatibility is not required.
2210+
var files []*File
2211+
if data.File != nil {
2212+
files = []*File{data.File}
2213+
}
2214+
2215+
var response []byte
2216+
if len(files) > 0 {
2217+
body := &bytes.Buffer{}
2218+
bodywriter := multipart.NewWriter(body)
2219+
2220+
var payload []byte
2221+
payload, err = json.Marshal(data)
2222+
if err != nil {
2223+
return
2224+
}
2225+
2226+
var p io.Writer
2227+
2228+
h := make(textproto.MIMEHeader)
2229+
h.Set("Content-Disposition", `form-data; name="payload_json"`)
2230+
h.Set("Content-Type", "application/json")
2231+
2232+
p, err = bodywriter.CreatePart(h)
2233+
if err != nil {
2234+
return
2235+
}
2236+
2237+
if _, err = p.Write(payload); err != nil {
2238+
return
2239+
}
2240+
2241+
for i, file := range files {
2242+
h := make(textproto.MIMEHeader)
2243+
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file%d"; filename="%s"`, i, quoteEscaper.Replace(file.Name)))
2244+
contentType := file.ContentType
2245+
if contentType == "" {
2246+
contentType = "application/octet-stream"
2247+
}
2248+
h.Set("Content-Type", contentType)
2249+
2250+
p, err = bodywriter.CreatePart(h)
2251+
if err != nil {
2252+
return
2253+
}
2254+
2255+
if _, err = io.Copy(p, file.Reader); err != nil {
2256+
return
2257+
}
2258+
}
2259+
2260+
err = bodywriter.Close()
2261+
if err != nil {
2262+
return
2263+
}
2264+
2265+
response, err = s.request("POST", endpoint, bodywriter.FormDataContentType(), body.Bytes(), EndpointWebhookToken(0, ""))
2266+
} else {
2267+
response, err = s.RequestWithBucketID("POST", endpoint, data, EndpointWebhookToken(0, ""))
2268+
}
2269+
2270+
if err != nil {
2271+
return
2272+
}
2273+
2274+
if wait {
2275+
err = unmarshal(response, &m)
2276+
}
2277+
2278+
return
2279+
2280+
// _, err = s.RequestWithBucketID("POST", uri, data, EndpointWebhookToken(0, ""))
2281+
// return
2282+
}
2283+
21972284
// MessageReactionAdd creates an emoji reaction to a message.
21982285
// channelID : The channel ID.
21992286
// messageID : The message ID.
@@ -2612,13 +2699,16 @@ func (s *Session) DeleteInteractionResponse(applicationID int64, token string) (
26122699
// CreateFollowupMessage Creates a followup message for an Interaction. Functions the same as Execute Webhook, but wait is always true, and flags can be set to 64 in the body to send an ephemeral message.
26132700
// POST /webhooks/{application.id}/{interaction.token}
26142701
func (s *Session) CreateFollowupMessage(applicationID int64, token string, data *WebhookParams) (st *Message, err error) {
2615-
body, err := s.RequestWithBucketID("POST", EndpointWebhookToken(applicationID, token), data, EndpointWebhookToken(0, ""))
2616-
if err != nil {
2617-
return
2618-
}
2702+
body, err := s.WebhookExecuteComplex(applicationID, token, true, data)
2703+
return body, err
26192704

2620-
err = unmarshal(body, &st)
2621-
return
2705+
// body, err := s.RequestWithBucketID("POST", EndpointWebhookToken(applicationID, token), data, EndpointWebhookToken(0, ""))
2706+
// if err != nil {
2707+
// return
2708+
// }
2709+
2710+
// err = unmarshal(body, &st)
2711+
// return
26222712
}
26232713

26242714
// EditFollowupMessage Edits a followup message for an Interaction. Functions the same as Edit Webhook Message.

structs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ type WebhookParams struct {
971971
Username string `json:"username,omitempty"`
972972
AvatarURL string `json:"avatar_url,omitempty"`
973973
TTS bool `json:"tts,omitempty"`
974-
File string `json:"file,omitempty"`
974+
File *File `json:"-,omitempty"`
975975
Embeds []*MessageEmbed `json:"embeds,omitempty"`
976976
Flags int64 `json:"flags,omitempty"`
977977
AllowedMentions *AllowedMentions `json:"allowed_mentions,omitempty"`

0 commit comments

Comments
 (0)