Skip to content

Commit b2618d9

Browse files
authored
Merge pull request #4 from the-programmers-hangout/minor-changes
"Minor changes"
2 parents 5305fa7 + 9498459 commit b2618d9

File tree

3 files changed

+90
-42
lines changed

3 files changed

+90
-42
lines changed

.github/workflows/publish-latest.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ jobs:
1717
- uses: docker/setup-qemu-action@v3
1818
- uses: docker/setup-buildx-action@v3
1919
- name: Get image repository
20-
run: echo IMAGE_REPOSITORY=$(echo ghcr.io/the-programmers-hangout/${{ github.event.repository.name }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
20+
run: echo IMAGE_REPOSITORY=$(echo ghcr.io/${{ github.repository }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
2121
- name: Login to registry
2222
uses: docker/login-action@v3
2323
with:
2424
registry: ghcr.io
25-
username: the-programmers-hangout
25+
username: ${{ github.repository_owner }}
2626
password: ${{ secrets.GITHUB_TOKEN }}
2727
- name: Build and push Docker image
2828
uses: docker/build-push-action@v6

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ A playful Discord bot that sends random praises and (occasionally) a conspiracy
44

55
## Setup
66

7+
### Create a Discord Bot
8+
9+
> [!WARNING]
10+
> Discord updates frequently -- if any steps seem outdated, refer to the official docs:
11+
> https://discord.com/developers/docs/intro
12+
13+
1. Log into Discord.
14+
2. Go to [Discord Developer Portal](https://discord.com/developers/applications)
15+
3. Click "New Application", give it a name, and create it.
16+
4. In the sidebar, go to Bot, click "Add Bot".
17+
5. Under OAuth2 > URL Generator:
18+
1. Select "bot" scope.
19+
2. Under Bot Permissions, check at least:
20+
- Send Messages
21+
6. Use the generated URL to invite the bot to your server.
22+
7. In the Bot tab, click "Reset Token", copy the token and save it in your `.env` (view next step).
23+
724
### Configuration
825

926
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:
2138
- `CONSPIRACY_PROBABILITY`: Probability (from 0.0 to 1.0) that a conspiracy message is sent after a praise. Eg, 0.4 =
2239
40% chance.
2340

24-
You can view the [.env-sample](./.env-sample) as an example.
41+
You can view the [.env-sample](./.env-sample) as an example completed template.
42+
43+
### Install + Run
44+
45+
#### Vanilla (Local Dev)
46+
47+
1. Install Go via https://go.dev/doc/install
48+
2. Install Make if you don't have it via https://www.gnu.org/software/make/
49+
3. Run:
50+
```bash
51+
make # builds the bot
52+
./bin/bot # starts the bot
53+
```
54+
55+
#### Docker
56+
57+
> We maintain a ghcr image at ghcr.io/the-programmers-hangout/swiftspiracy-bot:latest
58+
59+
1. Pull the image:
60+
```bash
61+
docker pull ghcr.io/the-programmers-hangout/swiftspiracy-bot:latest
62+
```
63+
2. Run it with your `.env` file:
64+
```bash
65+
docker run --env-file ./.env ghcr.io/the-programmers-hangout/swiftspiracy-bot:latest
66+
```
2567

2668
## Notes
2769

2870
- The bot gracefully shuts down on CTRL+C.
71+
- Conspiracy messages are deleted after a short delay to keep the mystery alive, you should configure it to be low.
72+
- Messages are sourced from [praises.json](./cmd/bot/praises.json) and
73+
[conspiracies.json](./cmd/bot/conspiracies.json). If your timings for message are too short for the duration the bot
74+
runs, the messages will repeat.

cmd/bot/main.go

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,63 @@ var praisesFile embed.FS
2525
var conspiraciesFile embed.FS
2626

2727
var (
28+
// messages sourced from the embedded files
2829
praises []string
2930
conspiracies []string
3031

32+
// internally track which messages have been sent
3133
praiseIndex int
3234
conspiracyIndex int
3335
)
3436

37+
// Settings for bot sourced from env variables
3538
var (
3639
botToken string
3740
channelID string
38-
)
3941

40-
var (
4142
SendMessageIntervalMin int
4243
SendMessageIntervalMax int
4344
SendMessageUnit time.Duration
44-
DeleteConspiracyDelay time.Duration
45-
ConspiracyProbability float64
45+
46+
DeleteConspiracyDelay time.Duration
47+
ConspiracyProbability float64
4648
)
4749

50+
func main() {
51+
if err := loadEnvConfig(); err != nil {
52+
log.Fatalf("[x] Failed to load configuration: %v", err)
53+
}
54+
55+
// Load messages at build time
56+
if err := loadMessages(); err != nil {
57+
log.Fatalf("[x] Error loading messages: %v", err)
58+
}
59+
60+
// Initialize bot session
61+
dg, err := discordgo.New("Bot " + botToken)
62+
if err != nil {
63+
log.Fatalf("[x] Error creating Discord session: %v", err)
64+
}
65+
66+
// Open WebSocket connection
67+
dg.AddHandler(readyHandler)
68+
if err := dg.Open(); err != nil {
69+
log.Fatalf("[x] Error opening connection: %v", err)
70+
}
71+
defer dg.Close()
72+
73+
// Start the message scheduler
74+
go startScheduler(dg, channelID)
75+
76+
// Graceful shutdown handling
77+
log.Println("[✓] Swiftspiracy Bot is now running. Press CTRL+C to exit.")
78+
sc := make(chan os.Signal, 1)
79+
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
80+
<-sc
81+
82+
log.Println("[↓] Shutting down bot gracefully...")
83+
}
84+
4885
func loadEnvConfig() error {
4986
if err := godotenv.Load(); err != nil {
5087
log.Println("[!] No .env file found, using system environment variables.")
@@ -94,41 +131,6 @@ func loadEnvConfig() error {
94131
return nil
95132
}
96133

97-
func main() {
98-
if err := loadEnvConfig(); err != nil {
99-
log.Fatalf("[x] Failed to load configuration: %v", err)
100-
}
101-
102-
// Load messages at build time
103-
if err := loadMessages(); err != nil {
104-
log.Fatalf("[x] Error loading messages: %v", err)
105-
}
106-
107-
// Initialize bot session
108-
dg, err := discordgo.New("Bot " + botToken)
109-
if err != nil {
110-
log.Fatalf("[x] Error creating Discord session: %v", err)
111-
}
112-
113-
// Open WebSocket connection
114-
dg.AddHandler(readyHandler)
115-
if err := dg.Open(); err != nil {
116-
log.Fatalf("[x] Error opening connection: %v", err)
117-
}
118-
defer dg.Close()
119-
120-
// Start the message scheduler
121-
go startScheduler(dg, channelID)
122-
123-
// Graceful shutdown handling
124-
log.Println("[✓] Swiftspiracy Bot is now running. Press CTRL+C to exit.")
125-
sc := make(chan os.Signal, 1)
126-
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
127-
<-sc
128-
129-
log.Println("[↓] Shutting down bot gracefully...")
130-
}
131-
132134
// loadMessages loads JSON files into memory at build time
133135
func loadMessages() error {
134136
var err error

0 commit comments

Comments
 (0)