TTW_Bot_GO/internal/notifications/manager.go

66 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package notifications
import (
"stream-bot/internal/audio" // вместо player
"stream-bot/internal/db"
"sync"
)
type Manager struct {
mu sync.RWMutex
settings map[string]*db.NotificationSetting
}
func NewManager() (*Manager, error) {
m := &Manager{
settings: make(map[string]*db.NotificationSetting),
}
if err := m.load(); err != nil {
return nil, err
}
return m, nil
}
func (m *Manager) load() error {
settings, err := db.GetAllNotificationSettings()
if err != nil {
return err
}
m.mu.Lock()
defer m.mu.Unlock()
for _, s := range settings {
m.settings[s.EventName] = &s
}
return nil
}
func (m *Manager) PlayEvent(eventName string) error {
m.mu.RLock()
setting, ok := m.settings[eventName]
m.mu.RUnlock()
if !ok || !setting.Enabled || setting.SoundFile == "" {
return nil
}
// Используем audio.PlayWithVolume с громкостью из настроек
return audio.PlayWithVolume(setting.SoundFile, setting.Volume)
}
func (m *Manager) UpdateSetting(ns *db.NotificationSetting) error {
if err := db.SaveNotificationSetting(ns); err != nil {
return err
}
m.mu.Lock()
m.settings[ns.EventName] = ns
m.mu.Unlock()
return nil
}
func (m *Manager) GetAll() []db.NotificationSetting {
m.mu.RLock()
defer m.mu.RUnlock()
result := make([]db.NotificationSetting, 0, len(m.settings))
for _, v := range m.settings {
result = append(result, *v)
}
return result
}