From 9f95a1b686e0e6f929b47a333e67150a4771a91c Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 27 Jan 2023 23:35:26 +0200 Subject: [PATCH] Add better error message when QR login fails due to captcha Closes #38 --- commands.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 186848e..2a22aef 100644 --- a/commands.go +++ b/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)