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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@@ -19,11 +20,59 @@ const (
|
|||||||
defaultCategory = "Private Räume"
|
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) =====
|
// ===== Per-Guild Config (in-memory) =====
|
||||||
type GuildConfig struct {
|
type GuildConfig struct {
|
||||||
LobbyName string
|
LobbyName string `json:"lobby_name"`
|
||||||
CategoryName string
|
CategoryName string `json:"category_name"`
|
||||||
TimeoutMin int
|
TimeoutMin int `json:"timeout_min"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -223,6 +272,7 @@ func createPrivateVCAndMove(
|
|||||||
}
|
}
|
||||||
|
|
||||||
go watchAndCleanup(s, guildID, newChan.ID, time.Duration(timeoutMin)*time.Minute)
|
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
|
return newChan, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,6 +302,7 @@ func watchAndCleanup(s *discordgo.Session, guildID, channelID string, timeout ti
|
|||||||
}
|
}
|
||||||
if time.Since(lastActive) >= timeout {
|
if time.Since(lastActive) >= timeout {
|
||||||
_, _ = s.ChannelDelete(channelID)
|
_, _ = s.ChannelDelete(channelID)
|
||||||
|
log.Println("➖ Deleted channel for guildID: " + guildID + " with ID: " + channelID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -393,6 +444,9 @@ func onInteractionCreate(_ string) func(s *discordgo.Session, i *discordgo.Inter
|
|||||||
cfgMu.Lock()
|
cfgMu.Lock()
|
||||||
getCfg(guildID).LobbyName = name
|
getCfg(guildID).LobbyName = name
|
||||||
cfgMu.Unlock()
|
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}})
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{Content: "✅ Lobby-Name aktualisiert auf: " + name, Flags: discordgo.MessageFlagsEphemeral}})
|
||||||
|
|
||||||
case "setcategory":
|
case "setcategory":
|
||||||
@@ -413,6 +467,9 @@ func onInteractionCreate(_ string) func(s *discordgo.Session, i *discordgo.Inter
|
|||||||
cfgMu.Lock()
|
cfgMu.Lock()
|
||||||
getCfg(guildID).CategoryName = name
|
getCfg(guildID).CategoryName = name
|
||||||
cfgMu.Unlock()
|
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}})
|
_ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{Content: "✅ Kategorie-Name aktualisiert auf: " + name, Flags: discordgo.MessageFlagsEphemeral}})
|
||||||
|
|
||||||
case "settimeout":
|
case "settimeout":
|
||||||
@@ -432,6 +489,9 @@ func onInteractionCreate(_ string) func(s *discordgo.Session, i *discordgo.Inter
|
|||||||
cfgMu.Lock()
|
cfgMu.Lock()
|
||||||
getCfg(guildID).TimeoutMin = int(minutes)
|
getCfg(guildID).TimeoutMin = int(minutes)
|
||||||
cfgMu.Unlock()
|
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}})
|
_ = 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":
|
case "adduser":
|
||||||
// wer ruft auf?
|
// wer ruft auf?
|
||||||
@@ -588,6 +648,11 @@ func main() {
|
|||||||
log.Fatal("Bitte setze DISCORD_TOKEN")
|
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)
|
s, err := discordgo.New("Bot " + token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Session fehlgeschlagen: %v", err)
|
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()
|
_ = s.Close()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user