diff --git a/discord.go b/discord.go index cf276e7..18f7ffa 100644 --- a/discord.go +++ b/discord.go @@ -136,7 +136,7 @@ func (s *Session) AddHandler(handler interface{}) { panic("Unable to add event handler, first argument must be of type *discordgo.Session.") } - if s.Handlers == nil { + if s.handlers == nil { s.Unlock() s.initialize() s.Lock() @@ -149,13 +149,13 @@ func (s *Session) AddHandler(handler interface{}) { eventType = nil } - handlers := s.Handlers[eventType] + handlers := s.handlers[eventType] if handlers == nil { handlers = []reflect.Value{} } handlers = append(handlers, reflect.ValueOf(handler)) - s.Handlers[eventType] = handlers + s.handlers[eventType] = handlers } func (s *Session) handle(event interface{}) { @@ -164,13 +164,13 @@ func (s *Session) handle(event interface{}) { handlerParameters := []reflect.Value{reflect.ValueOf(s), reflect.ValueOf(event)} - if handlers, ok := s.Handlers[reflect.TypeOf(event)]; ok { + if handlers, ok := s.handlers[reflect.TypeOf(event)]; ok { for _, handler := range handlers { handler.Call(handlerParameters) } } - if handlers, ok := s.Handlers[nil]; ok { + if handlers, ok := s.handlers[nil]; ok { for _, handler := range handlers { handler.Call(handlerParameters) } @@ -180,7 +180,7 @@ func (s *Session) handle(event interface{}) { // initialize adds all internal handlers and state tracking handlers. func (s *Session) initialize() { s.Lock() - s.Handlers = map[interface{}][]reflect.Value{} + s.handlers = map[interface{}][]reflect.Value{} s.Unlock() s.AddHandler(s.onEvent) diff --git a/structs.go b/structs.go index 8185902..0d10c1c 100644 --- a/structs.go +++ b/structs.go @@ -20,47 +20,59 @@ import ( "github.com/gorilla/websocket" ) -// A Session represents a connection to the Discord REST API. -// token : The authentication token returned from Discord -// Debug : If set to ture debug logging will be displayed. +// A Session represents a connection to the Discord API. type Session struct { sync.RWMutex // General configurable settings. - Token string // Authentication token for this session - Debug bool // Debug for printing JSON request/responses - // This is a mapping of event structs to a reflected value - // for event handlers. - // We store the reflected value instead of the function - // reference as it is more performant, instead of re-reflecting - // the function each event. - Handlers map[interface{}][]reflect.Value + // Authentication token for this session + Token string - // Exposed but should not be modified by User. - SessionID string // from websocket READY packet - DataReady bool // Set to true when Data Websocket is ready - VoiceReady bool // Set to true when Voice Websocket is ready - UDPReady bool // Set to true when UDP Connection is ready - - // The websocket connection. - wsConn *websocket.Conn - - // Stores all details related to voice connections - Voice *Voice - - // Managed state object, updated with events. - State *State - StateEnabled bool - - // When nil, the session is not listening. - listening chan interface{} + // Debug for printing JSON request/responses + Debug bool // Should the session reconnect the websocket on errors. ShouldReconnectOnError bool // Should the session request compressed websocket data. Compress bool + + // Should state tracking be enabled. + // State tracking is the best way for getting the the users + // active guilds and the members of the guilds. + StateEnabled bool + + // Exposed but should not be modified by User. + + // Whether the Data Websocket is ready + DataReady bool + + // Whether the Voice Websocket is ready + VoiceReady bool + + // Whether the UDP Connection is ready + UDPReady bool + + // Stores all details related to voice connections + Voice *Voice + + // Managed state object, updated internally with events when + // StateEnabled is true. + State *State + + // This is a mapping of event structs to a reflected value + // for event handlers. + // We store the reflected value instead of the function + // reference as it is more performant, instead of re-reflecting + // the function each event. + handlers map[interface{}][]reflect.Value + + // The websocket connection. + wsConn *websocket.Conn + + // When nil, the session is not listening. + listening chan interface{} } // A VoiceRegion stores data for a specific voice region server. diff --git a/wsapi.go b/wsapi.go index 6584072..b75c7e0 100644 --- a/wsapi.go +++ b/wsapi.go @@ -285,7 +285,7 @@ var eventToInterface = map[string]interface{}{ // All unhandled events will then be handled by OnEvent. func (s *Session) event(messageType int, message []byte) { s.RLock() - if s.Handlers == nil { + if s.handlers == nil { s.RUnlock() s.initialize() } else {