From 4e9b30c5134e52a9c6a145291b2d26295dcfbe90 Mon Sep 17 00:00:00 2001 From: Khoi Tran Date: Mon, 14 Dec 2015 20:07:12 -0500 Subject: [PATCH] Added addnext command --- README.md | 1 + commands.go | 26 +++++++++++++++++++++ config.gcfg | 8 +++++++ parseconfig.go | 2 ++ service.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++- songqueue.go | 30 ++++++++++++++++-------- strings.go | 11 +++++++++ 7 files changed, 130 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 10c2349..b23b347 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ These are all of the chat commands currently supported by MumbleDJ. All command Command | Description | Arguments | Admin | Example --------|-------------|-----------|-------|-------- **add** | Adds audio from a url to the song queue. If no songs are currently in the queue, the audio will begin playing immediately. Playlists may also be added using this command. Please note, however, that if a YouTube playlist contains over 25 videos only the first 25 videos will be placed in the song queue. | youtube_video_url OR youtube_playlist_url OR soundcloud_track_url OR soundcloud_playlist_url | No | `!add https://www.youtube.com/watch?v=5xfEr2Oxdys` +**add** | Adds audio from a url to the song queue after the current song. If no songs are currently in the queue, the audio will begin playing immediately. Playlists may also be added using this command. Please note, however, that if a YouTube playlist contains over 25 videos only the first 25 videos will be placed in the song queue. | youtube_video_url OR youtube_playlist_url OR soundcloud_track_url OR soundcloud_playlist_url | Yes | `!addnext https://www.youtube.com/watch?v=5xfEr2Oxdys` **skip**| Submits a vote to skip the current song. Once the skip ratio target (specified in `mumbledj.gcfg`) is met, the song will be skipped and the next will start playing. Each user may only submit one skip per song. | None | No | `!skip` **skipplaylist** | Submits a vote to skip the current playlist. Once the skip ratio target (specified in mumbledj.gcfg) is met, the playlist will be skipped and the next song/playlist will start playing. Each user may only submit one skip per playlist. | None | No | `!skipplaylist` **forceskip** | An admin command that forces a song skip. | None | Yes | `!forceskip` diff --git a/commands.go b/commands.go index 488f17e..8798657 100644 --- a/commands.go +++ b/commands.go @@ -41,6 +41,13 @@ func parseCommand(user *gumble.User, username, command string) { } else { dj.SendPrivateMessage(user, NO_PERMISSION_MSG) } + // Addnext command + case dj.conf.Aliases.AddNextAlias: + if dj.HasPermission(username, dj.conf.Permissions.AdminAddNext) { + addNext(user, argument) + } else { + dj.SendPrivateMessage(user, NO_PERMISSION_MSG) + } // Skip command case dj.conf.Aliases.SkipAlias: if dj.HasPermission(username, dj.conf.Permissions.AdminSkip) { @@ -205,6 +212,25 @@ func add(user *gumble.User, url string) error { } } +// addnext performs !addnext functionality. Checks input URL for service, and adds +// the URL to the queue as the next song if the format matches. +func addNext(user *gumble.User, url string) error { + if !dj.audioStream.IsPlaying() { + return add(user, url) + } else { + if url == "" { + dj.SendPrivateMessage(user, NO_ARGUMENT_MSG) + return errors.New("NO_ARGUMENT") + } else { + err := FindServiceAndInsertNext(user, url) + if err != nil { + dj.SendPrivateMessage(user, err.Error()) + } + return err + } + } +} + // skip performs !skip functionality. Adds a skip to the skippers slice for the current song, and then // evaluates if a skip should be performed. Both skip and forceskip are implemented here. func skip(user *gumble.User, admin, playlistSkip bool) { diff --git a/config.gcfg b/config.gcfg index 9ac089b..8cbb259 100644 --- a/config.gcfg +++ b/config.gcfg @@ -70,6 +70,10 @@ HighestVolume = 0.8 # DEFAULT VALUE: "add" AddAlias = "add" +# Alias used for addnext command +# DEFAULT VALUE: "addnext" +AddNextAlias = "addnext" + # Alias used for skip command # DEFAULT VALUE: "skip" SkipAlias = "skip" @@ -169,6 +173,10 @@ Admins = "Matt" # DEFAULT VALUE: false AdminAdd = false +# Make addnext an admin command? +# DEFAULT VALUE: true +AdminAddNext = true + # Make playlist adds an admin only action? # DEFAULT VALUE: false AdminAddPlaylists = false diff --git a/parseconfig.go b/parseconfig.go index 715fed3..7dbfddd 100644 --- a/parseconfig.go +++ b/parseconfig.go @@ -37,6 +37,7 @@ type DjConfig struct { } Aliases struct { AddAlias string + AddNextAlias string SkipAlias string SkipPlaylistAlias string AdminSkipAlias string @@ -62,6 +63,7 @@ type DjConfig struct { AdminsEnabled bool Admins []string AdminAdd bool + AdminAddNext bool AdminAddPlaylists bool AdminSkip bool AdminHelp bool diff --git a/service.go b/service.go index 245947e..02f9da5 100644 --- a/service.go +++ b/service.go @@ -115,7 +115,7 @@ func FindServiceAndAdd(user *gumble.User, url string) error { // Starts playing the new song if nothing else is playing if oldLength == 0 && dj.queue.Len() != 0 && !dj.audioStream.IsPlaying() { - if (dj.conf.General.AutomaticShuffleOn){ + if dj.conf.General.AutomaticShuffleOn { dj.queue.RandomNextSong(true) } if err := dj.queue.CurrentSong().Download(); err == nil { @@ -130,6 +130,67 @@ func FindServiceAndAdd(user *gumble.User, url string) error { } } +// FindServiceAndInsertNext tries the given url with each service +// and inserts the song/playlist with the correct service into the slot after the current one +func FindServiceAndInsertNext(user *gumble.User, url string) error { + var urlService Service + + // Checks all services to see if any can take the URL + for _, service := range services { + if service.URLRegex(url) { + urlService = service + } + } + + if urlService == nil { + return errors.New(INVALID_URL_MSG) + } else { + var title string + var songsAdded = 0 + var songArray []Song + var err error + + // Get service to create songs + if songArray, err = urlService.NewRequest(user, url); err != nil { + return err + } + + // Check Playlist Permission + if len(songArray) > 1 && !dj.HasPermission(user.Name, dj.conf.Permissions.AdminAddPlaylists) { + return errors.New(NO_PLAYLIST_PERMISSION_MSG) + } + + // Loop through all songs and add to the queue + i := 0 + for _, song := range songArray { + i++ + // Check song is not too long + if dj.conf.General.MaxSongDuration == 0 || int(song.Duration().Seconds()) <= dj.conf.General.MaxSongDuration { + if !isNil(song.Playlist()) { + title = song.Playlist().Title() + } else { + title = song.Title() + } + + // Add song to queue + dj.queue.InsertSong(song, i) + songsAdded++ + } + } + + // Alert channel of added song/playlist + if songsAdded == 0 { + return errors.New(fmt.Sprintf(TRACK_TOO_LONG_MSG, urlService.ServiceName())) + } else if songsAdded == 1 { + dj.client.Self.Channel.Send(fmt.Sprintf(NEXT_SONG_ADDED_HTML, user.Name, title), false) + } else { + dj.client.Self.Channel.Send(fmt.Sprintf(NEXT_PLAYLIST_ADDED_HTML, user.Name, title), false) + } + + return nil + } +} + // RegexpFromURL loops through an array of patterns to see if it matches the url func RegexpFromURL(url string, patterns []string) *regexp.Regexp { for _, pattern := range patterns { diff --git a/songqueue.go b/songqueue.go index a00e08d..3fa0539 100644 --- a/songqueue.go +++ b/songqueue.go @@ -10,12 +10,12 @@ package main import ( "errors" "fmt" - "time" "math/rand" + "time" ) func init() { - rand.Seed(time.Now().UTC().UnixNano()) + rand.Seed(time.Now().UTC().UnixNano()) } // SongQueue type declaration. @@ -40,6 +40,16 @@ func (q *SongQueue) AddSong(s Song) error { return errors.New("Could not add Song to the SongQueue.") } +// InsertSong inserts a Song to the SongQueue at a location. +func (q *SongQueue) InsertSong(s Song, i int) error { + beforeLen := q.Len() + q.queue = append(q.queue[:i], append([]Song{s}, q.queue[i:]...)...) + if len(q.queue) == beforeLen+1 { + return nil + } + return errors.New("Could not insert Song to the SongQueue.") +} + // CurrentSong returns the current Song. func (q *SongQueue) CurrentSong() Song { return q.queue[0] @@ -64,7 +74,7 @@ func (q *SongQueue) NextSong() { // PeekNext peeks at the next Song and returns it. func (q *SongQueue) PeekNext() (Song, error) { if q.Len() > 1 { - if dj.conf.General.AutomaticShuffleOn{ //Shuffle mode is active + if dj.conf.General.AutomaticShuffleOn { //Shuffle mode is active q.RandomNextSong(false) } return q.queue[1], nil @@ -115,21 +125,21 @@ func (q *SongQueue) PrepareAndPlayNextSong() { // Shuffles the songqueue using inside-out algorithm func (q *SongQueue) ShuffleSongs() { - for i := range q.queue[1:] { //Don't touch currently playing song - j := rand.Intn(i + 1) - q.queue[i + 1], q.queue[j + 1] = q.queue[j + 1], q.queue[i + 1] + for i := range q.queue[1:] { //Don't touch currently playing song + j := rand.Intn(i + 1) + q.queue[i+1], q.queue[j+1] = q.queue[j+1], q.queue[i+1] } } // Sets a random song as next song to be played // queueWasEmpty wether the queue was empty before adding the last song -func (q *SongQueue) RandomNextSong(queueWasEmpty bool){ - if (q.Len() > 1){ +func (q *SongQueue) RandomNextSong(queueWasEmpty bool) { + if q.Len() > 1 { nextSongIndex := 1 - if queueWasEmpty{ + if queueWasEmpty { nextSongIndex = 0 } - swapIndex := nextSongIndex + rand.Intn(q.Len() - 1) + swapIndex := nextSongIndex + rand.Intn(q.Len()-1) q.queue[nextSongIndex], q.queue[swapIndex] = q.queue[swapIndex], q.queue[nextSongIndex] } } diff --git a/strings.go b/strings.go index 12e7f0d..de0aad7 100644 --- a/strings.go +++ b/strings.go @@ -99,6 +99,16 @@ const PLAYLIST_ADDED_HTML = ` %s has added the playlist "%s" to the queue. ` +// Message shown to channel when a song is added to the queue by a user after the current song. +const NEXT_SONG_ADDED_HTML = ` + %s has added "%s" to the queue after the current song. +` + +// Message shown to channel when a playlist is added to the queue by a user after the current song. +const NEXT_PLAYLIST_ADDED_HTML = ` + %s has added the playlist "%s" to the queue after the current song. +` + // Message shown to channel when a song has been skipped. const SONG_SKIPPED_HTML = ` The number of votes required for a skip has been met. Skipping song! @@ -123,6 +133,7 @@ const HELP_HTML = `

!currentsong - Shows the title and submitter of the song currently playing.


Admin Commands:

+

!addnext - Adds songs/playlists to queue after the current song.

!reset - An admin command that resets the song queue.

!forceskip - An admin command that forces a song skip.

!forceskipplaylist - An admin command that forces a playlist skip.