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

Commit

Permalink
fix: db duplicated issue
Browse files Browse the repository at this point in the history
  • Loading branch information
keshon committed Apr 11, 2024
1 parent 95905a9 commit 67c2f77
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion mod-music/discord/cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (d *Discord) handleCacheListCommand(s *discordgo.Session, m *discordgo.Mess
// Iterate over the files and append their names and IDs to the buffer
for _, file := range files {
// Append file name and ID to the buffer
fileList.WriteString(fmt.Sprintf("- `%s`\n", file.Name()))
fileList.WriteString(fmt.Sprintf("`%s`\n", file.Name()))
}

// Send the list of cached files as a message
Expand Down
12 changes: 11 additions & 1 deletion mod-music/discord/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func getSongsFromSources(originType string, songsOrigins []string, guildID strin
song = &player.Song{
SongID: existingTrack.SongID,
Title: existingTrack.Title,
URL: existingTrack.URL,
Filepath: existingTrack.Filepath,
}
} else {
Expand Down Expand Up @@ -190,6 +191,7 @@ func getSongsFromSources(originType string, songsOrigins []string, guildID strin
if track.Source == "LocalFile" {
slog.Info("Track is from LocalFile")
song = []*player.Song{{
SongID: track.SongID,
Title: track.Title,
URL: track.URL,
Filepath: track.Filepath,
Expand Down Expand Up @@ -318,8 +320,16 @@ func showStatusMessage(d *Discord, s *discordgo.Session, channelID, prevMessageI

// Display current song information
if currentSong := d.Player.GetCurrentSong(); currentSong != nil {
var sourceLabels string
if len(currentSong.URL) > 0 {
content += fmt.Sprintf("\n**`%v`**\n[%v](%v)\n\n", strings.ToLower(currentSong.Source.String()), currentSong.Title, currentSong.URL)

if currentSong.Source == player.SourceLocalFile && utils.IsYouTubeURL(currentSong.URL) {
sourceLabels = "`youtube cached`"
} else {
sourceLabels = "`" + strings.ToLower(currentSong.Source.String()) + "`"
}

content += fmt.Sprintf("\n**`%v`**\n[%v](%v)\n\n", sourceLabels, currentSong.Title, currentSong.URL)
} else {
content += fmt.Sprintf("\n**`%v`**\n%v\n\n", strings.ToLower(currentSong.Source.String()), currentSong.Title)
}
Expand Down
15 changes: 3 additions & 12 deletions mod-music/player/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,19 @@ func (p *Player) Play(startAt int, song *Song) error {
p.SetStreamingSession(stream)
p.SetCurrentStatus(StatusPlaying)

// Create song ID
var songID string
slog.Fatal(p.GetCurrentSong().SongID)
if len(p.GetCurrentSong().SongID) > 0 {
songID = p.GetCurrentSong().SongID
} else {
songID = GetMD5Hash(p.GetCurrentSong().Title)
}

// Add song to history
historySong := &history.Song{
Title: p.GetCurrentSong().Title,
URL: p.GetCurrentSong().URL,
Filepath: p.GetCurrentSong().Filepath,
Duration: p.GetCurrentSong().Duration,
SongID: songID,
SongID: p.GetCurrentSong().SongID,
Thumbnail: history.Thumbnail(p.GetCurrentSong().Thumbnail),
Source: p.GetCurrentSong().Source.String(),
}
p.GetHistory().AddTrackToHistory(p.GetVoiceConnection().GuildID, historySong)

if err := p.GetHistory().AddPlaybackCountStats(p.GetVoiceConnection().GuildID, songID); err != nil {
if err := p.GetHistory().AddPlaybackCountStats(p.GetVoiceConnection().GuildID, p.GetCurrentSong().SongID); err != nil {
slog.Errorf("error adding playback count stats to history: %v", err)
}

Expand All @@ -135,7 +126,7 @@ func (p *Player) Play(startAt int, song *Song) error {
select {
case <-ticker.C:
if p.GetVoiceConnection() != nil && p.GetStreamingSession() != nil && p.GetCurrentSong() != nil && !p.GetStreamingSession().Paused() {
err := p.GetHistory().AddPlaybackDurationStats(p.GetVoiceConnection().GuildID, songID, float64(interval.Seconds()))
err := p.GetHistory().AddPlaybackDurationStats(p.GetVoiceConnection().GuildID, p.GetCurrentSong().SongID, float64(interval.Seconds()))
if err != nil {
slog.Warnf("Error adding playback duration stats to history: %v", err)
}
Expand Down

0 comments on commit 67c2f77

Please sign in to comment.