Added youtube search with play command

This commit is contained in:
Anthony committed 2018-03-23 07:55:44 -04:00
1 parent 9d15ed7643
commit 7dbf7dc979
3 files changed
+29 -6

No files matched your search

@@ -55,7 +55,13 @@ public class PlayCommand extends AbstractCommand {
if (player.isConnected()) { if (player.isConnected()) {
if (player.isConnectedTo(vChannel)) { if (player.isConnectedTo(vChannel)) {
event.getMessage().delete().queue(); event.getMessage().delete().queue();
if (args.startsWith("ytsearch:")) {
player.play(args, event.getTextChannel(), false);
} else {
player.play(args.split(" ")[0], event.getTextChannel(), false); player.play(args.split(" ")[0], event.getTextChannel(), false);
}
} else } else
event.getTextChannel().sendMessage("Not in your voice channel").queue(); event.getTextChannel().sendMessage("Not in your voice channel").queue();
} else { } else {
@@ -108,11 +108,12 @@ public class MusicManager {
public synchronized void play(String url, TextChannel channel, boolean silent ) { public synchronized void play(String url, TextChannel channel, boolean silent ) {
String youtubeCode = SourceFinder.getIdentifier(url); String identifier = SourceFinder.getIdentifier(url, channel);
System.out.println(identifier);
scheduler.setTextChannel(channel); scheduler.setTextChannel(channel);
if (youtubeCode != null) { if (identifier != null) {
playerManager.loadItem(youtubeCode, new AudioLoadResultHandler() { playerManager.loadItem(identifier, new AudioLoadResultHandler() {
@Override @Override
public void trackLoaded(AudioTrack audioTrack) { public void trackLoaded(AudioTrack audioTrack) {
if (!silent) if (!silent)
@@ -124,10 +125,15 @@ public class MusicManager {
@Override @Override
public void playlistLoaded(AudioPlaylist playlist) { public void playlistLoaded(AudioPlaylist playlist) {
if (identifier.startsWith("ytsearch:")) {
scheduler.queue(playlist.getTracks().get(0));
} else {
for (AudioTrack track : playlist.getTracks()) { for (AudioTrack track : playlist.getTracks()) {
scheduler.queue(track); scheduler.queue(track);
} }
} }
}
@Override @Override
public void noMatches() { public void noMatches() {
@@ -1,5 +1,7 @@
package netneutrality.music; package netneutrality.music;
import net.dv8tion.jda.core.entities.TextChannel;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -13,6 +15,7 @@ enum SourceEnum {
Soundcloud, Soundcloud,
Bandcamp, Bandcamp,
Vimeo, Vimeo,
YTSearch,
Nothing Nothing
} }
@@ -30,18 +33,26 @@ public class SourceFinder {
if (youtubeMatcher.find()) if (youtubeMatcher.find())
return SourceEnum.Youtube; return SourceEnum.Youtube;
if (url.startsWith("ytsearch:"))
return SourceEnum.YTSearch;
return SourceEnum.Nothing; return SourceEnum.Nothing;
} }
public static String getIdentifier(String url) { public static String getIdentifier(String url, TextChannel channel) {
String identifier = ""; String identifier = null;
switch (getSource(url)) { switch (getSource(url)) {
case Youtube: case Youtube:
return youtubeMatcher.group(1); identifier = youtubeMatcher.group(1);
break;
case YTSearch:
identifier = url;
break;
case Nothing: case Nothing:
identifier = null; identifier = null;
channel.sendMessage("No source found to play the given url").queue();
break; break;
} }