Added the ability to load an entire playlist from youtube.

This commit is contained in:
Andrew Lalis 2017-09-01 10:32:23 +02:00 committed by Andrew Lalis
parent d4163bbbea
commit b33762f80d
4 changed files with 56 additions and 7 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.github.andrewlalis</groupId>
<artifactId>HandieBot</artifactId>
<version>1.5.3</version>
<version>1.6.0</version>
<build>
<plugins>
<plugin>

View File

@ -1,19 +1,26 @@
package handiebot.command.commands.music;
import com.google.api.services.youtube.model.Video;
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import handiebot.HandieBot;
import handiebot.command.CommandContext;
import handiebot.command.Commands;
import handiebot.command.ReactionHandler;
import handiebot.command.reactionListeners.YoutubePlayListener;
import handiebot.command.types.ContextCommand;
import handiebot.lavaplayer.playlist.UnloadedTrack;
import handiebot.utils.MessageUtils;
import handiebot.utils.YoutubeSearch;
import handiebot.view.BotLog;
import sx.blah.discord.handle.obj.IMessage;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import static handiebot.HandieBot.resourceBundle;
import static handiebot.utils.MessageUtils.sendMessage;
@ -39,12 +46,51 @@ public class PlayCommand extends ContextCommand {
} else {
//Check if an actual URL is used, and if not, create a youtube request.
if (context.getArgs()[0].startsWith("http")) {
if (context.getArgs()[0].contains("list") && Commands.hasPermission(context, 8)){
try {
HandieBot.musicPlayer.getPlayerManager().loadItem(context.getArgs()[0], new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack track) {
//This should not happen.
HandieBot.log.log(BotLog.TYPE.ERROR, "Loaded song while attempting to load playlist.");
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
//This is expected to happen.
HandieBot.log.log(BotLog.TYPE.MUSIC, "Loading a playlist named: "+playlist.getName()+" with "+playlist.getTracks().size()+" tracks.");
MessageUtils.sendMessage("Songs from the playlist **"+playlist.getName()+"** have been added to the queue.", context.getChannel());
HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.clearQueue();
for (AudioTrack track : playlist.getTracks()){
HandieBot.log.log(BotLog.TYPE.MUSIC, "Added song from playlist: "+track.getInfo().title);
HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.getActivePlaylist().addTrack(new UnloadedTrack(track));
}
}
@Override
public void noMatches() {
//Error that nothing was found.
HandieBot.log.log(BotLog.TYPE.ERROR, "No matches while loading playlist.");
}
@Override
public void loadFailed(FriendlyException exception) {
//Error that loading failed.
HandieBot.log.log(BotLog.TYPE.ERROR, "Loading failed while loading playlist.");
}
}).get();
HandieBot.musicPlayer.playQueue(context.getGuild());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
} else {
try {
HandieBot.musicPlayer.addToQueue(context.getGuild(), new UnloadedTrack(context.getArgs()[0]), context.getUser());
} catch (Exception e) {
sendMessage(MessageFormat.format(resourceBundle.getString("commands.command.play.songAddError"), context.getArgs()[0]), context.getChannel());
e.printStackTrace();
}
}
} else {
//Construct a Youtube song choice.
List<Video> videos = YoutubeSearch.query(MessageUtils.getTextFromArgs(context.getArgs(), 0));

View File

@ -37,6 +37,7 @@ public class QueueCommand extends ContextCommand {
@Override
public void execute(CommandContext context) {
//TODO: Ensure that queue embed never runs out of space.
if (context.getArgs().length > 0){
switch (context.getArgs()[0]){
case ("all"):
@ -74,6 +75,7 @@ public class QueueCommand extends ContextCommand {
} else {
sendMessage(resourceBundle.getString("commands.command.queue.remove.error"), context.getChannel());
}
break;
case ("move"):
if (context.getArgs().length == 3 && Commands.hasPermission(context, 8)){
int startIndex = Integer.parseInt(context.getArgs()[1]);
@ -92,6 +94,7 @@ public class QueueCommand extends ContextCommand {
} else {
sendMessage(resourceBundle.getString("commands.command.queue.move.error"), context.getChannel());
}
break;
}
} else {
HandieBot.musicPlayer.showQueueList(context.getGuild(), false);

View File

@ -17,7 +17,7 @@ import static handiebot.HandieBot.log;
* This is useful for quickly loading playlists and only loading a track when it is needed.
*/
public class UnloadedTrack implements Cloneable {
//TODO: Externalize strings.
private String title;
private String url;
private long duration;