From 66bfe530b4fd5cb9755337ce718df26ee81f1b77 Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 17 Mar 2022 09:43:29 -0700 Subject: misc: go get -u ./... ; go mod vendor --- vendor/github.com/bwmarrin/discordgo/util.go | 93 ++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'vendor/github.com/bwmarrin/discordgo/util.go') diff --git a/vendor/github.com/bwmarrin/discordgo/util.go b/vendor/github.com/bwmarrin/discordgo/util.go index 8a2b2e0..6231303 100644 --- a/vendor/github.com/bwmarrin/discordgo/util.go +++ b/vendor/github.com/bwmarrin/discordgo/util.go @@ -1,7 +1,14 @@ package discordgo import ( + "bytes" + "encoding/json" + "fmt" + "io" + "mime/multipart" + "net/textproto" "strconv" + "strings" "time" ) @@ -15,3 +22,89 @@ func SnowflakeTimestamp(ID string) (t time.Time, err error) { t = time.Unix(0, timestamp*1000000) return } + +// MultipartBodyWithJSON returns the contentType and body for a discord request +// data : The object to encode for payload_json in the multipart request +// files : Files to include in the request +func MultipartBodyWithJSON(data interface{}, files []*File) (requestContentType string, requestBody []byte, err error) { + body := &bytes.Buffer{} + bodywriter := multipart.NewWriter(body) + + payload, err := json.Marshal(data) + if err != nil { + return + } + + var p io.Writer + + h := make(textproto.MIMEHeader) + h.Set("Content-Disposition", `form-data; name="payload_json"`) + h.Set("Content-Type", "application/json") + + p, err = bodywriter.CreatePart(h) + if err != nil { + return + } + + if _, err = p.Write(payload); err != nil { + return + } + + for i, file := range files { + h := make(textproto.MIMEHeader) + h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file%d"; filename="%s"`, i, quoteEscaper.Replace(file.Name))) + contentType := file.ContentType + if contentType == "" { + contentType = "application/octet-stream" + } + h.Set("Content-Type", contentType) + + p, err = bodywriter.CreatePart(h) + if err != nil { + return + } + + if _, err = io.Copy(p, file.Reader); err != nil { + return + } + } + + err = bodywriter.Close() + if err != nil { + return + } + + return bodywriter.FormDataContentType(), body.Bytes(), nil +} + +func avatarURL(avatarHash, defaultAvatarURL, staticAvatarURL, animatedAvatarURL, size string) string { + var URL string + if avatarHash == "" { + URL = defaultAvatarURL + } else if strings.HasPrefix(avatarHash, "a_") { + URL = animatedAvatarURL + } else { + URL = staticAvatarURL + } + + if size != "" { + return URL + "?size=" + size + } + return URL +} + +func bannerURL(bannerHash, staticBannerURL, animatedBannerURL, size string) string { + var URL string + if bannerHash == "" { + return "" + } else if strings.HasPrefix(bannerHash, "a_") { + URL = animatedBannerURL + } else { + URL = staticBannerURL + } + + if size != "" { + return URL + "?size=" + size + } + return URL +} -- cgit v1.2.3-54-g00ecf