Browse Source

Arise error when user has 2FA (#359)

* Arise error when user has 2FA

* Fixed error message

* Removed ticket
LEGOlord208 8 years ago
parent
commit
65f0cb9f73
3 changed files with 12 additions and 1 deletions
  1. 9 1
      discord.go
  2. 2 0
      restapi.go
  3. 1 0
      structs.go

+ 9 - 1
discord.go

@@ -14,6 +14,7 @@
 package discordgo
 
 import (
+	"errors"
 	"fmt"
 	"net/http"
 	"time"
@@ -22,6 +23,9 @@ import (
 // VERSION of Discordgo, follows Symantic Versioning. (http://semver.org/)
 const VERSION = "0.16.0-dev"
 
+// ErrMFA will be risen by New when the user has 2FA.
+var ErrMFA = errors.New("account has 2FA enabled")
+
 // New creates a new Discord session and will automate some startup
 // tasks if given enough information to do so.  Currently you can pass zero
 // arguments and it will return an empty Discord session.
@@ -119,7 +123,11 @@ func New(args ...interface{}) (s *Session, err error) {
 	} else {
 		err = s.Login(auth, pass)
 		if err != nil || s.Token == "" {
-			err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
+			if s.MFA {
+				err = ErrMFA
+			} else {
+				err = fmt.Errorf("Unable to fetch discord authentication token. %v", err)
+			}
 			return
 		}
 	}

+ 2 - 0
restapi.go

@@ -187,6 +187,7 @@ func (s *Session) Login(email, password string) (err error) {
 
 	temp := struct {
 		Token string `json:"token"`
+		MFA   bool   `json:"mfa"`
 	}{}
 
 	err = unmarshal(response, &temp)
@@ -195,6 +196,7 @@ func (s *Session) Login(email, password string) (err error) {
 	}
 
 	s.Token = temp.Token
+	s.MFA = temp.MFA
 	return
 }
 

+ 1 - 0
structs.go

@@ -29,6 +29,7 @@ type Session struct {
 
 	// Authentication token for this session
 	Token string
+	MFA   bool
 
 	// Debug for printing JSON request/responses
 	Debug    bool // Deprecated, will be removed.