From 158618024626407c93745c670f82b4e6e54b026f Mon Sep 17 00:00:00 2001 From: Matthieu Grieger Date: Mon, 5 Jan 2015 19:16:59 -0800 Subject: [PATCH] Added reset command --- README.md | 3 ++ commands.go | 45 ++++++++++++++++++++++---- mumbledj.gcfg | 8 +++++ parseconfig.go | 2 ++ songqueue.go | 86 ++++++++++++++++++++++++++------------------------ strings.go | 5 +++ 6 files changed, 101 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 9cba952..ad97741 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,9 @@ Moves MumbleDJ into `channel` if it exists. ####`!reload` Reloads `mumbledj.gcfg` to retrieve updated configuration settings. +####`!reset` +Resets the song queue. + ####`!kill` Safely cleans the bot environment and disconnects from the server. Please use this command to stop the bot instead of force closing, as the kill command deletes any remaining songs in the `~/.mumbledj/songs` directory. diff --git a/commands.go b/commands.go index acfb13d..75e334a 100644 --- a/commands.go +++ b/commands.go @@ -88,6 +88,13 @@ func parseCommand(user *gumble.User, username, command string) { } else { user.Send(NO_PERMISSION_MSG) } + // Reset command + case dj.conf.Aliases.ResetAlias: + if dj.HasPermission(username, dj.conf.Permissions.AdminReset) { + reset(username) + } else { + user.Send(NO_PERMISSION_MSG) + } // Kill command case dj.conf.Aliases.KillAlias: if dj.HasPermission(username, dj.conf.Permissions.AdminKill) { @@ -262,16 +269,26 @@ func reload(user *gumble.User) { } } +// Performs reset functionality. Clears the song queue, stops playing audio, and deletes all +// remaining songs in the ~/.mumbledj/songs directory. +func reset(username string) { + dj.queue.queue = dj.queue.queue[:0] + if err := dj.audioStream.Stop(); err == nil { + if err := deleteSongs(); err == nil { + dj.client.Self().Channel().Send(fmt.Sprintf(QUEUE_RESET_HTML, username), false) + } else { + panic(err) + } + } else { + panic(err) + } +} + // Performs kill functionality. First cleans the ~/.mumbledj/songs directory to get rid of any // excess m4a files. The bot then safely disconnects from the server. func kill() { - songsDir := fmt.Sprintf("%s/.mumbledj/songs", dj.homeDir) - if err := os.RemoveAll(songsDir); err != nil { - panic(errors.New("An error occurred while deleting the audio files.")) - } else { - if err := os.Mkdir(songsDir, 0777); err != nil { - panic(errors.New("An error occurred while recreating the songs directory.")) - } + if err := deleteSongs(); err != nil { + panic(err) } if err := dj.client.Disconnect(); err == nil { fmt.Println("Kill successful. Goodbye!") @@ -280,3 +297,17 @@ func kill() { panic(errors.New("An error occurred while disconnecting from the server.")) } } + +// Deletes songs from ~/.mumbledj/songs. +func deleteSongs() error { + songsDir := fmt.Sprintf("%s/.mumbledj/songs", dj.homeDir) + if err := os.RemoveAll(songsDir); err != nil { + return errors.New("An error occurred while deleting the audio files.") + } else { + if err := os.Mkdir(songsDir, 0777); err != nil { + return errors.New("An error occurred while recreating the songs directory.") + } + return nil + } + return nil +} diff --git a/mumbledj.gcfg b/mumbledj.gcfg index 140cc35..7db62e4 100644 --- a/mumbledj.gcfg +++ b/mumbledj.gcfg @@ -63,6 +63,10 @@ MoveAlias = "move" # DEFAULT VALUE: "reload" ReloadAlias = "reload" +# Alias used for queue reset command +# DEFAULT VALUE: "reset" +ResetAlias = "reset" + # Alias used for kill command # DEFAULT VALUE: "kill" KillAlias = "kill" @@ -107,6 +111,10 @@ AdminMove = true # DEFAULT VALUE: true AdminReload = true +# Make reset an admin command? +# DEFAULT VALUE: true +AdminReset = true + # Make kill an admin command? # DEFAULT VALUE: true (I recommend never changing this to false) AdminKill = true diff --git a/parseconfig.go b/parseconfig.go index b991765..dcd407d 100644 --- a/parseconfig.go +++ b/parseconfig.go @@ -34,6 +34,7 @@ type DjConfig struct { VolumeAlias string MoveAlias string ReloadAlias string + ResetAlias string KillAlias string } Permissions struct { @@ -45,6 +46,7 @@ type DjConfig struct { AdminVolume bool AdminMove bool AdminReload bool + AdminReset bool AdminKill bool } } diff --git a/songqueue.go b/songqueue.go index fbef615..bce90e2 100644 --- a/songqueue.go +++ b/songqueue.go @@ -60,61 +60,65 @@ func (q *SongQueue) Len() int { // OnItemFinished event. Deletes item that just finished playing, then queues the next item. func (q *SongQueue) OnItemFinished() { - if q.CurrentItem().ItemType() == "playlist" { - if err := q.CurrentItem().(*Playlist).songs.CurrentItem().(*Song).Delete(); err == nil { - if q.CurrentItem().(*Playlist).skipped == true { + if q.Len() != 0 { + if q.CurrentItem().ItemType() == "playlist" { + if err := q.CurrentItem().(*Playlist).songs.CurrentItem().(*Song).Delete(); err == nil { + if q.CurrentItem().(*Playlist).skipped == true { + if q.Len() > 1 { + q.NextItem() + q.PrepareAndPlayNextItem() + } else { + q.queue = q.queue[:0] + } + } else if q.CurrentItem().(*Playlist).songs.Len() > 1 { + q.CurrentItem().(*Playlist).songs.NextItem() + q.PrepareAndPlayNextItem() + } else { + if q.Len() > 1 { + q.NextItem() + q.PrepareAndPlayNextItem() + } else { + q.queue = q.queue[:0] + } + } + } else { + panic(err) + } + } else { + if err := q.CurrentItem().(*Song).Delete(); err == nil { if q.Len() > 1 { q.NextItem() q.PrepareAndPlayNextItem() } else { q.queue = q.queue[:0] } - } else if q.CurrentItem().(*Playlist).songs.Len() > 1 { - q.CurrentItem().(*Playlist).songs.NextItem() - q.PrepareAndPlayNextItem() } else { - if q.Len() > 1 { - q.NextItem() - q.PrepareAndPlayNextItem() - } else { - q.queue = q.queue[:0] - } + panic(err) } - } else { - panic(err) - } - } else { - if err := q.CurrentItem().(*Song).Delete(); err == nil { - if q.Len() > 1 { - q.NextItem() - q.PrepareAndPlayNextItem() - } else { - q.queue = q.queue[:0] - } - } else { - panic(err) } } } func (q *SongQueue) PrepareAndPlayNextItem() { - if q.CurrentItem().ItemType() == "playlist" { - if err := q.CurrentItem().(*Playlist).songs.CurrentItem().(*Song).Download(); err == nil { - q.CurrentItem().(*Playlist).songs.CurrentItem().(*Song).Play() + if q.Len() != 0 { + if q.CurrentItem().ItemType() == "playlist" { + if err := q.CurrentItem().(*Playlist).songs.CurrentItem().(*Song).Download(); err == nil { + q.CurrentItem().(*Playlist).songs.CurrentItem().(*Song).Play() + } else { + username := q.CurrentItem().(*Playlist).submitter + user := dj.client.Self().Channel().Users().Find(username) + user.Send(AUDIO_FAIL_MSG) + q.OnItemFinished() + } } else { - username := q.CurrentItem().(*Playlist).submitter - user := dj.client.Self().Channel().Users().Find(username) - user.Send(AUDIO_FAIL_MSG) - q.OnItemFinished() - } - } else { - if err := q.CurrentItem().(*Song).Download(); err == nil { - q.CurrentItem().(*Song).Play() - } else { - username := q.CurrentItem().(*Song).submitter - user := dj.client.Self().Channel().Users().Find(username) - user.Send(AUDIO_FAIL_MSG) - q.OnItemFinished() + if err := q.CurrentItem().(*Song).Download(); err == nil { + q.CurrentItem().(*Song).Play() + } else { + username := q.CurrentItem().(*Song).submitter + user := dj.client.Self().Channel().Users().Find(username) + user.Send(AUDIO_FAIL_MSG) + q.OnItemFinished() + } } } } diff --git a/strings.go b/strings.go index da0dd5e..8cfe78d 100644 --- a/strings.go +++ b/strings.go @@ -101,3 +101,8 @@ const PLAYLIST_SKIP_ADDED_HTML = ` const VOLUME_SUCCESS_HTML = ` %s has changed the volume to %.2f. ` + +// Message shown to users when a user successfully resets the SongQueue. +const QUEUE_RESET_HTML = ` + %s has cleared the song queue. +`