From f77e0bd08fe8721110691f59af29939d5c00c846 Mon Sep 17 00:00:00 2001 From: MrMelon Date: Mon, 16 May 2022 12:57:46 +0100 Subject: [PATCH] Add support for banner image --- module/discord/main.go | 72 ++++++++------ module/discord/pages/index.go.html | 147 +++++++++++++++-------------- 2 files changed, 118 insertions(+), 101 deletions(-) diff --git a/module/discord/main.go b/module/discord/main.go index 9d2c372..e9bde01 100644 --- a/module/discord/main.go +++ b/module/discord/main.go @@ -12,8 +12,7 @@ import ( "github.com/gorilla/mux" "golang.org/x/oauth2" "html/template" - "image" - "image/png" + "io" "net/http" "os" ) @@ -61,7 +60,11 @@ func (m *Module) SetupModule(router *mux.Router, f func(cb func(http.ResponseWri } router.HandleFunc("/", m.getClient(m.homepage)) router.HandleFunc("/login", m.sessionWrapper(m.loginPage)) + router.HandleFunc("/user/avatar/{userId}/a_{avatarId}", m.getClient(m.userAvatarAnim)) router.HandleFunc("/user/avatar/{userId}/{avatarId}", m.getClient(m.userAvatar)) + router.HandleFunc("/user/banner/{userId}/a_{bannerId}", m.getClient(m.userAvatarAnim)) + router.HandleFunc("/user/banner/{userId}/{bannerId}", m.getClient(m.userAvatar)) + router.HandleFunc("/guild/icon/{guildId}/a_{iconId}", m.getClient(m.guildIconAnim)) router.HandleFunc("/guild/icon/{guildId}/{iconId}", m.getClient(m.guildIcon)) router.PathPrefix("/assets/icon/{name}.svg").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) @@ -86,7 +89,7 @@ func (m *Module) getClient(cb func(http.ResponseWriter, *http.Request, *utils.St }) } -func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *utils.State, discordClient *discordgo.Session) { +func (m *Module) homepage(rw http.ResponseWriter, _ *http.Request, state *utils.State, discordClient *discordgo.Session) { myUser, err := discordClient.User("@me") if err != nil { state.Del(KeyOauthClient) @@ -122,6 +125,7 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util User *discordgo.User UserAccent string Avatar template.HTMLAttr + Banner template.HTMLAttr Guilds []*discordgo.UserGuild GuildIcons []template.HTMLAttr Connections []*discordgo.UserConnection @@ -129,6 +133,7 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util User: myUser, UserAccent: fmt.Sprintf("#%06x", myUser.AccentColor), Avatar: template.HTMLAttr(fmt.Sprintf("src=\"/discord/user/avatar/%s/%s\"", myUser.ID, myUser.Avatar)), + Banner: template.HTMLAttr(fmt.Sprintf("src=\"/discord/user/banner/%s/%s\"", myUser.ID, myUser.Banner)), Guilds: myGuilds, GuildIcons: guildIcons, Connections: myConns, @@ -178,40 +183,51 @@ func (m *Module) loginPage(rw http.ResponseWriter, req *http.Request, state *uti http.Redirect(rw, req, m.oauthClient.AuthCodeURL(flowState.String(), oauth2.AccessTypeOffline), http.StatusTemporaryRedirect) } -func (m *Module) userAvatar(rw http.ResponseWriter, req *http.Request, state *utils.State, discordClient *discordgo.Session) { - vars := mux.Vars(req) - body, err := discordClient.RequestWithBucketID("GET", discordgo.EndpointUserAvatar(vars["userId"], vars["avatarId"]), nil, discordgo.EndpointUserAvatar("", "")) +func (m *Module) bucketFetchImage(rw http.ResponseWriter, discordClient *discordgo.Session, endpoint, emptyEndpoint, contentType string) { + body, err := discordClient.RequestWithBucketID("GET", endpoint, nil, emptyEndpoint) if err != nil { return } - rw.Header().Set("Content-Type", "image/png") - rw.WriteHeader(200) - - myAvatar, _, err := image.Decode(bytes.NewReader(body)) - err = png.Encode(rw, myAvatar) - if err != nil { - http.Error(rw, err.Error(), http.StatusInternalServerError) - return - } + rw.Header().Set("Content-Type", contentType) + rw.WriteHeader(http.StatusOK) + _, _ = io.Copy(rw, bytes.NewReader(body)) } -func (m *Module) guildIcon(rw http.ResponseWriter, req *http.Request, state *utils.State, discordClient *discordgo.Session) { +func (m *Module) userAvatar(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) { vars := mux.Vars(req) - body, err := discordClient.RequestWithBucketID("GET", discordgo.EndpointGuildIcon(vars["guildId"], vars["iconId"]), nil, discordgo.EndpointGuildIcon(vars["guildId"], "")) - if err != nil { - return - } + endpoint := discordgo.EndpointUserAvatar + m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], vars["avatarId"]), endpoint("", ""), "image/png") +} - rw.Header().Set("Content-Type", "image/png") - rw.WriteHeader(http.StatusOK) +func (m *Module) userAvatarAnim(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) { + vars := mux.Vars(req) + endpoint := discordgo.EndpointUserAvatarAnimated + m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], "a_"+vars["avatarId"]), endpoint("", ""), "image/gif") +} - myAvatar, _, err := image.Decode(bytes.NewReader(body)) - err = png.Encode(rw, myAvatar) - if err != nil { - http.Error(rw, err.Error(), http.StatusInternalServerError) - return - } +func (m *Module) userBanner(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) { + vars := mux.Vars(req) + endpoint := discordgo.EndpointUserBanner + m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], vars["bannerId"]), endpoint("", ""), "image/png") +} + +func (m *Module) userBannerAnim(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) { + vars := mux.Vars(req) + endpoint := discordgo.EndpointUserBannerAnimated + m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], "a_"+vars["bannerId"]), endpoint("", ""), "image/gif") +} + +func (m *Module) guildIcon(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) { + vars := mux.Vars(req) + endpoint := discordgo.EndpointGuildIcon + m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], vars["guildId"]), endpoint("", ""), "image/png") +} + +func (m *Module) guildIconAnim(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) { + vars := mux.Vars(req) + endpoint := discordgo.EndpointGuildIconAnimated + m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], "a_"+vars["guildId"]), endpoint("", ""), "image/gif") } func connectedLinkFunc(a *discordgo.UserConnection) string { diff --git a/module/discord/pages/index.go.html b/module/discord/pages/index.go.html index 8eb21b8..e34df93 100644 --- a/module/discord/pages/index.go.html +++ b/module/discord/pages/index.go.html @@ -43,84 +43,85 @@
{{with .User}} -

My User: {{.Username}}#{{.Discriminator}}

-
    -
  • ID: {{.ID}}
  • -
  • Email: {{.Email}}
  • -
  • Username: {{.Username}}
  • -
  • Avatar:
  • -
  • Locale: {{.Locale}}
  • -
  • Discriminator: {{.Discriminator}}
  • -
  • Verified: {{.Verified}}
  • -
  • MFA: {{.MFAEnabled}}
  • -
  • Banner: {{.Banner}}
  • -
  • Accent Color: {{$.UserAccent}}   -
  • -
  • Bot: {{.Bot}}
  • -
  • Premium: {{.PremiumType}}
  • -
  • System: {{.System}}
  • -
+

My User: {{.Username}}#{{.Discriminator}}

+
    +
  • ID: {{.ID}}
  • +
  • Email: {{.Email}}
  • +
  • Username: {{.Username}}
  • +
  • Avatar: User Avatar
  • +
  • Locale: {{.Locale}}
  • +
  • Discriminator: {{.Discriminator}}
  • +
  • Verified: {{.Verified}}
  • +
  • MFA: {{.MFAEnabled}}
  • +
  • Banner: User Banner
  • +
  • Accent Color: {{$.UserAccent}}   +
  • +
  • Bot: {{.Bot}}
  • +
  • Premium: {{.PremiumType}}
  • +
  • System: {{.System}}
  • +
{{end}}
{{with .User.PublicFlags}} -

My Flags:

-
    - {{if checkFlag . 1}} -
  • Discord Employee
  • - {{end}} - {{if checkFlag . 2}} -
  • Discord Partner
  • - {{end}} - {{if checkFlag . 4}} -
  • Hype Squad Events
  • - {{end}} - {{if checkFlag . 8}} -
  • Bug Hunter Level 1
  • - {{end}} - {{if checkFlag . 64}} -
  • House Bravery
  • - {{end}} - {{if checkFlag . 128}} -
  • House Brilliance
  • - {{end}} - {{if checkFlag . 256}} -
  • House Balance
  • - {{end}} - {{if checkFlag . 512}} -
  • Early Supporter
  • - {{end}} - {{if checkFlag . 1024}} -
  • Team User
  • - {{end}} - {{if checkFlag . 4096}} -
  • System
  • - {{end}} - {{if checkFlag . 16384}} -
  • Bug Hunter Level 2
  • - {{end}} - {{if checkFlag . 65536}} -
  • Verified Bot
  • - {{end}} - {{if checkFlag . 131072}} -
  • Verified Bot Developer
  • - {{end}} - {{if checkFlag . 262144}} -
  • Discord Certified Moderator
  • - {{end}} -
+

My Flags:

+
    + {{if checkFlag . 1}} +
  • Discord Employee
  • + {{end}} + {{if checkFlag . 2}} +
  • Discord Partner
  • + {{end}} + {{if checkFlag . 4}} +
  • Hype Squad Events
  • + {{end}} + {{if checkFlag . 8}} +
  • Bug Hunter Level 1
  • + {{end}} + {{if checkFlag . 64}} +
  • House Bravery
  • + {{end}} + {{if checkFlag . 128}} +
  • House Brilliance
  • + {{end}} + {{if checkFlag . 256}} +
  • House Balance
  • + {{end}} + {{if checkFlag . 512}} +
  • Early Supporter
  • + {{end}} + {{if checkFlag . 1024}} +
  • Team User
  • + {{end}} + {{if checkFlag . 4096}} +
  • System
  • + {{end}} + {{if checkFlag . 16384}} +
  • Bug Hunter Level 2
  • + {{end}} + {{if checkFlag . 65536}} +
  • Verified Bot
  • + {{end}} + {{if checkFlag . 131072}} +
  • Verified Bot Developer
  • + {{end}} + {{if checkFlag . 262144}} +
  • Discord Certified Moderator
  • + {{end}} +
{{end}}

Connections ({{len .Connections}}):

@@ -128,11 +129,11 @@

Guilds ({{len .Guilds}}):

{{range $k, $v := .Guilds}} -
-
-
{{$v.Name}}
-
@*{{$v.ID}}
-
+
+
+
{{$v.Name}}
+
@*{{$v.ID}}
+
{{end}}