Lots of minor bug fixes, added queue clear.

This commit is contained in:
Andrew Lalis 2017-06-25 00:31:10 +02:00
parent 3288f7aa75
commit e7283743fc
9 changed files with 91 additions and 56 deletions

View File

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

View File

@ -1,9 +1,5 @@
package handiebot.command; 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.DisappearingMessage;
import handiebot.utils.FileUtil; import handiebot.utils.FileUtil;
import handiebot.view.BotLog; import handiebot.view.BotLog;
@ -53,38 +49,7 @@ public class CommandHandler {
CommandContext context = new CommandContext(user, channel, guild, args); CommandContext context = new CommandContext(user, channel, guild, args);
if (guild != null && command != null){ if (guild != null && command != null){
DisappearingMessage.deleteMessageAfter(1000, message); DisappearingMessage.deleteMessageAfter(1000, message);
switch (command){ Commands.executeCommand(command, context);
//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());
}
} }
} }

View File

@ -1,11 +1,18 @@
package handiebot.command; 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.commands.music.*;
import handiebot.command.types.Command; import handiebot.command.types.Command;
import handiebot.command.types.ContextCommand;
import handiebot.view.BotLog;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static handiebot.HandieBot.log;
/** /**
* @author Andrew Lalis * @author Andrew Lalis
* Class to hold a list of commands, as static definitions that can be called upon by {@code CommandHandler}. * 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 RepeatCommand());
commands.add(new ShuffleCommand()); commands.add(new ShuffleCommand());
commands.add(new PlaylistCommand()); 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());
} }
} }

View File

@ -12,7 +12,7 @@ import java.awt.*;
* Class for sending help/command info to a user if they so desire it. * Class for sending help/command info to a user if they so desire it.
*/ */
public class HelpCommand extends ContextCommand { public class HelpCommand extends ContextCommand {
//TODO: Finish the help class.
public HelpCommand() { public HelpCommand() {
super("help"); super("help");
} }

View File

@ -126,7 +126,7 @@ public class PlaylistCommand extends ContextCommand {
Playlist playlist = new Playlist(context.getArgs()[1]); Playlist playlist = new Playlist(context.getArgs()[1]);
playlist.load(); playlist.load();
IMessage message = context.getChannel().sendMessage(playlist.toString()); IMessage message = context.getChannel().sendMessage(playlist.toString());
DisappearingMessage.deleteMessageAfter(6000, message); DisappearingMessage.deleteMessageAfter(12000, message);
} else { } 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); 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'); sb.append(playlist).append('\n');
} }
IMessage message = context.getChannel().sendMessage(sb.toString()); 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 playlist = new Playlist(context.getArgs()[1]);
playlist.load(); playlist.load();
try{ try{
int index = Integer.parseInt(context.getArgs()[2]); int index = Integer.parseInt(context.getArgs()[2]) - 1;
UnloadedTrack track = playlist.getTracks().get(index); UnloadedTrack track = playlist.getTracks().get(index);
playlist.removeTrack(track); playlist.removeTrack(track);
playlist.save();
new DisappearingMessage(context.getChannel(), "Removed song: *"+track.getTitle()+"* from playlist **"+playlist.getName()+"**.", 6000); 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()+"]."); log.log(BotLog.TYPE.MUSIC, "Removed song: "+track.getTitle()+" from playlist ["+playlist.getName()+"].");
DisappearingMessage.deleteMessageAfter(6000, context.getChannel().sendMessage(playlist.toString())); 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); new DisappearingMessage(context.getChannel(), "You must enter two integer values for the song indices.", 5000);
} }
UnloadedTrack track; UnloadedTrack track;
if (oldIndex > -1 && oldIndex < playlist.getTrackCount()){ if ((oldIndex > -1 && oldIndex < playlist.getTrackCount()) &&
(newIndex > -1 && newIndex <= playlist.getTrackCount())){
track = playlist.getTracks().remove(oldIndex); track = playlist.getTracks().remove(oldIndex);
if (newIndex > -1 && newIndex <= playlist.getTrackCount()){
playlist.getTracks().add(newIndex, track); playlist.getTracks().add(newIndex, track);
playlist.save();
new DisappearingMessage(context.getChannel(), "Moved song *"+track.getTitle()+"* from position "+(oldIndex+1)+" to position "+(newIndex+1), 6000); 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)); log.log(BotLog.TYPE.MUSIC, "Moved song "+track.getTitle()+" from position "+(oldIndex+1)+" to position "+(newIndex+1));
} else { } else {
new DisappearingMessage(context.getChannel(), "The index of the song's new position is invalid. You entered "+newIndex, 5000); new DisappearingMessage(context.getChannel(), "The song indices are invalid. You specified moving song "+oldIndex+" to position "+newIndex+". ", 5000);
}
} else {
new DisappearingMessage(context.getChannel(), "The index of the song is invalid. You entered "+oldIndex, 5000);
} }
} else { } else {
new DisappearingMessage(context.getChannel(), "You must provide a playlist name, followed by the song index, and a new index for that song.", 5000); new DisappearingMessage(context.getChannel(), "You must provide a playlist name, followed by the song index, and a new index for that song.", 5000);

View File

@ -3,6 +3,7 @@ package handiebot.command.commands.music;
import handiebot.HandieBot; import handiebot.HandieBot;
import handiebot.command.CommandContext; import handiebot.command.CommandContext;
import handiebot.command.types.ContextCommand; import handiebot.command.types.ContextCommand;
import handiebot.utils.DisappearingMessage;
/** /**
* @author Andrew Lalis * @author Andrew Lalis
@ -15,7 +16,16 @@ public class QueueCommand extends ContextCommand {
@Override @Override
public void execute(CommandContext context) { 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);
}
} }
} }

View File

@ -169,7 +169,7 @@ public class MusicPlayer {
sb.append(tracks.get(i).getURL()).append(")"); sb.append(tracks.get(i).getURL()).append(")");
sb.append(tracks.get(i).getFormattedDuration()).append('\n'); 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()); IMessage message = getChatChannel(guild).sendMessage(builder.build());
DisappearingMessage.deleteMessageAfter(6000, message); DisappearingMessage.deleteMessageAfter(6000, message);
} }

View File

@ -50,13 +50,21 @@ public class TrackScheduler extends AudioEventAdapter {
* @param playlist the playlist to load from. * @param playlist the playlist to load from.
*/ */
public void setPlaylist(Playlist playlist){ public void setPlaylist(Playlist playlist){
this.activePlaylist = playlist; this.activePlaylist.copy(playlist);
} }
public Playlist getActivePlaylist(){ public Playlist getActivePlaylist(){
return this.activePlaylist; 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. * Sets whether or not songs get placed back into the queue once they're played.
* @param value True if the playlist should repeat. * @param value True if the playlist should repeat.

View File

@ -56,6 +56,21 @@ public class Playlist {
this.tracks.remove(track); 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. * Loads and returns the audio track that's first on the list.
* This removes that track from the playlist. * 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. * @return The AudioTrack corresponding to the next UnloadedTrack in the list.
*/ */
public AudioTrack loadNextTrack(boolean shouldShuffle){ public AudioTrack loadNextTrack(boolean shouldShuffle){
if (this.getTrackCount() == 0){
return null;
}
if (shouldShuffle){ if (shouldShuffle){
return this.tracks.remove(getShuffledIndex(this.tracks.size())).loadAudioTrack(); return this.tracks.remove(getShuffledIndex(this.tracks.size())).loadAudioTrack();
} else { } else {
@ -188,10 +206,14 @@ public class Playlist {
@Override @Override
public String toString(){ public String toString(){
StringBuilder sb = new StringBuilder("HandieBot Playlist: "+this.getName()+'\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++) { 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"); sb.append(i + 1).append(". ").append(this.tracks.get(i).getTitle()).append(" ").append(this.tracks.get(i).getFormattedDuration()).append("\n");
} }
}
return sb.toString(); return sb.toString();
} }