123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- package discordgo
- import (
- "fmt"
- "reflect"
- )
- const VERSION = "0.11.0-alpha"
- func New(args ...interface{}) (s *Session, err error) {
-
- s = &Session{
- State: NewState(),
- StateEnabled: true,
- Compress: true,
- ShouldReconnectOnError: true,
- }
-
-
- if args == nil {
- return
- }
-
- var auth, pass string
-
- for _, arg := range args {
- switch v := arg.(type) {
- case []string:
- if len(v) > 3 {
- err = fmt.Errorf("Too many string parameters provided.")
- return
- }
-
- if len(v) > 0 {
- auth = v[0]
- }
-
- if len(v) > 1 {
- pass = v[1]
- }
-
- if len(v) > 2 {
- s.Token = v[2]
- }
- case string:
-
-
-
- if auth == "" {
- auth = v
- } else if pass == "" {
- pass = v
- } else if s.Token == "" {
- s.Token = v
- } else {
- err = fmt.Errorf("Too many string parameters provided.")
- return
- }
-
-
- default:
- err = fmt.Errorf("Unsupported parameter type provided.")
- return
- }
- }
-
-
-
-
- if pass == "" {
- s.Token = auth
- } else {
- err = s.Login(auth, pass)
- if err != nil || s.Token == "" {
- err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
- return
- }
- }
-
-
- return
- }
- func (s *Session) validateHandler(handler interface{}) reflect.Type {
- handlerType := reflect.TypeOf(handler)
- if handlerType.NumIn() != 2 {
- panic("Unable to add event handler, handler must be of the type func(*discordgo.Session, *discordgo.EventType).")
- }
- if handlerType.In(0) != reflect.TypeOf(s) {
- panic("Unable to add event handler, first argument must be of type *discordgo.Session.")
- }
- eventType := handlerType.In(1)
-
- if eventType.Kind() == reflect.Interface {
- eventType = nil
- }
- return eventType
- }
- func (s *Session) AddHandler(handler interface{}) func() {
- s.initialize()
- eventType := s.validateHandler(handler)
- s.handlersMu.Lock()
- defer s.handlersMu.Unlock()
- h := reflect.ValueOf(handler)
- handlers := s.handlers[eventType]
- if handlers == nil {
- handlers = []reflect.Value{}
- }
- s.handlers[eventType] = append(handlers, h)
-
-
-
- return func() {
- s.handlersMu.Lock()
- defer s.handlersMu.Unlock()
- handlers := s.handlers[eventType]
- for i, v := range handlers {
- if h == v {
- s.handlers[eventType] = append(handlers[:i], handlers[i+1:]...)
- return
- }
- }
- }
- }
- func (s *Session) handle(event interface{}) {
- s.handlersMu.RLock()
- defer s.handlersMu.RUnlock()
- if s.handlers == nil {
- return
- }
- handlerParameters := []reflect.Value{reflect.ValueOf(s), reflect.ValueOf(event)}
- if handlers, ok := s.handlers[reflect.TypeOf(event)]; ok {
- for _, handler := range handlers {
- handler.Call(handlerParameters)
- }
- }
- if handlers, ok := s.handlers[nil]; ok {
- for _, handler := range handlers {
- handler.Call(handlerParameters)
- }
- }
- }
- func (s *Session) initialize() {
- s.handlersMu.Lock()
- if s.handlers != nil {
- s.handlersMu.Unlock()
- return
- }
- s.handlers = map[interface{}][]reflect.Value{}
- s.handlersMu.Unlock()
- s.AddHandler(s.onEvent)
- s.AddHandler(s.onReady)
- s.AddHandler(s.onVoiceServerUpdate)
- s.AddHandler(s.onVoiceStateUpdate)
- s.AddHandler(s.State.onInterface)
- }
- func (s *Session) onEvent(se *Session, e *Event) {
- printEvent(e)
- }
- func (s *Session) onReady(se *Session, r *Ready) {
- go s.heartbeat(s.wsConn, s.listening, r.HeartbeatInterval)
- }
|