Token validation is now performed by attempting to make a socket connection
FIxes
MODIFY
app/app.go
app/app.go
@@ -62,7 +62,7 @@ func Run() {
panic(shortcutsLoadError)
}
- discord := attemptLogin(loginScreen, "", configuration)
+ discord, readyEvent := attemptLogin(loginScreen, "", configuration)
config.GetConfig().Token = discord.Token
@@ -72,20 +72,7 @@ func Run() {
log.Fatalf("Error persisting configuration (%s).\n", persistError.Error())
}
- readyChan := make(chan *discordgo.Ready, 1)
- discord.AddHandlerOnce(func(s *discordgo.Session, event *discordgo.Ready) {
- readyChan <- event
- })
-
discord.State.MaxMessageCount = 100
- discordError := discord.Open()
- if discordError != nil {
- app.Stop()
- log.Fatalln("Error establishing web socket connection", discordError)
- }
-
- readyEvent := <-readyChan
- close(readyChan)
readstate.Load(discord.State)
@@ -175,9 +162,10 @@ func Run() {
}
}
-func attemptLogin(loginScreen *ui.Login, loginMessage string, configuration *config.Config) *discordgo.Session {
+func attemptLogin(loginScreen *ui.Login, loginMessage string, configuration *config.Config) (*discordgo.Session, *discordgo.Ready) {
var (
session *discordgo.Session
+ readyEvent *discordgo.Ready
discordError error
)
@@ -185,18 +173,27 @@ func attemptLogin(loginScreen *ui.Login, loginMessage string, configuration *con
session, discordError = loginScreen.RequestLogin(loginMessage)
} else {
session, discordError = discordgo.NewWithToken(userAgent, configuration.Token)
- //When logging in via token, the token isn't ever validated, therefore we do a test lookup here.
- _, discordError = session.UserGuilds(0, "", "")
- if discordError == discordgo.ErrUnauthorized {
+ }
+
+ if discordError != nil {
configuration.Token = ""
return attemptLogin(loginScreen, fmt.Sprintf("Error during last login attempt:\n\n[red]%s", discordError), configuration)
}
- }
+
+ readyChan := make(chan *discordgo.Ready, 1)
+ session.AddHandlerOnce(func(s *discordgo.Session, event *discordgo.Ready) {
+ readyChan <- event
+ })
+
+ discordError = session.Open()
if discordError != nil {
configuration.Token = ""
- return attemptLogin(loginScreen, fmt.Sprintf("Error during last login attempt:\n\n[red]%s", discordError), configuration)
+ return attemptLogin(loginScreen, fmt.Sprintf("Error during last login attempt:\n\n[red]%s", discordError), configuration);
}
- return session
+ readyEvent = <-readyChan
+ close(readyChan)
+
+ return session, readyEvent
}

MODIFY
ui/login.go
ui/login.go
@@ -265,13 +265,7 @@ func (login *Login) attemptLogin() {
panic("Was in state loginType=None during login attempt.")
case Token:
session, loginError := discordgo.NewWithToken(userAgent, login.tokenInput.GetText())
- if loginError != nil || session == nil {
login.sessionChannel <- &loginAttempt{session, loginError}
- } else {
- //Dummy lookup for validating the token.
- _, loginError = session.UserGuilds(0, "", "")
- login.sessionChannel <- &loginAttempt{session, loginError}
- }
case Password:
// Even if the login is supposed to be without two-factor-authentication, we
// attempt parsing a 2fa code, since the underlying rest-call can also handle

Token validation is now performed by attempting to make a socket connection

FIxes #206