first commit
This commit is contained in:
66
main.go
Normal file
66
main.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"cycling-discord-bot/bot"
|
||||
"cycling-discord-bot/db"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_ = godotenv.Load()
|
||||
|
||||
token := mustEnv("DISCORD_TOKEN")
|
||||
dbPath := getEnv("DB_PATH", "cycling_bot.db")
|
||||
guildID := getEnv("GUILD_ID", "") // empty = global commands (takes ~1h to propagate)
|
||||
|
||||
database, err := db.Open(dbPath)
|
||||
if err != nil {
|
||||
log.Fatalf("open database: %v", err)
|
||||
}
|
||||
defer database.Close()
|
||||
|
||||
b, err := bot.New(token, database, guildID)
|
||||
if err != nil {
|
||||
log.Fatalf("create bot: %v", err)
|
||||
}
|
||||
|
||||
if err := b.Open(); err != nil {
|
||||
log.Fatalf("open connection: %v", err)
|
||||
}
|
||||
defer b.Close()
|
||||
|
||||
// Give the session a moment to identify before registering commands
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
if err := b.RegisterCommands(); err != nil {
|
||||
log.Fatalf("register commands: %v", err)
|
||||
}
|
||||
|
||||
log.Println("bot is running — press Ctrl+C to stop")
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-stop
|
||||
log.Println("shutting down")
|
||||
}
|
||||
|
||||
func mustEnv(key string) string {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
log.Fatalf("missing required env var: %s", key)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func getEnv(key, fallback string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
Reference in New Issue
Block a user