feat: announce poll lifecycle events to IRC and Kosmi
Handle poll.start, poll.ending, voting.ended, and poll.ending.cancelled WebSocket messages from the upstream GamePicker API. Broadcasts opening, closing-countdown, and closed announcements with per-bridge bold formatting (IRC control codes vs asterisks for Kosmi). Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -11,16 +11,17 @@ import (
|
||||
|
||||
// Manager handles the Jackbox integration lifecycle
|
||||
type Manager struct {
|
||||
client *Client
|
||||
webhookServer *WebhookServer
|
||||
wsClient *WebSocketClient
|
||||
config config.Config
|
||||
log *logrus.Entry
|
||||
enabled bool
|
||||
useWebSocket bool
|
||||
messageCallback func(string)
|
||||
muted bool
|
||||
mu sync.RWMutex
|
||||
client *Client
|
||||
webhookServer *WebhookServer
|
||||
wsClient *WebSocketClient
|
||||
config config.Config
|
||||
log *logrus.Entry
|
||||
enabled bool
|
||||
useWebSocket bool
|
||||
messageCallback func(string)
|
||||
formattedMessageCallback func(string, string)
|
||||
muted bool
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewManager creates a new Jackbox manager
|
||||
@@ -67,15 +68,15 @@ func (m *Manager) Initialize() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// StartWebhookServer starts the webhook server with the provided message callback
|
||||
func (m *Manager) StartWebhookServer(messageCallback func(string)) error {
|
||||
// StartWebhookServer starts the webhook server with the provided message callbacks
|
||||
func (m *Manager) StartWebhookServer(messageCallback func(string), formattedCallback func(string, string)) error {
|
||||
if !m.enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Use WebSocket if enabled, otherwise fall back to webhook
|
||||
if m.useWebSocket {
|
||||
return m.startWebSocketClient(messageCallback)
|
||||
return m.startWebSocketClient(messageCallback, formattedCallback)
|
||||
}
|
||||
|
||||
webhookPort := m.config.Viper().GetInt("jackbox.WebhookPort")
|
||||
@@ -102,11 +103,12 @@ func (m *Manager) StartWebhookServer(messageCallback func(string)) error {
|
||||
}
|
||||
|
||||
// startWebSocketClient starts the WebSocket client connection
|
||||
func (m *Manager) startWebSocketClient(messageCallback func(string)) error {
|
||||
func (m *Manager) startWebSocketClient(messageCallback func(string), formattedCallback func(string, string)) error {
|
||||
apiURL := m.config.Viper().GetString("jackbox.APIURL")
|
||||
|
||||
// Store the callback for use in monitoring
|
||||
// Store the callbacks for use in monitoring and reconnection
|
||||
m.messageCallback = messageCallback
|
||||
m.formattedMessageCallback = formattedCallback
|
||||
|
||||
// Wrap the callback to check mute status
|
||||
wrappedCallback := func(message string) {
|
||||
@@ -116,6 +118,15 @@ func (m *Manager) startWebSocketClient(messageCallback func(string)) error {
|
||||
}
|
||||
messageCallback(message)
|
||||
}
|
||||
|
||||
// Wrap the formatted callback to check mute status
|
||||
wrappedFormattedCallback := func(ircMsg, plainMsg string) {
|
||||
if m.IsMuted() {
|
||||
m.log.Debugf("Jackbox formatted message suppressed (muted): %s", plainMsg)
|
||||
return
|
||||
}
|
||||
formattedCallback(ircMsg, plainMsg)
|
||||
}
|
||||
|
||||
// Set wrapped callback on client for vote broadcasts
|
||||
m.client.SetMessageCallback(wrappedCallback)
|
||||
@@ -141,7 +152,7 @@ func (m *Manager) startWebSocketClient(messageCallback func(string)) error {
|
||||
m.config.Viper().GetInt("jackbox.RoomCodePlaintextDelay"))
|
||||
|
||||
// Create WebSocket client (pass the API client for vote tracking)
|
||||
m.wsClient = NewWebSocketClient(apiURL, token, wrappedCallback, m.client, enableRoomCodeImage, imageDelay, plaintextDelay, m.log)
|
||||
m.wsClient = NewWebSocketClient(apiURL, token, wrappedCallback, wrappedFormattedCallback, m.client, enableRoomCodeImage, imageDelay, plaintextDelay, m.log)
|
||||
|
||||
// Connect to WebSocket
|
||||
if err := m.wsClient.Connect(); err != nil {
|
||||
@@ -265,12 +276,12 @@ func (m *Manager) Reconnect() error {
|
||||
return fmt.Errorf("re-authentication failed: %w", err)
|
||||
}
|
||||
|
||||
// Rebuild the WebSocket client using the original callback
|
||||
// Rebuild the WebSocket client using the original callbacks
|
||||
if m.messageCallback == nil {
|
||||
return fmt.Errorf("no message callback registered")
|
||||
}
|
||||
|
||||
return m.startWebSocketClient(m.messageCallback)
|
||||
return m.startWebSocketClient(m.messageCallback, m.formattedMessageCallback)
|
||||
}
|
||||
|
||||
// GetClient returns the Jackbox API client (may be nil if disabled)
|
||||
|
||||
Reference in New Issue
Block a user