Un-expose handlers. Clean up Session struct.
This commit is contained in:
parent
88335b6f54
commit
b083ce00c7
3 changed files with 48 additions and 36 deletions
12
discord.go
12
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.")
|
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.Unlock()
|
||||||
s.initialize()
|
s.initialize()
|
||||||
s.Lock()
|
s.Lock()
|
||||||
|
@ -149,13 +149,13 @@ func (s *Session) AddHandler(handler interface{}) {
|
||||||
eventType = nil
|
eventType = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
handlers := s.Handlers[eventType]
|
handlers := s.handlers[eventType]
|
||||||
if handlers == nil {
|
if handlers == nil {
|
||||||
handlers = []reflect.Value{}
|
handlers = []reflect.Value{}
|
||||||
}
|
}
|
||||||
|
|
||||||
handlers = append(handlers, reflect.ValueOf(handler))
|
handlers = append(handlers, reflect.ValueOf(handler))
|
||||||
s.Handlers[eventType] = handlers
|
s.handlers[eventType] = handlers
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) handle(event interface{}) {
|
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)}
|
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 {
|
for _, handler := range handlers {
|
||||||
handler.Call(handlerParameters)
|
handler.Call(handlerParameters)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if handlers, ok := s.Handlers[nil]; ok {
|
if handlers, ok := s.handlers[nil]; ok {
|
||||||
for _, handler := range handlers {
|
for _, handler := range handlers {
|
||||||
handler.Call(handlerParameters)
|
handler.Call(handlerParameters)
|
||||||
}
|
}
|
||||||
|
@ -180,7 +180,7 @@ func (s *Session) handle(event interface{}) {
|
||||||
// initialize adds all internal handlers and state tracking handlers.
|
// initialize adds all internal handlers and state tracking handlers.
|
||||||
func (s *Session) initialize() {
|
func (s *Session) initialize() {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
s.Handlers = map[interface{}][]reflect.Value{}
|
s.handlers = map[interface{}][]reflect.Value{}
|
||||||
s.Unlock()
|
s.Unlock()
|
||||||
|
|
||||||
s.AddHandler(s.onEvent)
|
s.AddHandler(s.onEvent)
|
||||||
|
|
70
structs.go
70
structs.go
|
@ -20,47 +20,59 @@ import (
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Session represents a connection to the Discord REST API.
|
// A Session represents a connection to the Discord API.
|
||||||
// token : The authentication token returned from Discord
|
|
||||||
// Debug : If set to ture debug logging will be displayed.
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
|
|
||||||
// General configurable settings.
|
// 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
|
// Authentication token for this session
|
||||||
// for event handlers.
|
Token string
|
||||||
// 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
|
|
||||||
|
|
||||||
// Exposed but should not be modified by User.
|
// Debug for printing JSON request/responses
|
||||||
SessionID string // from websocket READY packet
|
Debug bool
|
||||||
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{}
|
|
||||||
|
|
||||||
// Should the session reconnect the websocket on errors.
|
// Should the session reconnect the websocket on errors.
|
||||||
ShouldReconnectOnError bool
|
ShouldReconnectOnError bool
|
||||||
|
|
||||||
// Should the session request compressed websocket data.
|
// Should the session request compressed websocket data.
|
||||||
Compress bool
|
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.
|
// A VoiceRegion stores data for a specific voice region server.
|
||||||
|
|
2
wsapi.go
2
wsapi.go
|
@ -285,7 +285,7 @@ var eventToInterface = map[string]interface{}{
|
||||||
// All unhandled events will then be handled by OnEvent.
|
// All unhandled events will then be handled by OnEvent.
|
||||||
func (s *Session) event(messageType int, message []byte) {
|
func (s *Session) event(messageType int, message []byte) {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
if s.Handlers == nil {
|
if s.handlers == nil {
|
||||||
s.RUnlock()
|
s.RUnlock()
|
||||||
s.initialize()
|
s.initialize()
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue