1 Commits

Author SHA1 Message Date
richard 6c56192141 Add startup logging and Discord ready timeout
Adds log lines at each startup step so the log file is never empty on
failure. Adds a 30s timeout on the Discord ready event so a bad token or
blocked network produces a clear log message instead of a silent hang.
Falls back to %TEMP% for the log file if the exe directory isn't writable.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-01 19:31:21 +03:00
+28 -7
View File
@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
"fyne.io/systray"
"github.com/bwmarrin/discordgo"
@@ -120,19 +121,34 @@ func toggleMute(dg *discordgo.Session, cfg *Config, muted bool) {
}
}
func run() {
// Log to file next to the executable so errors are visible even without a console.
func setupLogging() {
// Try exe directory first, fall back to temp dir (e.g. if installed in Program Files).
var dirs []string
if exe, err := os.Executable(); err == nil {
logPath := filepath.Join(filepath.Dir(exe), "discord-lobby-mute.log")
if f, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644); err == nil {
dirs = append(dirs, filepath.Dir(exe))
}
dirs = append(dirs, os.TempDir())
for _, dir := range dirs {
logPath := filepath.Join(dir, "discord-lobby-mute.log")
f, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
continue
}
log.SetOutput(io.MultiWriter(os.Stderr, f))
return
}
}
}
func run() {
setupLogging()
log.Println("starting")
cfg, err := loadConfig()
if err != nil {
log.Fatal(err)
}
log.Println("config loaded")
dg, err := discordgo.New("Bot " + cfg.Token)
if err != nil {
@@ -145,13 +161,18 @@ func run() {
close(ready)
})
log.Println("connecting to Discord...")
if err := dg.Open(); err != nil {
log.Fatalf("connect to Discord: %v", err)
}
defer dg.Close()
<-ready
log.Println("Connected to Discord.")
select {
case <-ready:
case <-time.After(30 * time.Second):
log.Fatal("timed out waiting for Discord ready — check token and network connection")
}
log.Println("connected to Discord")
keydownCh, err := startHotkey(cfg.Keybind)
if err != nil {