More development

#6 Added ability to play a playlist
This commit is contained in:
Isaac Parenteau
2019-10-11 23:26:11 -05:00
parent 1fca679a79
commit 0426c16c45
2 changed files with 87 additions and 44 deletions

View File

@ -35,7 +35,6 @@ import java.nio.file.Path;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
@ -95,6 +94,8 @@ public class GuildMusicHandler {
private ReactionHandler reactionHandler;
private long guildId;
private GuildPlaylist currentPlaylist;
public GuildMusicHandler(Path musicDir, long guildId, Mp3UploadHandler uploadHandler, ReactionHandler reactionHandler, GuildSongRepoService guildSongRepoService) throws IOException {
this.logger = ApplicationLoggerFactory.getLogger(GuildMusicHandler.class);
this.playing = new AtomicBoolean(false);
@ -113,6 +114,26 @@ public class GuildMusicHandler {
player.addListener(ts);
}
public void playlist(GuildMessageReceivedEvent event, List<String> commands) throws Exception {
String command = commands.remove(0);
switch(command) {
case "add":
addPlayList(event, commands);
return;
case "delete":
deletePlayList(event, commands);
return;
case "list":
listPlayList(event, commands);
return;
case "play":
playPlayList(event, commands);
return;
}
}
public Long getCurrentVoiceChannelId() {
return voiceChannelId;
}
@ -186,14 +207,21 @@ public class GuildMusicHandler {
}
playing.set(false);
stop(event);
stop(event, true);
play(event, commands);
playing.set(true);
}
public void stop(GuildMessageReceivedEvent event) {
stop(event, false);
}
public void stop(GuildMessageReceivedEvent event, boolean stoppedFromRepeat) {
player.stopTrack();
voiceChannelId = null;
if (!stoppedFromRepeat) {
currentPlaylist = null;
}
}
public void play(GuildMessageReceivedEvent event) throws Exception {
@ -237,7 +265,7 @@ public class GuildMusicHandler {
event.getChannel().sendMessageFormat("Unable to find song with identifier: %s", uuid).queue();
return;
}
stop(event);
stop(event, true);
}
if (!ts.hasTracks()) {
@ -300,6 +328,30 @@ public class GuildMusicHandler {
}
}
private void playPlayList(GuildMessageReceivedEvent event, List<String> commands) throws Exception {
if (commands == null || commands.isEmpty()) {
event.getChannel().sendMessage(event.getAuthor().getAsMention() + " please provide the name of the playlist to play").queue();
return;
}
String playlist = commands.remove(0);
currentPlaylist = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdAndPlaylist(guildId, event.getMember().getIdLong(), playlist);
if (currentPlaylist == null) {
event.getChannel().sendMessage(event.getAuthor().getAsMention() + ", no playlist with the name of " + playlist + " exists").queue();
return;
}
ts.clearTracks();
for(GuildPlaylistSong gpls : guildSongRepoService.getGuildPlaylistSongRepo().findByGuildPlaylist(currentPlaylist)) {
Song s = gpls.getGuildSong().getSong();
apm.loadItem(s.getFilePath(), ts).get();
}
playing.set(false);
play(event);
playing.set(true);
}
private MessageEmbed error(GuildMessageReceivedEvent event, Throwable ex, String fileName) {
return new EmbedBuilder()
.setTitle("Unable to upload file: " + fileName)
@ -418,7 +470,13 @@ public class GuildMusicHandler {
}
private void loadRandomSong() throws Exception {
List<GuildSong> gsList = guildSongRepoService.getGuildSongRepo().findByGuild(guildId);
List<GuildSong> gsList = null;
if (currentPlaylist == null) {
gsList = guildSongRepoService.getGuildSongRepo().findByGuild(guildId);
} else {
gsList = guildSongRepoService.getGuildPlaylistSongRepo().findByGuildPlaylist(currentPlaylist).stream().map(gpl -> gpl.getGuildSong()).collect(Collectors.toList());
}
if (gsList == null || gsList.isEmpty()) return;
Random random = new Random(System.currentTimeMillis());
@ -438,24 +496,6 @@ public class GuildMusicHandler {
return true;
}
public void playlist(GuildMessageReceivedEvent event, List<String> commands) {
String command = commands.remove(0);
switch(command) {
case "add":
addPlayList(event, commands);
return;
case "delete":
deletePlayList(event, commands);
return;
case "list":
listPlayList(event, commands);
return;
}
}
private void listPlayList(GuildMessageReceivedEvent event, List<String> commands) {
if (commands == null || commands.isEmpty()) {
List<GuildPlaylist> userPlaylist = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong());

View File

@ -79,6 +79,10 @@ public class TrackScheduler extends AudioEventAdapter implements AudioEventListe
}
}
public void clearTracks() {
trackQueue.clear();
}
public void noMatches() {
// TODO Auto-generated method stub
}
@ -107,5 +111,4 @@ public class TrackScheduler extends AudioEventAdapter implements AudioEventListe
return started;
}
}