From d15c9e34910cb798edaac5f92e1302f5746b90d6 Mon Sep 17 00:00:00 2001 From: Khinshan Khan Date: Tue, 1 Apr 2025 10:22:49 -0400 Subject: [PATCH] add logic to help resume bot from given indices --- .env-sample | 4 ++++ README.md | 5 +++++ cmd/bot/main.go | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/.env-sample b/.env-sample index 9dc84f2..41af447 100644 --- a/.env-sample +++ b/.env-sample @@ -12,3 +12,7 @@ DELETE_CONSPIRACY_DELAY=3s # Probability of sending a conspiracy message (0.0 to 1.0) CONSPIRACY_PROBABILITY=0.4 + +# Optional indices for resuming bot +PRAISE_INDEX=5 +CONSPIRACY_INDEX=5 diff --git a/README.md b/README.md index 7b44191..260cbf0 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,11 @@ Create a `.env` file in the root of your project with the following keys: - `CONSPIRACY_PROBABILITY`: Probability (from 0.0 to 1.0) that a conspiracy message is sent after a praise. Eg, 0.4 = 40% chance. +Optional: +- `PRAISE_INDEX`: Index of the [praises.json](./cmd/bot/praises.json) to start from, useful when resuming bot. +- `CONSPIRACY_INDEX`: Index of the [conspiracies.json](./cmd/bot/conspiracies.json) to start from, useful when resuming + bot. + You can view the [.env-sample](./.env-sample) as an example completed template. ### Install + Run diff --git a/cmd/bot/main.go b/cmd/bot/main.go index cc0a3c8..a1dc9d4 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -154,6 +154,25 @@ func loadEnvConfig() error { return fmt.Errorf("CONSPIRACY_PROBABILITY: %w", err) } + // Get values in case of resuming at an index + praiseIndex = 0 + if valStr := os.Getenv("PRAISE_INDEX"); valStr != "" { + praiseIndex, err = strconv.Atoi(valStr) + if err != nil { + log.Printf("[!] Warning: %s is not a valid int, using default %d", "PRAISE_INDEX", 0) + praiseIndex = 0 + } + } + + conspiracyIndex = 0 + if valStr := os.Getenv("CONSPIRACY_INDEX"); valStr != "" { + conspiracyIndex, err = strconv.Atoi(valStr) + if err != nil { + log.Printf("[!] Warning: %s is not a valid int, using default %d", "CONSPIRACY_INDEX", 0) + conspiracyIndex = 0 + } + } + return nil }