Added reset command

This commit is contained in:
Matthieu Grieger 2015-01-05 19:16:59 -08:00
parent 803d977e27
commit 1586180246
6 changed files with 101 additions and 48 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -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()
}
}
}
}

View file

@ -101,3 +101,8 @@ const PLAYLIST_SKIP_ADDED_HTML = `
const VOLUME_SUCCESS_HTML = `
<b>%s</b> has changed the volume to <b>%.2f</b>.
`
// Message shown to users when a user successfully resets the SongQueue.
const QUEUE_RESET_HTML = `
<b>%s</b> has cleared the song queue.
`