diff --git a/chess.go b/chess.go index 9d2f459..22c16e1 100644 --- a/chess.go +++ b/chess.go @@ -59,6 +59,7 @@ func chessCommand(m chat1.MsgSummary) { game.Move = true game.StartTime = time.Now() config.Games[convID] = game + saveConfig() k.SendMessageByConvID(m.ConvID, fmt.Sprintf("I have created a new game for this conversation @%+v is playing as White, @%+v is playing as Black.", game.White, game.Black)) k.SendMessageByConvID(m.ConvID, fmt.Sprintf("Possible commands are `@chessbot show` to show the board, or `@chessbot forfeit`. Otherwise I will be expecting a move in Algebraic Notation.")) @@ -103,6 +104,7 @@ func showBoard(m chat1.MsgSummary, g *Game) { func submitMove(m chat1.MsgSummary, g *Game) { defer log.PanicSafe() + defer saveConfig() parts := strings.Split(m.Content.Text.Body, " ") if g.Move { if m.Sender.Username != g.White { diff --git a/config.go b/config.go index 6b03ba4..a644b6a 100644 --- a/config.go +++ b/config.go @@ -5,20 +5,16 @@ import ( "io/ioutil" ) -func loadConfig() { +func loadConfig() Config { var c Config confFile, _ := ioutil.ReadFile(configFile) - err := json.Unmarshal([]byte(confFile), &c) - if err != nil { - log.LogErrorType(err) - return - } - config = c - log.LogInfo("Setup completed using config file.") + _ = json.Unmarshal([]byte(confFile), &c) + return c } func saveConfig() { defer log.PanicSafe() + log.LogInfo("Saving config.") file, err := json.Marshal(config) if err != nil { log.LogErrorType(err) diff --git a/main.go b/main.go index 789a0d3..806b595 100755 --- a/main.go +++ b/main.go @@ -19,14 +19,13 @@ var ( k = keybase.NewKeybase() configFile = "config.json" - config Config + config = loadConfig() log = loggy.NewLogger(config.LogOpts) r = rand.New(rand.NewSource(time.Now().UnixNano())) ) func printChat(m chat1.MsgSummary) { defer log.PanicSafe() - defer saveConfig() if m.Sender.Username == k.Username { return } @@ -38,7 +37,6 @@ func printChat(m chat1.MsgSummary) { } func main() { fmt.Println("Loading config.") - loadConfig() fmt.Println("Starting log") log = loggy.NewLogger(config.LogOpts) fmt.Println("Log configured")