From e7283743fcc9f0d3b9789b415a0767ae417f24ff Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sun, 25 Jun 2017 00:31:10 +0200 Subject: [PATCH] Lots of minor bug fixes, added queue clear. --- README.md | 6 ++- .../handiebot/command/CommandHandler.java | 37 +------------------ src/main/java/handiebot/command/Commands.java | 27 ++++++++++++++ .../command/commands/HelpCommand.java | 2 +- .../commands/music/PlaylistCommand.java | 23 ++++++------ .../command/commands/music/QueueCommand.java | 12 +++++- .../handiebot/lavaplayer/MusicPlayer.java | 2 +- .../handiebot/lavaplayer/TrackScheduler.java | 10 ++++- .../lavaplayer/playlist/Playlist.java | 28 ++++++++++++-- 9 files changed, 91 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index feee5e1..0f5f1b8 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,11 @@ Because the play command is defined as `play [URL]`, and the queue command is de * `skip` - If a song is playing, the bot will skip it and play the next song in the queue. -* `queue [all]` - Lists up to the first 10 items on the queue, if no argument is given. If you add `all`, the bot will upload a list to [PasteBin](http://pastebin.com) of the entire queue, provided it is greater than 10 elements, and give you a link which expires in 10 minutes. +* `queue [all|clear]` - Lists up to the first 10 items on the queue, if no argument is given. + + * If you add `all`, the bot will upload a list to [PasteBin](http://pastebin.com) of the entire queue, provided it is greater than 10 elements, and give you a link which expires in 10 minutes. + + * If `clear` is used, the queue will be cleared and the current song will be stopped. * `repeat [true|false]` - Sets the bot to repeat the playlist, as in once a song is removed from the queue to be played, it is added back to the end of the playlist. diff --git a/src/main/java/handiebot/command/CommandHandler.java b/src/main/java/handiebot/command/CommandHandler.java index 3b78cd5..7e7df3a 100644 --- a/src/main/java/handiebot/command/CommandHandler.java +++ b/src/main/java/handiebot/command/CommandHandler.java @@ -1,9 +1,5 @@ package handiebot.command; -import handiebot.command.commands.HelpCommand; -import handiebot.command.commands.InfoCommand; -import handiebot.command.commands.SetPrefixCommand; -import handiebot.command.commands.music.*; import handiebot.utils.DisappearingMessage; import handiebot.utils.FileUtil; import handiebot.view.BotLog; @@ -53,38 +49,7 @@ public class CommandHandler { CommandContext context = new CommandContext(user, channel, guild, args); if (guild != null && command != null){ DisappearingMessage.deleteMessageAfter(1000, message); - switch (command){ - //Music commands. - case ("play"): - new PlayCommand().execute(context); - break; - case ("skip"): - new SkipCommand().execute(context); - break; - case ("queue"): - new QueueCommand().execute(context); - break; - case ("repeat"): - new RepeatCommand().execute(context); - break; - case ("shuffle"): - new ShuffleCommand().execute(context); - break; - case ("playlist"): - new PlaylistCommand().execute(context); - break; - //Other commands. - case ("help"): - new HelpCommand().execute(context); - break; - case ("info"): - new InfoCommand().execute(context); - break; - case ("setprefix"): - new SetPrefixCommand().execute(context); - default: - log.log(BotLog.TYPE.ERROR, guild, "Invalid command: "+command+" issued by "+user.getName()); - } + Commands.executeCommand(command, context); } } diff --git a/src/main/java/handiebot/command/Commands.java b/src/main/java/handiebot/command/Commands.java index 67a1aba..2f87ae2 100644 --- a/src/main/java/handiebot/command/Commands.java +++ b/src/main/java/handiebot/command/Commands.java @@ -1,11 +1,18 @@ package handiebot.command; +import handiebot.command.commands.HelpCommand; +import handiebot.command.commands.InfoCommand; +import handiebot.command.commands.SetPrefixCommand; import handiebot.command.commands.music.*; import handiebot.command.types.Command; +import handiebot.command.types.ContextCommand; +import handiebot.view.BotLog; import java.util.ArrayList; import java.util.List; +import static handiebot.HandieBot.log; + /** * @author Andrew Lalis * Class to hold a list of commands, as static definitions that can be called upon by {@code CommandHandler}. @@ -22,6 +29,26 @@ public class Commands { commands.add(new RepeatCommand()); commands.add(new ShuffleCommand()); commands.add(new PlaylistCommand()); + //Other commands. + commands.add(new HelpCommand()); + commands.add(new InfoCommand()); + commands.add(new SetPrefixCommand()); + } + + /** + * Attempts to execute a command from a given command string. + * @param command The string representation of a main command, without prefix. + */ + public static void executeCommand(String command, CommandContext context){ + for (Command cmd : commands) { + if (cmd.getName().equals(command)){ + if (cmd instanceof ContextCommand){ + ((ContextCommand)cmd).execute(context); + return; + } + } + } + log.log(BotLog.TYPE.ERROR, context.getGuild(), "Invalid command: "+command+" issued by "+context.getUser().getName()); } } diff --git a/src/main/java/handiebot/command/commands/HelpCommand.java b/src/main/java/handiebot/command/commands/HelpCommand.java index 6142cf2..4050398 100644 --- a/src/main/java/handiebot/command/commands/HelpCommand.java +++ b/src/main/java/handiebot/command/commands/HelpCommand.java @@ -12,7 +12,7 @@ import java.awt.*; * Class for sending help/command info to a user if they so desire it. */ public class HelpCommand extends ContextCommand { - +//TODO: Finish the help class. public HelpCommand() { super("help"); } diff --git a/src/main/java/handiebot/command/commands/music/PlaylistCommand.java b/src/main/java/handiebot/command/commands/music/PlaylistCommand.java index cdc9fa9..0b82649 100644 --- a/src/main/java/handiebot/command/commands/music/PlaylistCommand.java +++ b/src/main/java/handiebot/command/commands/music/PlaylistCommand.java @@ -126,7 +126,7 @@ public class PlaylistCommand extends ContextCommand { Playlist playlist = new Playlist(context.getArgs()[1]); playlist.load(); IMessage message = context.getChannel().sendMessage(playlist.toString()); - DisappearingMessage.deleteMessageAfter(6000, message); + DisappearingMessage.deleteMessageAfter(12000, message); } else { new DisappearingMessage(context.getChannel(), "The playlist you specified does not exist.\nUse `"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists.", 5000); } @@ -137,7 +137,7 @@ public class PlaylistCommand extends ContextCommand { sb.append(playlist).append('\n'); } IMessage message = context.getChannel().sendMessage(sb.toString()); - DisappearingMessage.deleteMessageAfter(6000, message); + DisappearingMessage.deleteMessageAfter(12000, message); } } @@ -228,9 +228,10 @@ public class PlaylistCommand extends ContextCommand { Playlist playlist = new Playlist(context.getArgs()[1]); playlist.load(); try{ - int index = Integer.parseInt(context.getArgs()[2]); + int index = Integer.parseInt(context.getArgs()[2]) - 1; UnloadedTrack track = playlist.getTracks().get(index); playlist.removeTrack(track); + playlist.save(); new DisappearingMessage(context.getChannel(), "Removed song: *"+track.getTitle()+"* from playlist **"+playlist.getName()+"**.", 6000); log.log(BotLog.TYPE.MUSIC, "Removed song: "+track.getTitle()+" from playlist ["+playlist.getName()+"]."); DisappearingMessage.deleteMessageAfter(6000, context.getChannel().sendMessage(playlist.toString())); @@ -266,17 +267,15 @@ public class PlaylistCommand extends ContextCommand { new DisappearingMessage(context.getChannel(), "You must enter two integer values for the song indices.", 5000); } UnloadedTrack track; - if (oldIndex > -1 && oldIndex < playlist.getTrackCount()){ + if ((oldIndex > -1 && oldIndex < playlist.getTrackCount()) && + (newIndex > -1 && newIndex <= playlist.getTrackCount())){ track = playlist.getTracks().remove(oldIndex); - if (newIndex > -1 && newIndex <= playlist.getTrackCount()){ - playlist.getTracks().add(newIndex, track); - new DisappearingMessage(context.getChannel(), "Moved song *"+track.getTitle()+"* from position "+(oldIndex+1)+" to position "+(newIndex+1), 6000); - log.log(BotLog.TYPE.MUSIC, "Moved song "+track.getTitle()+" from position "+(oldIndex+1)+" to position "+(newIndex+1)); - } else { - new DisappearingMessage(context.getChannel(), "The index of the song's new position is invalid. You entered "+newIndex, 5000); - } + playlist.getTracks().add(newIndex, track); + playlist.save(); + new DisappearingMessage(context.getChannel(), "Moved song *"+track.getTitle()+"* from position "+(oldIndex+1)+" to position "+(newIndex+1), 6000); + log.log(BotLog.TYPE.MUSIC, "Moved song "+track.getTitle()+" from position "+(oldIndex+1)+" to position "+(newIndex+1)); } else { - new DisappearingMessage(context.getChannel(), "The index of the song is invalid. You entered "+oldIndex, 5000); + new DisappearingMessage(context.getChannel(), "The song indices are invalid. You specified moving song "+oldIndex+" to position "+newIndex+". ", 5000); } } else { new DisappearingMessage(context.getChannel(), "You must provide a playlist name, followed by the song index, and a new index for that song.", 5000); diff --git a/src/main/java/handiebot/command/commands/music/QueueCommand.java b/src/main/java/handiebot/command/commands/music/QueueCommand.java index ebcae20..d5ab4b9 100644 --- a/src/main/java/handiebot/command/commands/music/QueueCommand.java +++ b/src/main/java/handiebot/command/commands/music/QueueCommand.java @@ -3,6 +3,7 @@ package handiebot.command.commands.music; import handiebot.HandieBot; import handiebot.command.CommandContext; import handiebot.command.types.ContextCommand; +import handiebot.utils.DisappearingMessage; /** * @author Andrew Lalis @@ -15,7 +16,16 @@ public class QueueCommand extends ContextCommand { @Override public void execute(CommandContext context) { - HandieBot.musicPlayer.showQueueList(context.getGuild(), (context.getArgs().length == 1 && context.getArgs()[0].equals("all"))); + if (context.getArgs().length == 1){ + if (context.getArgs()[0].equals("all")){ + HandieBot.musicPlayer.showQueueList(context.getGuild(), true); + } else if (context.getArgs()[0].equals("clear")){ + HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.clearQueue(); + new DisappearingMessage(context.getChannel(), "Cleared the queue.", 5000); + } + } else { + HandieBot.musicPlayer.showQueueList(context.getGuild(), false); + } } } diff --git a/src/main/java/handiebot/lavaplayer/MusicPlayer.java b/src/main/java/handiebot/lavaplayer/MusicPlayer.java index 7e9438d..95024a4 100644 --- a/src/main/java/handiebot/lavaplayer/MusicPlayer.java +++ b/src/main/java/handiebot/lavaplayer/MusicPlayer.java @@ -169,7 +169,7 @@ public class MusicPlayer { sb.append(tracks.get(i).getURL()).append(")"); sb.append(tracks.get(i).getFormattedDuration()).append('\n'); } - builder.appendField("Showing " + (tracks.size() <= 10 ? tracks.size() : "the first 10") + " track" + (tracks.size() > 1 ? "s" : "") + ".", sb.toString(), false); + builder.appendField("Showing " + (tracks.size() <= 10 ? tracks.size() : "the first 10") + " track" + (tracks.size() > 1 ? "s" : "") + " out of "+tracks.size()+".", sb.toString(), false); IMessage message = getChatChannel(guild).sendMessage(builder.build()); DisappearingMessage.deleteMessageAfter(6000, message); } diff --git a/src/main/java/handiebot/lavaplayer/TrackScheduler.java b/src/main/java/handiebot/lavaplayer/TrackScheduler.java index 49be0cf..98cd89c 100644 --- a/src/main/java/handiebot/lavaplayer/TrackScheduler.java +++ b/src/main/java/handiebot/lavaplayer/TrackScheduler.java @@ -50,13 +50,21 @@ public class TrackScheduler extends AudioEventAdapter { * @param playlist the playlist to load from. */ public void setPlaylist(Playlist playlist){ - this.activePlaylist = playlist; + this.activePlaylist.copy(playlist); } public Playlist getActivePlaylist(){ return this.activePlaylist; } + /** + * Clears the queue. + */ + public void clearQueue(){ + this.quit(); + this.activePlaylist.clear(); + } + /** * Sets whether or not songs get placed back into the queue once they're played. * @param value True if the playlist should repeat. diff --git a/src/main/java/handiebot/lavaplayer/playlist/Playlist.java b/src/main/java/handiebot/lavaplayer/playlist/Playlist.java index cee15e1..442e37a 100644 --- a/src/main/java/handiebot/lavaplayer/playlist/Playlist.java +++ b/src/main/java/handiebot/lavaplayer/playlist/Playlist.java @@ -56,6 +56,21 @@ public class Playlist { this.tracks.remove(track); } + /** + * Copies all the tracks from another playlist onto this one. + * @param playlist A playlist. + */ + public void copy(Playlist playlist){ + this.getTracks().clear(); + for (UnloadedTrack track : playlist.getTracks()){ + this.tracks.add(track.clone()); + } + } + + public void clear(){ + this.tracks.clear(); + } + /** * Loads and returns the audio track that's first on the list. * This removes that track from the playlist. @@ -63,6 +78,9 @@ public class Playlist { * @return The AudioTrack corresponding to the next UnloadedTrack in the list. */ public AudioTrack loadNextTrack(boolean shouldShuffle){ + if (this.getTrackCount() == 0){ + return null; + } if (shouldShuffle){ return this.tracks.remove(getShuffledIndex(this.tracks.size())).loadAudioTrack(); } else { @@ -188,9 +206,13 @@ public class Playlist { @Override public String toString(){ - StringBuilder sb = new StringBuilder("HandieBot Playlist: "+this.getName()+'\n'); - for (int i = 0; i < this.getTrackCount(); i++){ - sb.append(i+1).append(". ").append(this.tracks.get(i).getTitle()).append(" ").append(this.tracks.get(i).getFormattedDuration()).append("\n"); + StringBuilder sb = new StringBuilder("Playlist: "+this.getName()+'\n'); + if (this.getTrackCount() == 0){ + sb.append("There are no songs in this playlist."); + } else { + for (int i = 0; i < this.getTrackCount(); i++) { + sb.append(i + 1).append(". ").append(this.tracks.get(i).getTitle()).append(" ").append(this.tracks.get(i).getFormattedDuration()).append("\n"); + } } return sb.toString(); }