mirror of
https://github.com/the-programmers-hangout/swiftspiracy-bot.git
synced 2026-09-09 16:06:26 +02:00
reorganize the code and add in comments
This commit is contained in:
+41
-39
@@ -25,26 +25,63 @@ var praisesFile embed.FS
|
|||||||
var conspiraciesFile embed.FS
|
var conspiraciesFile embed.FS
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// messages sourced from the embedded files
|
||||||
praises []string
|
praises []string
|
||||||
conspiracies []string
|
conspiracies []string
|
||||||
|
|
||||||
|
// internally track which messages have been sent
|
||||||
praiseIndex int
|
praiseIndex int
|
||||||
conspiracyIndex int
|
conspiracyIndex int
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Settings for bot sourced from env variables
|
||||||
var (
|
var (
|
||||||
botToken string
|
botToken string
|
||||||
channelID string
|
channelID string
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
SendMessageIntervalMin int
|
SendMessageIntervalMin int
|
||||||
SendMessageIntervalMax int
|
SendMessageIntervalMax int
|
||||||
SendMessageUnit time.Duration
|
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 {
|
func loadEnvConfig() error {
|
||||||
if err := godotenv.Load(); err != nil {
|
if err := godotenv.Load(); err != nil {
|
||||||
log.Println("[!] No .env file found, using system environment variables.")
|
log.Println("[!] No .env file found, using system environment variables.")
|
||||||
@@ -94,41 +131,6 @@ func loadEnvConfig() error {
|
|||||||
return nil
|
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
|
// loadMessages loads JSON files into memory at build time
|
||||||
func loadMessages() error {
|
func loadMessages() error {
|
||||||
var err error
|
var err error
|
||||||
|
|||||||
Reference in New Issue
Block a user