From 0197c6f7e8b63c004f2be9c667bcb60185252d76 Mon Sep 17 00:00:00 2001 From: Khinshan Khan Date: Sat, 29 Mar 2025 08:27:14 -0400 Subject: [PATCH 1/3] update readme --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ca7fa59..7b44191 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,23 @@ A playful Discord bot that sends random praises and (occasionally) a conspiracy ## Setup +### Create a Discord Bot + +> [!WARNING] +> Discord updates frequently -- if any steps seem outdated, refer to the official docs: +> https://discord.com/developers/docs/intro + +1. Log into Discord. +2. Go to [Discord Developer Portal](https://discord.com/developers/applications) +3. Click "New Application", give it a name, and create it. +4. In the sidebar, go to Bot, click "Add Bot". +5. Under OAuth2 > URL Generator: + 1. Select "bot" scope. + 2. Under Bot Permissions, check at least: + - Send Messages +6. Use the generated URL to invite the bot to your server. +7. In the Bot tab, click "Reset Token", copy the token and save it in your `.env` (view next step). + ### Configuration Create a `.env` file in the root of your project with the following keys: @@ -21,8 +38,37 @@ 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. -You can view the [.env-sample](./.env-sample) as an example. +You can view the [.env-sample](./.env-sample) as an example completed template. + +### Install + Run + +#### Vanilla (Local Dev) + +1. Install Go via https://go.dev/doc/install +2. Install Make if you don't have it via https://www.gnu.org/software/make/ +3. Run: + ```bash + make # builds the bot + ./bin/bot # starts the bot + ``` + +#### Docker + +> We maintain a ghcr image at ghcr.io/the-programmers-hangout/swiftspiracy-bot:latest + +1. Pull the image: + ```bash + docker pull ghcr.io/the-programmers-hangout/swiftspiracy-bot:latest + ``` +2. Run it with your `.env` file: + ```bash + docker run --env-file ./.env ghcr.io/the-programmers-hangout/swiftspiracy-bot:latest + ``` ## Notes - The bot gracefully shuts down on CTRL+C. +- Conspiracy messages are deleted after a short delay to keep the mystery alive, you should configure it to be low. +- Messages are sourced from [praises.json](./cmd/bot/praises.json) and + [conspiracies.json](./cmd/bot/conspiracies.json). If your timings for message are too short for the duration the bot + runs, the messages will repeat. From 4975bcfb6084d3f50f75e468adf8ca3557bec305 Mon Sep 17 00:00:00 2001 From: Khinshan Khan Date: Sat, 29 Mar 2025 08:32:13 -0400 Subject: [PATCH 2/3] reorganize the code and add in comments --- cmd/bot/main.go | 80 +++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/cmd/bot/main.go b/cmd/bot/main.go index e0db7ae..a378c07 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -25,26 +25,63 @@ var praisesFile embed.FS var conspiraciesFile embed.FS var ( + // messages sourced from the embedded files praises []string conspiracies []string + // internally track which messages have been sent praiseIndex int conspiracyIndex int ) +// Settings for bot sourced from env variables var ( botToken string channelID string -) -var ( SendMessageIntervalMin int SendMessageIntervalMax int SendMessageUnit time.Duration - DeleteConspiracyDelay time.Duration - ConspiracyProbability float64 + + DeleteConspiracyDelay time.Duration + ConspiracyProbability float64 ) +func main() { + if err := loadEnvConfig(); err != nil { + log.Fatalf("[x] Failed to load configuration: %v", err) + } + + // Load messages at build time + if err := loadMessages(); err != nil { + log.Fatalf("[x] Error loading messages: %v", err) + } + + // Initialize bot session + dg, err := discordgo.New("Bot " + botToken) + if err != nil { + log.Fatalf("[x] Error creating Discord session: %v", err) + } + + // Open WebSocket connection + dg.AddHandler(readyHandler) + if err := dg.Open(); err != nil { + log.Fatalf("[x] Error opening connection: %v", err) + } + defer dg.Close() + + // Start the message scheduler + go startScheduler(dg, channelID) + + // Graceful shutdown handling + log.Println("[✓] Swiftspiracy Bot is now running. Press CTRL+C to exit.") + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) + <-sc + + log.Println("[↓] Shutting down bot gracefully...") +} + func loadEnvConfig() error { if err := godotenv.Load(); err != nil { log.Println("[!] No .env file found, using system environment variables.") @@ -94,41 +131,6 @@ func loadEnvConfig() error { return nil } -func main() { - if err := loadEnvConfig(); err != nil { - log.Fatalf("[x] Failed to load configuration: %v", err) - } - - // Load messages at build time - if err := loadMessages(); err != nil { - log.Fatalf("[x] Error loading messages: %v", err) - } - - // Initialize bot session - dg, err := discordgo.New("Bot " + botToken) - if err != nil { - log.Fatalf("[x] Error creating Discord session: %v", err) - } - - // Open WebSocket connection - dg.AddHandler(readyHandler) - if err := dg.Open(); err != nil { - log.Fatalf("[x] Error opening connection: %v", err) - } - defer dg.Close() - - // Start the message scheduler - go startScheduler(dg, channelID) - - // Graceful shutdown handling - log.Println("[✓] Swiftspiracy Bot is now running. Press CTRL+C to exit.") - sc := make(chan os.Signal, 1) - signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) - <-sc - - log.Println("[↓] Shutting down bot gracefully...") -} - // loadMessages loads JSON files into memory at build time func loadMessages() error { var err error From 9498459f7b5a9b1a2990dfbfdca5041e2f361a37 Mon Sep 17 00:00:00 2001 From: Khinshan Khan Date: Sat, 29 Mar 2025 08:55:27 -0400 Subject: [PATCH 3/3] use github context over hardcoded values in gha --- .github/workflows/publish-latest.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/publish-latest.yml b/.github/workflows/publish-latest.yml index 852148d..d3ee9ba 100644 --- a/.github/workflows/publish-latest.yml +++ b/.github/workflows/publish-latest.yml @@ -17,12 +17,12 @@ jobs: - uses: docker/setup-qemu-action@v3 - uses: docker/setup-buildx-action@v3 - name: Get image repository - run: echo IMAGE_REPOSITORY=$(echo ghcr.io/the-programmers-hangout/${{ github.event.repository.name }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV + run: echo IMAGE_REPOSITORY=$(echo ghcr.io/${{ github.repository }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV - name: Login to registry uses: docker/login-action@v3 with: registry: ghcr.io - username: the-programmers-hangout + username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push Docker image uses: docker/build-push-action@v6