Skip to content

Commit

Permalink
Display originating playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
depado committed Nov 8, 2020
1 parent 282a0e9 commit c4e59b5
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/
conf.yml
conf.*yml
fox
dockervol/
10 changes: 6 additions & 4 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ func (b *BotInstance) SalvageVoice() error {
}
}

voice, err := b.Session.ChannelVoiceJoin(b.conf.Bot.Guild, b.conf.Bot.Channels.Voice, false, true)
if err != nil {
return fmt.Errorf("unable to establish connection to vocal channel: %w", err)
if b.Session != nil {
voice, err := b.Session.ChannelVoiceJoin(b.conf.Bot.Guild, b.conf.Bot.Channels.Voice, false, true)
if err != nil {
return fmt.Errorf("unable to establish connection to vocal channel: %w", err)
}
b.Voice = voice
}
b.Voice = voice
return nil
}
8 changes: 8 additions & 0 deletions bot/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func (b *BotInstance) TrackEmbed(t soundcloud.Track, queue bool) *discordgo.Mess
},
}

if t.Playlist != nil {
e.Fields = append(e.Fields, &discordgo.MessageEmbedField{
Name: "In Playlist",
Value: fmt.Sprintf("[%s](%s) by [%s](%s)", t.Playlist.Title, t.Playlist.PermalinkURL, t.Playlist.User.Username, t.Playlist.User.PermalinkURL),
Inline: false,
})
}

if queue {
e.Footer = &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf(
Expand Down
2 changes: 2 additions & 0 deletions bot/handlers_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (b *BotInstance) JoinHandler(m *discordgo.MessageCreate) {
return
}
b.Voice = voice
b.SendNotice("", fmt.Sprintf("➡️ Joined voice channel as instructed by <@%s>", m.Author.ID), "", m.ChannelID)
b.log.Debug().Str("user", m.Author.Username).Str("method", "join").Msg("bot joined vocal channel")
}
}
Expand All @@ -128,6 +129,7 @@ func (b *BotInstance) LeaveHandler(m *discordgo.MessageCreate) {
}
b.Voice = nil
b.log.Debug().Str("user", m.Author.Username).Str("method", "leave").Msg("bot left vocal channel")
b.SendNotice("", fmt.Sprintf("⬅️ Left voice channel as instructed by <@%s>", m.Author.ID), "", m.ChannelID)
return
}
}
Expand Down
10 changes: 8 additions & 2 deletions bot/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func (b *BotInstance) PlayQueue() {
defer func() {
b.Player.audioM.Unlock()
b.Player.playing = false
b.Session.UpdateStatus(0, "") // nolint:errcheck
}()
for {
tracklen := b.Player.QueueSize()
Expand All @@ -29,13 +28,15 @@ func (b *BotInstance) PlayQueue() {
b.SendPublicMessage("Nothing left to play!", fmt.Sprintf("You can give me more by using the %s command!", b.conf.Bot.Prefix), "")
return
}

b.log.Debug().Msg("getting track")
t := b.Player.Get()
if t == nil {
b.Player.Pop()
b.log.Error().Msg("queue isn't empty but a nil track was returned")
continue
}

b.log.Debug().Msg("track was found, fetching URL")
ts, _, _ := b.Soundcloud.Track().FromTrack(t, false)
url, err := ts.Stream(soundcloud.Opus)
Expand All @@ -47,10 +48,12 @@ func (b *BotInstance) PlayQueue() {
continue
}
}

b.Player.playing = true
b.Player.stop = false
b.SendNowPlaying(*t)
b.Session.UpdateStatus(0, fmt.Sprintf("%s - %s", t.Title, t.User.Username)) // nolint:errcheck
b.Session.UpdateListeningStatus(fmt.Sprintf("%s - %s", t.Title, t.User.Username)) // nolint:errcheck

if err := b.Play(url); err != nil {
b.log.Err(err).Msg("unable to play")
continue
Expand All @@ -64,6 +67,9 @@ func (b *BotInstance) PlayQueue() {
}

func (b *BotInstance) Play(url string) error {
if b.Voice == nil {
return fmt.Errorf("no voice connection found")
}
if err := b.Voice.Speaking(true); err != nil {
return fmt.Errorf("failed setting voice to speaking: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion bot/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (b *BotInstance) AddToQueue(m *discordgo.MessageCreate, url string, next bo
URL: pl.User.PermalinkURL,
},
Fields: []*discordgo.MessageEmbedField{
{Name: "Added by", Value: fmt.Sprintf("<@%s>", m.Author.ID), Inline: false},
{Name: "Added by", Value: fmt.Sprintf("<@%s>", m.Author.ID), Inline: true},
{Name: "Tracks", Value: strconv.Itoa(len(pl.Tracks)), Inline: true},
{Name: "Duration", Value: durafmt.Parse(time.Duration(pl.Duration) * time.Millisecond).LimitFirstN(2).String(), Inline: true},
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/Depado/fox
go 1.15

require (
github.com/Depado/soundcloud v0.1.0
github.com/Depado/soundcloud v0.1.1
github.com/bwmarrin/discordgo v0.22.0
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Depado/soundcloud v0.1.0 h1:gGx5hq90N05Hr1S++mxHsx4dJifqKki5EhIKuJX8e90=
github.com/Depado/soundcloud v0.1.0/go.mod h1:I1Woxhnr/3k0taYYkALq6iX1omE95eF9831CYZbz5Ic=
github.com/Depado/soundcloud v0.1.1 h1:ZBzupSIVX2Cu8q810jvZG8Lz31vubrfoL/CesCm3XgQ=
github.com/Depado/soundcloud v0.1.1/go.mod h1:I1Woxhnr/3k0taYYkALq6iX1omE95eF9831CYZbz5Ic=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down

0 comments on commit c4e59b5

Please sign in to comment.