From 6210f620bbc2760ae3ac228ac030d847657575c8 Mon Sep 17 00:00:00 2001 From: Khoi Tran Date: Tue, 15 Dec 2015 20:32:20 -0500 Subject: [PATCH] Added an argument for listsongs command that limits the number of songs in the list --- README.md | 2 +- commands.go | 18 +++++++++++++++--- strings.go | 4 +++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b640dc9..cfb3d94 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ Command | Description | Arguments | Admin | Example **numsongs** | Outputs the number of songs in the queue in chat. Individual songs and songs within playlists are both counted. | None | No | `!numsongs` **nextsong** | Outputs the title and name of the submitter of the next song in the queue if it exists. | None | No | `!nextsong` **currentsong** | Outputs the title and name of the submitter of the song currently playing. | None | No | `!currentsong` -**listsongs** | Outputs a list of the songs currently in the queue. | None | No | `!listsongs` +**listsongs** | Outputs a list of the songs currently in the queue. | None or desired number of songs to list | No | `!listsongs` **setcomment** | Sets the comment for the bot. If no argument is given, the current comment will be removed. | None OR new_comment | Yes | `!setcomment Hello! I am a bot. Type !help for the available commands.` **numcached** | Outputs the number of songs currently cached on disk. | None | Yes | `!numcached` **cachesize** | Outputs the total file size of the cache in MB. | None | Yes | `!cachesize` diff --git a/commands.go b/commands.go index 8798657..2e81c29 100644 --- a/commands.go +++ b/commands.go @@ -188,7 +188,7 @@ func parseCommand(user *gumble.User, username, command string) { // ListSongs command case dj.conf.Aliases.ListSongsAlias: if dj.HasPermission(username, dj.conf.Permissions.AdminListSongs) { - listSongs(user) + listSongs(user, argument) } else { dj.SendPrivateMessage(user, NO_PERMISSION_MSG) } @@ -478,11 +478,23 @@ func toggleAutomaticShuffle(activate bool, user *gumble.User, username string) { } // listSongs handles !listSongs functionality. Sends a private message to the user a list of all songs in the queue -func listSongs(user *gumble.User) { +func listSongs(user *gumble.User, value string) { if dj.audioStream.IsPlaying() { + num := 0 + if value == "" { + num = dj.queue.Len() + } else { + if parsedNum, err := strconv.Atoi(value); err != nil { + num = dj.queue.Len() + } else { + num = parsedNum + } + } var buffer bytes.Buffer dj.queue.Traverse(func(i int, song Song) { - buffer.WriteString(fmt.Sprintf(SONG_LIST_HTML, song.Title(), song.Submitter())) + if i < num { + buffer.WriteString(fmt.Sprintf(SONG_LIST_HTML, i+1, song.Title(), song.Submitter())) + } }) dj.SendPrivateMessage(user, buffer.String()) } else { diff --git a/strings.go b/strings.go index de0aad7..772bbfb 100644 --- a/strings.go +++ b/strings.go @@ -203,6 +203,8 @@ const CURRENT_SONG_HTML = ` const CURRENT_SONG_PLAYLIST_HTML = ` The %s currently playing is "%s", added %s from the %s "%s". ` + +// Message shown to user when the listsongs command is issued const SONG_LIST_HTML = ` -
"%s", added by %s +
%d: "%s", added by %s `