main.go aktualisiert
All checks were successful
release-tag / release-image (push) Successful in 1m27s
All checks were successful
release-tag / release-image (push) Successful in 1m27s
This commit is contained in:
74
main.go
74
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
@@ -19,11 +20,59 @@ const (
|
||||
defaultCategory = "Private Räume"
|
||||
)
|
||||
|
||||
// Pfad zur Persistenz-Datei (überschreibbar via CONFIG_PATH)
|
||||
var configPath = func() string {
|
||||
if v := os.Getenv("CONFIG_PATH"); v != "" {
|
||||
return v
|
||||
}
|
||||
return "guild_config.json"
|
||||
}()
|
||||
|
||||
// Persistenz der Guild-Konfiguration
|
||||
func loadGuildCfgs() error {
|
||||
f, err := os.Open(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var m map[string]*GuildConfig
|
||||
if err := json.NewDecoder(f).Decode(&m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfgMu.Lock()
|
||||
for k, v := range m {
|
||||
guildCfgs[k] = v
|
||||
}
|
||||
cfgMu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveGuildCfgs() error {
|
||||
tmp := configPath + ".tmp"
|
||||
f, err := os.Create(tmp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
enc := json.NewEncoder(f)
|
||||
enc.SetIndent("", " ")
|
||||
|
||||
cfgMu.RLock()
|
||||
err = enc.Encode(guildCfgs)
|
||||
cfgMu.RUnlock()
|
||||
_ = f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp, configPath)
|
||||
}
|
||||
|
||||
// ===== Per-Guild Config (in-memory) =====
|
||||
type GuildConfig struct {
|
||||
LobbyName string
|
||||
CategoryName string
|
||||
TimeoutMin int
|
||||
LobbyName string `json:"lobby_name"`
|
||||
CategoryName string `json:"category_name"`
|
||||
TimeoutMin int `json:"timeout_min"`
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -223,6 +272,7 @@ func createPrivateVCAndMove(
|
||||
}
|
||||
|
||||
go watchAndCleanup(s, guildID, newChan.ID, time.Duration(timeoutMin)*time.Minute)
|
||||
log.Println("➕ Added channel for guildID: " + guildID + " with new ID: " + newChan.ID)
|
||||
return newChan, nil
|
||||
}
|
||||
|
||||
@@ -252,6 +302,7 @@ func watchAndCleanup(s *discordgo.Session, guildID, channelID string, timeout ti
|
||||
}
|
||||
if time.Since(lastActive) >= timeout {
|
||||
_, _ = s.ChannelDelete(channelID)
|
||||
log.Println("➖ Deleted channel for guildID: " + guildID + " with ID: " + channelID)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -393,6 +444,9 @@ func onInteractionCreate(_ string) func(s *discordgo.Session, i *discordgo.Inter
|
||||
cfgMu.Lock()
|
||||
getCfg(guildID).LobbyName = name
|
||||
cfgMu.Unlock()
|
||||
if err := saveGuildCfgs(); err != nil {
|
||||
log.Printf("Speichern fehlgeschlagen: %v", err)
|
||||
}
|
||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{Content: "✅ Lobby-Name aktualisiert auf: " + name, Flags: discordgo.MessageFlagsEphemeral}})
|
||||
|
||||
case "setcategory":
|
||||
@@ -413,6 +467,9 @@ func onInteractionCreate(_ string) func(s *discordgo.Session, i *discordgo.Inter
|
||||
cfgMu.Lock()
|
||||
getCfg(guildID).CategoryName = name
|
||||
cfgMu.Unlock()
|
||||
if err := saveGuildCfgs(); err != nil {
|
||||
log.Printf("Speichern fehlgeschlagen: %v", err)
|
||||
}
|
||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{Content: "✅ Kategorie-Name aktualisiert auf: " + name, Flags: discordgo.MessageFlagsEphemeral}})
|
||||
|
||||
case "settimeout":
|
||||
@@ -432,6 +489,9 @@ func onInteractionCreate(_ string) func(s *discordgo.Session, i *discordgo.Inter
|
||||
cfgMu.Lock()
|
||||
getCfg(guildID).TimeoutMin = int(minutes)
|
||||
cfgMu.Unlock()
|
||||
if err := saveGuildCfgs(); err != nil {
|
||||
log.Printf("Speichern fehlgeschlagen: %v", err)
|
||||
}
|
||||
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{Content: fmt.Sprintf("✅ Timeout auf %d Minuten gesetzt.", minutes), Flags: discordgo.MessageFlagsEphemeral}})
|
||||
case "adduser":
|
||||
// wer ruft auf?
|
||||
@@ -588,6 +648,11 @@ func main() {
|
||||
log.Fatal("Bitte setze DISCORD_TOKEN")
|
||||
}
|
||||
|
||||
// Persistente Konfiguration laden (optional)
|
||||
if err := loadGuildCfgs(); err != nil {
|
||||
log.Printf("Hinweis: Konnte %s nicht laden (%v). Starte mit Defaults.", configPath, err)
|
||||
}
|
||||
|
||||
s, err := discordgo.New("Bot " + token)
|
||||
if err != nil {
|
||||
log.Fatalf("Session fehlgeschlagen: %v", err)
|
||||
@@ -634,5 +699,8 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := saveGuildCfgs(); err != nil {
|
||||
log.Printf("Speichern zum Shutdown fehlgeschlagen: %v", err)
|
||||
}
|
||||
_ = s.Close()
|
||||
}
|
||||
|
Reference in New Issue
Block a user