discordmuffin/util.go
Sentinel cb59c78e32
Add size parameters to IconURL and BannerURL (#1301)
* Add missing parameters/structs

Adds guild icon size parameter, guild banner size parameter, missing ClientStatus struct

* Add missing endpoint for animated banners

* feat: revert ClientStatus changes

Revert addition of ClientStatus since another PR was already merged with
it.

* feat: add documentation for size parameter

* refactor: move icon url logic into separate func

Move logic for icon URLs in various functions into a single iconURL
function. Similar to banner and avatar functions.

Co-authored-by: Fedor Lapshin <fe.lap.prog@gmail.com>
2023-01-18 01:59:00 +03:00

125 lines
2.7 KiB
Go

package discordgo
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/textproto"
"strconv"
"strings"
"time"
)
// SnowflakeTimestamp returns the creation time of a Snowflake ID relative to the creation of Discord.
func SnowflakeTimestamp(ID string) (t time.Time, err error) {
i, err := strconv.ParseInt(ID, 10, 64)
if err != nil {
return
}
timestamp := (i >> 22) + 1420070400000
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 := 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="files[%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
}
func iconURL(iconHash, staticIconURL, animatedIconURL, size string) string {
var URL string
if iconHash == "" {
return ""
} else if strings.HasPrefix(iconHash, "a_") {
URL = animatedIconURL
} else {
URL = staticIconURL
}
if size != "" {
return URL + "?size=" + size
}
return URL
}