diff --git a/state/state.go b/state/state.go index 3e77d8c..12e3b26 100644 --- a/state/state.go +++ b/state/state.go @@ -110,6 +110,8 @@ func NewWithStore(token string, store Store) (*State, error) { return NewFromSession(s, store) } +// NewFromSession never returns an error. This API is kept for backwards +// compatibility. func NewFromSession(s *session.Session, store Store) (*State, error) { state := &State{ Session: s, @@ -121,8 +123,8 @@ func NewFromSession(s *session.Session, store Store) (*State, error) { unavailableGuilds: moreatomic.NewSnowflakeSet(), unreadyGuilds: moreatomic.NewSnowflakeSet(), } - - return state, state.hookSession() + state.hookSession() + return state, nil } // WithContext returns a shallow copy of State with the context replaced in the diff --git a/state/state_events.go b/state/state_events.go index 689aec9..5f4100f 100644 --- a/state/state_events.go +++ b/state/state_events.go @@ -17,30 +17,32 @@ func (s *State) hookSession() { // Run the state handler. s.onEvent(event) - // Always call the event on function exit. Extra events constructed by - // handlers will be called before the main event, but that's fine. - defer s.Handler.Call(event) - - switch e := event.(type) { + switch event := event.(type) { case *gateway.ReadyEvent: - s.handleReady(e) - return + s.Handler.Call(event) + s.handleReady(event) case *gateway.GuildCreateEvent: - s.handleGuildCreate(e) - return + s.Handler.Call(event) + s.handleGuildCreate(event) case *gateway.GuildDeleteEvent: - s.handleGuildDelete(e) - return + s.Handler.Call(event) + s.handleGuildDelete(event) // https://github.com/discord/discord-api-docs/commit/01665c4 case *gateway.MessageCreateEvent: - if e.Member != nil { - e.Member.User = e.Author + if event.Member != nil { + event.Member.User = event.Author } + s.Handler.Call(event) + case *gateway.MessageUpdateEvent: - if e.Member != nil { - e.Member.User = e.Author + if event.Member != nil { + event.Member.User = event.Author } + s.Handler.Call(event) + + default: + s.Handler.Call(event) } }) }