feat: show tne current playing station in the bot status

This commit is contained in:
Felipe M 2025-05-08 11:18:38 +02:00
parent 9dafbe26c7
commit 96c8e2b797
Signed by: fmartingr
GPG key ID: CCFBC5637D4000A8
2 changed files with 63 additions and 3 deletions

View file

@ -8,6 +8,13 @@ A Discord bot that streams radio stations to your voice channel.
- FFmpeg installed on your system
- Discord Bot Token
## Features
- Streams radio stations to Discord voice channels
- Slash command interface for easy control
- Support for multiple radio stations
- Container-ready with provided Containerfile
## Setup
1. Install FFmpeg on your system:

59
main.go
View file

@ -38,9 +38,10 @@ var (
Description: "A tope con la",
},
}
logger *slog.Logger
debug bool
guildID string // For testing in a specific guild
logger *slog.Logger
debug bool
guildID string // For testing in a specific guild
currentStation string // Track current playing station
)
func init() {
@ -351,6 +352,18 @@ func handleRadioCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
guildID := i.GuildID
logger = logger.With("guild_id", guildID)
// Check if we're already connected to a voice channel in this guild
existingVC, err := findExistingVoiceConnection(ctx, s, guildID)
if err == nil && existingVC != nil {
// We're already connected, disconnect first
logger.Info("Already connected to voice channel, disconnecting first")
if err := existingVC.Disconnect(); err != nil {
logger.Error("Error disconnecting from voice channel", "error", err)
}
// Give Discord a moment to process the disconnection
time.Sleep(1 * time.Second)
}
// Find the voice channel the user is in
vs, err := findUserVoiceState(ctx, s, guildID, i.Member.User.ID)
if err != nil {
@ -379,6 +392,10 @@ func handleRadioCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Wait for connection to be ready
time.Sleep(2 * time.Second)
// Update bot status to show the current station
currentStation = stationName
updateBotStatus(s, stationName)
// Update message
followupMsg(s, i, fmt.Sprintf("¡Ahora transmitiendo la radio %s en tu canal de voz!", station.Name))
@ -397,6 +414,10 @@ func handleRadioCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
if err != nil {
logger.Error("Audio streaming error", "error", err)
followupMsg(s, i, "La transmisión de audio ha terminado inesperadamente. Por favor, inténtalo más tarde.")
// Clear status if streaming ends with error
currentStation = ""
updateBotStatus(s, "")
}
}()
@ -413,10 +434,42 @@ func handleRadioCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
if vc.Disconnect() != nil {
logger.Error("Error disconnecting from voice channel", "error", err)
}
// Clear the bot status
currentStation = ""
updateBotStatus(s, "")
followupMsg(s, i, "Desconectado del canal de voz.")
}
}
// updateBotStatus updates the bot's status to show the current station
func updateBotStatus(s *discordgo.Session, stationName string) {
var status string
if stationName != "" {
station, ok := stations[stationName]
if ok {
status = station.Name
}
}
err := s.UpdateStatusComplex(discordgo.UpdateStatusData{
Status: "online",
Activities: []*discordgo.Activity{
{
Name: status,
Type: discordgo.ActivityTypeListening,
},
},
})
if err != nil {
logger.Error("Error updating bot status", "error", err, "station", stationName)
} else {
logger.Info("Bot status updated", "status", status)
}
}
// Helper function to send followup messages
func followupMsg(s *discordgo.Session, i *discordgo.InteractionCreate, content string) {
_, err := s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{