diff --git a/event.go b/event.go index 84dbdc7fb..e9af6a455 100644 --- a/event.go +++ b/event.go @@ -51,7 +51,6 @@ func registerInterfaceProvider(eh EventInterfaceProvider) { // fmt.Errorf("event %s already registered", eh.Type()) } registeredInterfaceProviders[eh.Type()] = eh - return } // eventHandlerInstance is a wrapper around an event handler, as functions @@ -102,12 +101,14 @@ func (s *Session) addEventHandlerOnce(eventHandler EventHandler) func() { // to a struct corresponding to the event for which you want to listen. // // eg: -// Session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) { -// }) +// +// Session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) { +// }) // // or: -// Session.AddHandler(func(s *discordgo.Session, m *discordgo.PresenceUpdate) { -// }) +// +// Session.AddHandler(func(s *discordgo.Session, m *discordgo.PresenceUpdate) { +// }) // // List of events can be found at this page, with corresponding names in the // library for each event: https://discord.com/developers/docs/topics/gateway#event-names diff --git a/oauth2_test.go b/oauth2_test.go index 1d5451bdf..bb93b24de 100644 --- a/oauth2_test.go +++ b/oauth2_test.go @@ -52,6 +52,4 @@ func ExampleApplication() { // Delete the application we created. err = dg.ApplicationDelete(ap.ID) log.Printf("Delete: err: %+v\n", err) - - return } diff --git a/ratelimit.go b/ratelimit.go index c992fd45e..554421771 100644 --- a/ratelimit.go +++ b/ratelimit.go @@ -74,7 +74,7 @@ func (r *RateLimiter) GetWaitTime(b *Bucket, minRemaining int) time.Duration { // If we ran out of calls and the reset time is still ahead of us // then we need to take it easy and relax a little if b.Remaining < minRemaining && b.reset.After(time.Now()) { - return b.reset.Sub(time.Now()) + return time.Until(b.reset) } // Check for global ratelimits @@ -124,7 +124,7 @@ func (b *Bucket) Release(headers http.Header) error { // Check if the bucket uses a custom ratelimiter if rl := b.customRateLimit; rl != nil { - if time.Now().Sub(b.lastReset) >= rl.reset { + if time.Since(b.lastReset) >= rl.reset { b.Remaining = rl.requests - 1 b.lastReset = time.Now() } diff --git a/restapi.go b/restapi.go index b8ed67992..63d8b4c95 100644 --- a/restapi.go +++ b/restapi.go @@ -297,7 +297,7 @@ func (s *Session) RequestWithLockedBucket(method, urlStr, contentType string, b } case http.StatusUnauthorized: if strings.Index(s.Token, "Bot ") != 0 { - s.log(LogInformational, ErrUnauthorized.Error()) + s.log(LogInformational, "%s", ErrUnauthorized.Error()) err = ErrUnauthorized } fallthrough @@ -1579,7 +1579,7 @@ func (s *Session) GuildTemplateCreate(guildID string, data *GuildTemplateParams, return } - err = unmarshal(body, &st) + unmarshal(body, &st) return } @@ -3600,7 +3600,7 @@ func (s *Session) Entitlements(appID string, filterOptions *EntitlementFilterOpt if filterOptions.UserID != "" { queryParams.Set("user_id", filterOptions.UserID) } - if filterOptions.SkuIDs != nil && len(filterOptions.SkuIDs) > 0 { + if len(filterOptions.SkuIDs) > 0 { queryParams.Set("sku_ids", strings.Join(filterOptions.SkuIDs, ",")) } if filterOptions.Before != nil { diff --git a/restapi_test.go b/restapi_test.go index bb922b9de..22f9be214 100644 --- a/restapi_test.go +++ b/restapi_test.go @@ -95,7 +95,7 @@ func TestUserChannelCreate(t *testing.T) { _, err := dg.UserChannelCreate(envAdmin) if err != nil { - t.Errorf(err.Error()) + t.Errorf("%s", err.Error()) } // TODO make sure the channel was added @@ -108,7 +108,7 @@ func TestUserGuilds(t *testing.T) { _, err := dg.UserGuilds(10, "", "", false) if err != nil { - t.Errorf(err.Error()) + t.Errorf("%s", err.Error()) } } diff --git a/state.go b/state.go index bac4bf3f3..c706f3793 100644 --- a/state.go +++ b/state.go @@ -175,8 +175,9 @@ func (s *State) GuildRemove(guild *Guild) error { // Guild gets a guild by ID. // Useful for querying if @me is in a guild: -// _, err := discordgo.Session.State.Guild(guildID) -// isInGuild := err == nil +// +// _, err := discordgo.Session.State.Guild(guildID) +// isInGuild := err == nil func (s *State) Guild(guildID string) (*Guild, error) { if s == nil { return nil, ErrNilState @@ -1061,7 +1062,7 @@ func (s *State) OnInterface(se *Session, i interface{}) (err error) { oldCopy := *old t.BeforeUpdate = &oldCopy } - err = s.ChannelAdd(t.Channel) + s.ChannelAdd(t.Channel) } case *ChannelDelete: if s.TrackChannels { @@ -1078,7 +1079,7 @@ func (s *State) OnInterface(se *Session, i interface{}) (err error) { oldCopy := *old t.BeforeUpdate = &oldCopy } - err = s.ChannelAdd(t.Channel) + s.ChannelAdd(t.Channel) } case *ThreadDelete: if s.TrackThreads { diff --git a/voice.go b/voice.go index e9c89bee1..d758ab338 100644 --- a/voice.go +++ b/voice.go @@ -501,8 +501,6 @@ func (v *VoiceConnection) onEvent(message []byte) { default: v.log(LogDebug, "unknown voice operation, %d, %s", e.Operation, string(e.RawData)) } - - return } type voiceHeartbeatOp struct { @@ -787,7 +785,8 @@ func (v *VoiceConnection) opusSender(udpConn *net.UDPConn, close <-chan struct{} sequence++ } - if (timestamp + uint32(size)) >= 0xFFFFFFFF { + // Check for overflow and reset timestamp if needed + if (timestamp + uint32(size)) < timestamp { timestamp = 0 } else { timestamp += uint32(size) @@ -920,7 +919,7 @@ func (v *VoiceConnection) reconnect() { wait = 600 } - if v.session.DataReady == false || v.session.wsConn == nil { + if !v.session.DataReady || v.session.wsConn == nil { v.log(LogInformational, "cannot reconnect to channel %s with unready session", v.ChannelID) continue } diff --git a/wsapi.go b/wsapi.go index d101c542a..0b30dae32 100644 --- a/wsapi.go +++ b/wsapi.go @@ -275,7 +275,7 @@ func (s *Session) HeartbeatLatency() time.Duration { // heartbeat sends regular heartbeats to Discord so it knows the client // is still connected. If you do not send these heartbeats Discord will // disconnect the websocket connection after a few seconds. -func (s *Session) heartbeat(wsConn *websocket.Conn, listening <-chan interface{}, heartbeatIntervalMsec time.Duration) { +func (s *Session) heartbeat(wsConn *websocket.Conn, listening <-chan interface{}, heartbeatInterval time.Duration) { s.log(LogInformational, "called") @@ -284,7 +284,7 @@ func (s *Session) heartbeat(wsConn *websocket.Conn, listening <-chan interface{} } var err error - ticker := time.NewTicker(heartbeatIntervalMsec * time.Millisecond) + ticker := time.NewTicker(heartbeatInterval * time.Millisecond) defer ticker.Stop() for { @@ -297,7 +297,7 @@ func (s *Session) heartbeat(wsConn *websocket.Conn, listening <-chan interface{} s.LastHeartbeatSent = time.Now().UTC() err = wsConn.WriteJSON(heartbeatOp{1, sequence}) s.wsMutex.Unlock() - if err != nil || time.Now().UTC().Sub(last) > (heartbeatIntervalMsec*FailedHeartbeatAcks) { + if err != nil || time.Now().UTC().Sub(last) > (heartbeatInterval*FailedHeartbeatAcks) { if err != nil { s.log(LogError, "error sending heartbeat to gateway %s, %s", s.gateway, err) } else { @@ -697,16 +697,16 @@ type voiceChannelJoinOp struct { // ChannelVoiceJoin joins the session user to a voice channel. // -// gID : Guild ID of the channel to join. -// cID : Channel ID of the channel to join. -// mute : If true, you will be set to muted upon joining. -// deaf : If true, you will be set to deafened upon joining. +// gID : Guild ID of the channel to join. +// cID : Channel ID of the channel to join. +// mute : If true, you will be set to muted upon joining. +// deaf : If true, you will be set to deafened upon joining. func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (voice *VoiceConnection, err error) { s.log(LogInformational, "called") s.RLock() - voice, _ = s.VoiceConnections[gID] + voice = s.VoiceConnections[gID] s.RUnlock() if voice == nil { @@ -744,10 +744,10 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (voice *Voi // // This should only be used when the VoiceServerUpdate will be intercepted and used elsewhere. // -// gID : Guild ID of the channel to join. -// cID : Channel ID of the channel to join, leave empty to disconnect. -// mute : If true, you will be set to muted upon joining. -// deaf : If true, you will be set to deafened upon joining. +// gID : Guild ID of the channel to join. +// cID : Channel ID of the channel to join, leave empty to disconnect. +// mute : If true, you will be set to muted upon joining. +// deaf : If true, you will be set to deafened upon joining. func (s *Session) ChannelVoiceJoinManual(gID, cID string, mute, deaf bool) (err error) { s.log(LogInformational, "called") @@ -843,7 +843,7 @@ func (s *Session) identify() error { // TODO: This is a temporary block of code to help // maintain backwards compatibility - if s.Compress == false { + if !s.Compress { s.Identify.Compress = false }