From 8737777ce72bb218154b5d5c9619bc14c52c1602 Mon Sep 17 00:00:00 2001 From: Cory Ory Date: Sun, 22 Oct 2017 22:08:08 +0100 Subject: [PATCH] Implement Raw sending of status (Rich Presence and Online Status) (#462) * Added ability to change the online status * Add structs for rick presence implementation * Refractor and publicise UpdateStatusData * Add UpdateStatusComplex for raw status data sending * Case gameType to int, stopped it compiling * Might want to gofmt. Doesn't do it on save because Gogland removed it and their new thing I can't make sense of. * Revert "Added ability to change the online status" This reverts commit 235cd15a8eebbec070cb95a5853295387bceae1c. * Change gametypeto match * Move RLock to UpdateStatusComplex --- structs.go | 27 ++++++++++++++++++++++++--- wsapi.go | 24 +++++++++++++++--------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/structs.go b/structs.go index c5dbdae..83e5089 100644 --- a/structs.go +++ b/structs.go @@ -323,9 +323,30 @@ const ( // A Game struct holds the name of the "playing .." game for a user type Game struct { - Name string `json:"name"` - Type GameType `json:"type"` - URL string `json:"url,omitempty"` + Name string `json:"name"` + Type GameType `json:"type"` + URL string `json:"url,omitempty"` + Details string `json:"details,omitempty"` + State string `json:"state,omitempty"` + TimeStamps TimeStamps `json:"timestamps,omitempty"` + Assets Assets `json:"assets,omitempty"` + ApplicationID string `json:"application_id,omitempty"` + Instance int8 `json:"instance,omitempty"` + // TODO: Party and Secrets (unknown structure) +} + +// A TimeStamps struct contains start and end times used in the rich presence "playing .." Game +type TimeStamps struct { + EndTimestamp uint `json:"end,omitempty"` + StartTimestamp uint `json:"start,omitempty"` +} + +// An Assets struct contains assets and labels used in the rich presence "playing .." Game +type Assets struct { + LargeImageID string `json:"large_image,omitempty"` + SmallImageID string `json:"small_image,omitempty"` + LargeText string `json:"large_text,omitempty"` + SmallText string `json:"small_text,omitempty"` } // A Member stores user information for Guild members. diff --git a/wsapi.go b/wsapi.go index 59dc532..94281b0 100644 --- a/wsapi.go +++ b/wsapi.go @@ -249,7 +249,7 @@ func (s *Session) heartbeat(wsConn *websocket.Conn, listening <-chan interface{} } } -type updateStatusData struct { +type UpdateStatusData struct { IdleSince *int `json:"since"` Game *Game `json:"game"` AFK bool `json:"afk"` @@ -258,7 +258,7 @@ type updateStatusData struct { type updateStatusOp struct { Op int `json:"op"` - Data updateStatusData `json:"d"` + Data UpdateStatusData `json:"d"` } // UpdateStreamingStatus is used to update the user's streaming status. @@ -270,13 +270,7 @@ func (s *Session) UpdateStreamingStatus(idle int, game string, url string) (err s.log(LogInformational, "called") - s.RLock() - defer s.RUnlock() - if s.wsConn == nil { - return ErrWSNotFound - } - - usd := updateStatusData{ + usd := UpdateStatusData{ Status: "online", } @@ -296,6 +290,18 @@ func (s *Session) UpdateStreamingStatus(idle int, game string, url string) (err } } + return s.UpdateStatusComplex(usd) +} + +// UpdateStatusComplex allows for sending the raw status update data untouched by discordgo. +func (s *Session) UpdateStatusComplex(usd UpdateStatusData) (err error) { + + s.RLock() + defer s.RUnlock() + if s.wsConn == nil { + return ErrWSNotFound + } + s.wsMutex.Lock() err = s.wsConn.WriteJSON(updateStatusOp{3, usd}) s.wsMutex.Unlock()