github.com/meowstercatel/discordgo@v0.0.0-20250615204139-29b43d676735/util.go (about) 1 package discordgo 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "mime/multipart" 8 "net/textproto" 9 "strconv" 10 "strings" 11 "time" 12 ) 13 14 // SnowflakeTimestamp returns the creation time of a Snowflake ID relative to the creation of Discord. 15 func SnowflakeTimestamp(ID string) (t time.Time, err error) { 16 i, err := strconv.ParseInt(ID, 10, 64) 17 if err != nil { 18 return 19 } 20 timestamp := (i >> 22) + 1420070400000 21 t = time.Unix(0, timestamp*1000000) 22 return 23 } 24 25 // MultipartBodyWithJSON returns the contentType and body for a discord request 26 // data : The object to encode for payload_json in the multipart request 27 // files : Files to include in the request 28 func MultipartBodyWithJSON(data interface{}, files []*File) (requestContentType string, requestBody []byte, err error) { 29 body := &bytes.Buffer{} 30 bodywriter := multipart.NewWriter(body) 31 32 payload, err := Marshal(data) 33 if err != nil { 34 return 35 } 36 37 var p io.Writer 38 39 h := make(textproto.MIMEHeader) 40 h.Set("Content-Disposition", `form-data; name="payload_json"`) 41 h.Set("Content-Type", "application/json") 42 43 p, err = bodywriter.CreatePart(h) 44 if err != nil { 45 return 46 } 47 48 if _, err = p.Write(payload); err != nil { 49 return 50 } 51 52 for i, file := range files { 53 h := make(textproto.MIMEHeader) 54 h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="files[%d]"; filename="%s"`, i, quoteEscaper.Replace(file.Name))) 55 contentType := file.ContentType 56 if contentType == "" { 57 contentType = "application/octet-stream" 58 } 59 h.Set("Content-Type", contentType) 60 61 p, err = bodywriter.CreatePart(h) 62 if err != nil { 63 return 64 } 65 66 if _, err = io.Copy(p, file.Reader); err != nil { 67 return 68 } 69 } 70 71 err = bodywriter.Close() 72 if err != nil { 73 return 74 } 75 76 return bodywriter.FormDataContentType(), body.Bytes(), nil 77 } 78 79 func avatarURL(avatarHash, defaultAvatarURL, staticAvatarURL, animatedAvatarURL, size string) string { 80 var URL string 81 if avatarHash == "" { 82 URL = defaultAvatarURL 83 } else if strings.HasPrefix(avatarHash, "a_") { 84 URL = animatedAvatarURL 85 } else { 86 URL = staticAvatarURL 87 } 88 89 if size != "" { 90 return URL + "?size=" + size 91 } 92 return URL 93 } 94 95 func bannerURL(bannerHash, staticBannerURL, animatedBannerURL, size string) string { 96 var URL string 97 if bannerHash == "" { 98 return "" 99 } else if strings.HasPrefix(bannerHash, "a_") { 100 URL = animatedBannerURL 101 } else { 102 URL = staticBannerURL 103 } 104 105 if size != "" { 106 return URL + "?size=" + size 107 } 108 return URL 109 } 110 111 func iconURL(iconHash, staticIconURL, animatedIconURL, size string) string { 112 var URL string 113 if iconHash == "" { 114 return "" 115 } else if strings.HasPrefix(iconHash, "a_") { 116 URL = animatedIconURL 117 } else { 118 URL = staticIconURL 119 } 120 121 if size != "" { 122 return URL + "?size=" + size 123 } 124 return URL 125 }