Browse Source

Add better error message when QR login fails due to captcha

Closes #38
Tulir Asokan 2 years ago
parent
commit
9f95a1b686
1 changed files with 11 additions and 1 deletions
  1. 11 1
      commands.go

+ 11 - 1
commands.go

@@ -17,11 +17,15 @@
 package main
 
 import (
+	"bytes"
 	"context"
+	"errors"
 	"fmt"
+	"net/http"
 	"strconv"
 	"strings"
 
+	"github.com/bwmarrin/discordgo"
 	"github.com/skip2/go-qrcode"
 
 	"maunium.net/go/mautrix"
@@ -142,7 +146,13 @@ func fnLoginQR(ce *WrappedCommandEvent) {
 
 	user, err := client.Result()
 	if err != nil || len(user.Token) == 0 {
-		ce.Reply("Error logging in: %v", err)
+		if restErr := (&discordgo.RESTError{}); errors.As(err, &restErr) &&
+			restErr.Response.StatusCode == http.StatusBadRequest &&
+			bytes.Contains(restErr.ResponseBody, []byte("captcha-required")) {
+			ce.Reply("Error logging in: %v\n\nCAPTCHAs are currently not supported - use token login instead", err)
+		} else {
+			ce.Reply("Error logging in: %v", err)
+		}
 		return
 	} else if err = ce.User.Login(user.Token); err != nil {
 		ce.Reply("Error connecting after login: %v", err)