forked from pothtonswer/discordmuffin
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.")
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
70
structs.go
70
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.
|
||||
|
|
2
wsapi.go
2
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 {
|
||||
|
|
Loading…
Reference in a new issue