util.go 2.4 KB

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