From bd2720daaed3b1e483ae9277ffda89073f415b99 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Sat, 21 Jan 2023 12:01:27 +0530 Subject: [PATCH] refactor: new func openSession creates and opens a new session --- login_form.go | 3 +-- main.go | 4 +--- state.go | 11 ++++++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/login_form.go b/login_form.go index fa2bc8c..6ff679a 100644 --- a/login_form.go +++ b/login_form.go @@ -63,8 +63,7 @@ func (lf *LoginForm) onLoginButtonSelected() { } // We got the token, return with a new Session. - discordState = newState(l.Token) - err = discordState.Open(context.Background()) + discordState, err = openState(l.Token) if err != nil { log.Fatal(err) } diff --git a/main.go b/main.go index 47a624f..2b423f8 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "flag" "log" "os" @@ -77,8 +76,7 @@ func main() { if token == "" { app.SetRoot(newLoginForm(), true) } else { - discordState = newState(token) - err = discordState.Open(context.Background()) + discordState, err = openState(token) if err != nil { log.Fatal(err) } diff --git a/state.go b/state.go index 452474c..a6f3a96 100644 --- a/state.go +++ b/state.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "log" "runtime" @@ -26,18 +27,22 @@ type State struct { *state.State } -func newState(token string) *State { +func openState(token string) (*State, error) { s := &State{ State: state.New(token), } + // Handlers s.AddHandler(s.onReady) s.AddHandler(s.onMessageCreate) - s.StateLog = s.onLog s.OnRequest = append(s.Client.OnRequest, s.onRequest) - return s + if err := s.Open(context.TODO()); err != nil { + return nil, err + } + + return s, nil } func (s *State) onLog(err error) {