87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
const defaultFile = "bot_config.json"
|
|
|
|
// Config holds bot configuration settings
|
|
type Config struct {
|
|
SummaryTime string `json:"summary_time"` // HH:MM format
|
|
SummaryChannel string `json:"summary_channel"` // Channel ID
|
|
Timezone string `json:"timezone"`
|
|
}
|
|
|
|
// Manager handles configuration persistence
|
|
type Manager struct {
|
|
config Config
|
|
filename string
|
|
}
|
|
|
|
// New creates a new config manager
|
|
func New(filename string) (*Manager, error) {
|
|
if filename == "" {
|
|
filename = defaultFile
|
|
}
|
|
|
|
cm := &Manager{
|
|
filename: filename,
|
|
config: Config{
|
|
SummaryTime: "09:00",
|
|
SummaryChannel: "",
|
|
Timezone: "America/New_York",
|
|
},
|
|
}
|
|
|
|
if err := cm.load(); err != nil {
|
|
if !os.IsNotExist(err) {
|
|
return nil, fmt.Errorf("failed to load config: %w", err)
|
|
}
|
|
}
|
|
|
|
return cm, nil
|
|
}
|
|
|
|
func (cm *Manager) load() error {
|
|
data, err := os.ReadFile(cm.filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return json.Unmarshal(data, &cm.config)
|
|
}
|
|
|
|
func (cm *Manager) save() error {
|
|
data, err := json.MarshalIndent(cm.config, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal config: %w", err)
|
|
}
|
|
|
|
return os.WriteFile(cm.filename, data, 0644)
|
|
}
|
|
|
|
// SetSummaryTime sets the time for daily summaries
|
|
func (cm *Manager) SetSummaryTime(timeStr string) error {
|
|
if _, err := time.Parse("15:04", timeStr); err != nil {
|
|
return fmt.Errorf("invalid time format, use HH:MM: %w", err)
|
|
}
|
|
|
|
cm.config.SummaryTime = timeStr
|
|
return cm.save()
|
|
}
|
|
|
|
// SetSummaryChannel sets the channel for daily summaries
|
|
func (cm *Manager) SetSummaryChannel(channelID string) error {
|
|
cm.config.SummaryChannel = channelID
|
|
return cm.save()
|
|
}
|
|
|
|
// Get returns the current configuration
|
|
func (cm *Manager) Get() Config {
|
|
return cm.config
|
|
}
|