emoji.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "github.com/bwmarrin/discordgo"
  6. "maunium.net/go/mautrix/appservice"
  7. "maunium.net/go/mautrix/id"
  8. )
  9. func (portal *Portal) downloadDiscordEmoji(id string, animated bool) ([]byte, string, error) {
  10. var url string
  11. var mimeType string
  12. if animated {
  13. // This url requests a gif, so that's what we set the mimetype to.
  14. url = discordgo.EndpointEmojiAnimated(id)
  15. mimeType = "image/gif"
  16. } else {
  17. // This url requests a png, so that's what we set the mimetype to.
  18. url = discordgo.EndpointEmoji(id)
  19. mimeType = "image/png"
  20. }
  21. req, err := http.NewRequest(http.MethodGet, url, nil)
  22. if err != nil {
  23. return nil, mimeType, err
  24. }
  25. req.Header.Set("User-Agent", discordgo.DroidBrowserUserAgent)
  26. resp, err := http.DefaultClient.Do(req)
  27. if err != nil {
  28. return nil, mimeType, err
  29. }
  30. defer resp.Body.Close()
  31. data, err := ioutil.ReadAll(resp.Body)
  32. return data, mimeType, err
  33. }
  34. func (portal *Portal) uploadMatrixEmoji(intent *appservice.IntentAPI, data []byte, mimeType string) (id.ContentURI, error) {
  35. uploaded, err := intent.UploadBytes(data, mimeType)
  36. if err != nil {
  37. return id.ContentURI{}, err
  38. }
  39. return uploaded.ContentURI, nil
  40. }