From cb5b8123868cc1571005f583e6aa7c4f4fe1a12b Mon Sep 17 00:00:00 2001 From: Khinshan Khan Date: Tue, 1 Apr 2025 10:07:18 -0400 Subject: [PATCH 1/5] add interaction handler to debug build info --- Makefile | 2 +- cmd/bot/main.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 30129aa..da96068 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ MAKEFLAGS += --no-builtin-rules # VARIABLES GIT_COMMIT = $(shell git rev-parse --verify HEAD) -BUILD_DATE = $(shell date +ā€%Y.%m.%d.%H%M%Sā€) +BUILD_DATE = $(shell date +%Y.%m.%d.%H%M%S) GOSERVICES = $(sort $(notdir $(realpath $(dir $(wildcard ./cmd/*/main.go))))) diff --git a/cmd/bot/main.go b/cmd/bot/main.go index a378c07..4bc5b26 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -16,6 +16,14 @@ import ( "github.com/joho/godotenv" ) +var ( + // CommitHash and BuildData get replaced during build with the commit hash and time of build. + CommitHash string + BuildDate string + // StartTime is reset every time the application is restarted + StartTime = time.Now() +) + // Embed JSON files at build time //go:embed praises.json @@ -63,13 +71,31 @@ func main() { log.Fatalf("[x] Error creating Discord session: %v", err) } - // Open WebSocket connection dg.AddHandler(readyHandler) + interactionHandlers := make(map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)) + for _, interaction := range interactions { + interactionHandlers[interaction.command.Name] = interaction.handler + } + dg.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { + if h, ok := interactionHandlers[i.ApplicationCommandData().Name]; ok { + h(s, i) + } + }) + + // Open WebSocket connection if err := dg.Open(); err != nil { log.Fatalf("[x] Error opening connection: %v", err) } defer dg.Close() + for _, entry := range interactions { + created, err := dg.ApplicationCommandCreate(dg.State.User.ID, "", entry.command) + if err != nil { + log.Fatalf("Cannot create '%s' command: %v", entry.command.Name, err) + } + interactionHandlers[created.Name] = entry.handler + } + // Start the message scheduler go startScheduler(dg, channelID) @@ -161,6 +187,36 @@ func readyHandler(s *discordgo.Session, r *discordgo.Ready) { log.Println("[āœ“] Swiftspiracy Bot is online and ready!") } +var interactions = []struct { + command *discordgo.ApplicationCommand + handler func(s *discordgo.Session, i *discordgo.InteractionCreate) +}{ + { + command: &discordgo.ApplicationCommand{ + Name: "buildinfo", + Description: "Get the bot's build info and uptime", + }, + handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + uptime := time.Since(StartTime).Round(time.Second) + response := fmt.Sprintf( + ":tools: Build Info:\n- Commit Hash: `%s`\n- Build Date: `%s`\n- Uptime: `%s`", + CommitHash, + BuildDate, + uptime, + ) + err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: response, + }, + }) + if err != nil { + log.Printf("[x] Failed to respond to interaction: %v", err) + } + }, + }, +} + // startScheduler handles sending messages at intervals func startScheduler(s *discordgo.Session, channelID string) { randomDuration := 0 * time.Second From af937d3e6d94fc844c9a81bde8b82b605f558e2f Mon Sep 17 00:00:00 2001 From: Khinshan Khan Date: Tue, 1 Apr 2025 10:10:30 -0400 Subject: [PATCH 2/5] make buildinfo reply ephemeral --- cmd/bot/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 4bc5b26..cc0a3c8 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -207,6 +207,7 @@ var interactions = []struct { err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ + Flags: discordgo.MessageFlagsEphemeral, Content: response, }, }) From d15c9e34910cb798edaac5f92e1302f5746b90d6 Mon Sep 17 00:00:00 2001 From: Khinshan Khan Date: Tue, 1 Apr 2025 10:22:49 -0400 Subject: [PATCH 3/5] 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 } From f67b60ab26272a3a515ed9c6e4444d10e4c00130 Mon Sep 17 00:00:00 2001 From: Khinshan Khan Date: Tue, 1 Apr 2025 10:23:30 -0400 Subject: [PATCH 4/5] rename sample env for github --- .env-sample => .env.sample | 0 README.md | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename .env-sample => .env.sample (100%) diff --git a/.env-sample b/.env.sample similarity index 100% rename from .env-sample rename to .env.sample diff --git a/README.md b/README.md index 260cbf0..5c97db9 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Optional: - `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. +You can view the [.env.sample](./.env.sample) as an example completed template. ### Install + Run From 1fec7a20622bb6d62b48298ed0e8ae87a2add0d4 Mon Sep 17 00:00:00 2001 From: Khinshan Khan Date: Tue, 1 Apr 2025 10:24:55 -0400 Subject: [PATCH 5/5] default indices should be 0 and commented out --- .env.sample | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.env.sample b/.env.sample index 41af447..76ee09e 100644 --- a/.env.sample +++ b/.env.sample @@ -14,5 +14,5 @@ DELETE_CONSPIRACY_DELAY=3s CONSPIRACY_PROBABILITY=0.4 # Optional indices for resuming bot -PRAISE_INDEX=5 -CONSPIRACY_INDEX=5 +# PRAISE_INDEX=0 +# CONSPIRACY_INDEX=0