Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
ref: play handler
Browse files Browse the repository at this point in the history
  • Loading branch information
keshon committed Mar 1, 2024
1 parent 8afe796 commit be7aa25
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions mod-music/discord/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ func (d *Discord) handlePlayCommand(s *discordgo.Session, m *discordgo.MessageCr
return
}

channel, _ := s.State.Channel(m.Message.ChannelID)
guild, _ := s.State.Guild(channel.GuildID)
channel, err := s.State.Channel(m.Message.ChannelID)
if err != nil {
slog.Error("Error getting channel: %v", err)
}

guild, err := s.State.Guild(channel.GuildID)
if err != nil {
slog.Error("Error getting guild: %v", err)
}

if len(guild.VoiceStates) == 0 {
embedStr = "You are not in a voice channel, please join one first."
Expand All @@ -66,7 +73,7 @@ func (d *Discord) handlePlayCommand(s *discordgo.Session, m *discordgo.MessageCr
return
}

songs, err := fetchSongs(originType, origins, d, m)
songs, err := fetchSongsToList(originType, origins, d, m)
if err != nil {
embedStr = fmt.Sprintf("%v\n\n*details:*\n`%v`", "Error forming playlist", err)
embedMsg = embed.NewEmbed().
Expand All @@ -80,17 +87,6 @@ func (d *Discord) handlePlayCommand(s *discordgo.Session, m *discordgo.MessageCr
return
}

if len(songs) == 0 {
embedStr = "There are no songs in the playlist."
embedMsg = embed.NewEmbed().
SetColor(0x9f00d4).
SetDescription(embedStr).
SetColor(0x9f00d4).MessageEmbed

s.ChannelMessageEditEmbed(m.Message.ChannelID, pleaseWaitMessage.ID, embedMsg)
return
}

// Enqueue playlist to the player
err = playOrEnqueue(d, songs, s, m, enqueueOnly, pleaseWaitMessage.ID)
if err != nil {
Expand All @@ -107,8 +103,8 @@ func (d *Discord) handlePlayCommand(s *discordgo.Session, m *discordgo.MessageCr
}
}

func fetchSongs(originType string, songsOrigins []string, d *Discord, m *discordgo.MessageCreate) ([]*player.Song, error) {
var playlist []*player.Song
func fetchSongsToList(originType string, songsOrigins []string, d *Discord, m *discordgo.MessageCreate) ([]*player.Song, error) {
var songsList []*player.Song

youtube := sources.NewYoutube()
stream := sources.NewStream()
Expand Down Expand Up @@ -155,15 +151,19 @@ func fetchSongs(originType string, songsOrigins []string, d *Discord, m *discord
}
}

playlist = append(playlist, songs...)
songsList = append(songsList, songs...)
}

slog.Info("Up-to-date playlist now:")
for _, song := range playlist {
if len(songsList) == 0 {
return nil, errors.New("no songs were fetched to playlist")
}

slog.Info("Up-to-date songs list now is:")
for _, song := range songsList {
slog.Info(" - ", song.Title, song.Source)
}

return playlist, nil
return songsList, nil
}

func playOrEnqueue(d *Discord, playlist []*player.Song, s *discordgo.Session, m *discordgo.MessageCreate, enqueueOnly bool, prevMessageID string) (err error) {
Expand Down

0 comments on commit be7aa25

Please sign in to comment.