From 4390b565fa2d8e3971b14d29c806f34e1fbda1da Mon Sep 17 00:00:00 2001 From: nitroflap Date: Sun, 27 Feb 2022 17:46:03 +0300 Subject: [PATCH] feat(structs#Session): made sessionID and sequence fields public --- discord.go | 2 +- event.go | 2 +- structs.go | 8 ++++---- wsapi.go | 12 ++++++------ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/discord.go b/discord.go index a453a4d..aa9efd6 100644 --- a/discord.go +++ b/discord.go @@ -41,7 +41,7 @@ func New(token string) (s *Session, err error) { MaxRestRetries: 3, Client: &http.Client{Timeout: (20 * time.Second)}, UserAgent: "DiscordBot (https://github.com/bwmarrin/discordgo, v" + VERSION + ")", - sequence: new(int64), + Sequence: new(int64), LastHeartbeatAck: time.Now().UTC(), } diff --git a/event.go b/event.go index 84dbdc7..308a779 100644 --- a/event.go +++ b/event.go @@ -243,5 +243,5 @@ func (s *Session) onInterface(i interface{}) { func (s *Session) onReady(r *Ready) { // Store the SessionID within the Session struct. - s.sessionID = r.SessionID + s.SessionID = r.SessionID } diff --git a/structs.go b/structs.go index 02adaa1..9399bf6 100644 --- a/structs.go +++ b/structs.go @@ -115,14 +115,14 @@ type Session struct { // When nil, the session is not listening. listening chan interface{} - // sequence tracks the current gateway api websocket sequence number - sequence *int64 + // Sequence tracks the current gateway api websocket sequence number + Sequence *int64 // stores sessions current Discord Gateway gateway string - // stores session ID of current Gateway connection - sessionID string + // SessionID stores session ID of current Gateway connection + SessionID string // used to make sure gateway websocket writes do not happen concurrently wsMutex sync.Mutex diff --git a/wsapi.go b/wsapi.go index f2c228d..eb12794 100644 --- a/wsapi.go +++ b/wsapi.go @@ -123,8 +123,8 @@ func (s *Session) Open() error { // Now we send either an Op 2 Identity if this is a brand new // connection or Op 6 Resume if we are resuming an existing connection. - sequence := atomic.LoadInt64(s.sequence) - if s.sessionID == "" && sequence == 0 { + sequence := atomic.LoadInt64(s.Sequence) + if s.SessionID == "" && sequence == 0 { // Send Op 2 Identity Packet err = s.identify() @@ -139,7 +139,7 @@ func (s *Session) Open() error { p := resumePacket{} p.Op = 6 p.Data.Token = s.Token - p.Data.SessionID = s.sessionID + p.Data.SessionID = s.SessionID p.Data.Sequence = sequence s.log(LogInformational, "sending resume packet to gateway") @@ -291,7 +291,7 @@ func (s *Session) heartbeat(wsConn *websocket.Conn, listening <-chan interface{} s.RLock() last := s.LastHeartbeatAck s.RUnlock() - sequence := atomic.LoadInt64(s.sequence) + sequence := atomic.LoadInt64(s.Sequence) s.log(LogDebug, "sending gateway websocket heartbeat seq %d", sequence) s.wsMutex.Lock() s.LastHeartbeatSent = time.Now().UTC() @@ -518,7 +518,7 @@ func (s *Session) onEvent(messageType int, message []byte) (*Event, error) { if e.Operation == 1 { s.log(LogInformational, "sending heartbeat in response to Op1") s.wsMutex.Lock() - err = s.wsConn.WriteJSON(heartbeatOp{1, atomic.LoadInt64(s.sequence)}) + err = s.wsConn.WriteJSON(heartbeatOp{1, atomic.LoadInt64(s.Sequence)}) s.wsMutex.Unlock() if err != nil { s.log(LogError, "error sending heartbeat in response to Op1") @@ -574,7 +574,7 @@ func (s *Session) onEvent(messageType int, message []byte) (*Event, error) { } // Store the message sequence - atomic.StoreInt64(s.sequence, e.Sequence) + atomic.StoreInt64(s.Sequence, e.Sequence) // Map event to registered event handlers and pass it along to any registered handlers. if eh, ok := registeredInterfaceProviders[e.Type]; ok {