Added addnext command

This commit is contained in:
Khoi Tran 2015-12-14 20:07:12 -05:00
parent ebfd810099
commit 4e9b30c513
7 changed files with 130 additions and 11 deletions

View file

@ -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`

View file

@ -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) {

View file

@ -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

View file

@ -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

View file

@ -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 {

View file

@ -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]
}
}

View file

@ -99,6 +99,16 @@ const PLAYLIST_ADDED_HTML = `
<b>%s</b> 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 = `
<b>%s</b> 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 = `
<b>%s</b> 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. <b>Skipping song!</b>
@ -123,6 +133,7 @@ const HELP_HTML = `<br/>
<p><b>!currentsong</b> - Shows the title and submitter of the song currently playing.</p>
<p style="-qt-paragraph-type:empty"><br/></p>
<p><b>Admin Commands:</b></p>
<p><b>!addnext</b> - Adds songs/playlists to queue after the current song.</p>
<p><b>!reset</b> - An admin command that resets the song queue. </p>
<p><b>!forceskip</b> - An admin command that forces a song skip. </p>
<p><b>!forceskipplaylist</b> - An admin command that forces a playlist skip. </p>