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.time.OffsetDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
@ -95,6 +94,8 @@ public class GuildMusicHandler {
private ReactionHandler reactionHandler; private ReactionHandler reactionHandler;
private long guildId; private long guildId;
private GuildPlaylist currentPlaylist;
public GuildMusicHandler(Path musicDir, long guildId, Mp3UploadHandler uploadHandler, ReactionHandler reactionHandler, GuildSongRepoService guildSongRepoService) throws IOException { public GuildMusicHandler(Path musicDir, long guildId, Mp3UploadHandler uploadHandler, ReactionHandler reactionHandler, GuildSongRepoService guildSongRepoService) throws IOException {
this.logger = ApplicationLoggerFactory.getLogger(GuildMusicHandler.class); this.logger = ApplicationLoggerFactory.getLogger(GuildMusicHandler.class);
this.playing = new AtomicBoolean(false); this.playing = new AtomicBoolean(false);
@ -113,6 +114,26 @@ public class GuildMusicHandler {
player.addListener(ts); 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() { public Long getCurrentVoiceChannelId() {
return voiceChannelId; return voiceChannelId;
} }
@ -186,14 +207,21 @@ public class GuildMusicHandler {
} }
playing.set(false); playing.set(false);
stop(event); stop(event, true);
play(event, commands); play(event, commands);
playing.set(true); playing.set(true);
} }
public void stop(GuildMessageReceivedEvent event) { public void stop(GuildMessageReceivedEvent event) {
stop(event, false);
}
public void stop(GuildMessageReceivedEvent event, boolean stoppedFromRepeat) {
player.stopTrack(); player.stopTrack();
voiceChannelId = null; voiceChannelId = null;
if (!stoppedFromRepeat) {
currentPlaylist = null;
}
} }
public void play(GuildMessageReceivedEvent event) throws Exception { 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(); event.getChannel().sendMessageFormat("Unable to find song with identifier: %s", uuid).queue();
return; return;
} }
stop(event); stop(event, true);
} }
if (!ts.hasTracks()) { 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) { private MessageEmbed error(GuildMessageReceivedEvent event, Throwable ex, String fileName) {
return new EmbedBuilder() return new EmbedBuilder()
.setTitle("Unable to upload file: " + fileName) .setTitle("Unable to upload file: " + fileName)
@ -418,7 +470,13 @@ public class GuildMusicHandler {
} }
private void loadRandomSong() throws Exception { 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; if (gsList == null || gsList.isEmpty()) return;
Random random = new Random(System.currentTimeMillis()); Random random = new Random(System.currentTimeMillis());
@ -438,24 +496,6 @@ public class GuildMusicHandler {
return true; 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) { private void listPlayList(GuildMessageReceivedEvent event, List<String> commands) {
if (commands == null || commands.isEmpty()) { if (commands == null || commands.isEmpty()) {
List<GuildPlaylist> userPlaylist = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong()); List<GuildPlaylist> userPlaylist = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong());
@ -481,24 +521,24 @@ public class GuildMusicHandler {
} }
sb.append("```"); sb.append("```");
event.getChannel().sendMessage("**" + "Currently available playlists for " + event.getMember().getAsMention() + "**").queue((succcess)-> event.getChannel().sendMessage("**" + "Currently available playlists for " + event.getMember().getAsMention() + "**").queue((succcess)->
event.getChannel().sendMessage(sb.toString()).queue() event.getChannel().sendMessage(sb.toString()).queue()
); );
return; return;
} }
String playlist = commands.remove(0); String playlist = commands.remove(0);
GuildPlaylist gpl = guildSongRepoService.getGuildPlaylistRepo().findGuildUserPlaylistFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong(), playlist); GuildPlaylist gpl = guildSongRepoService.getGuildPlaylistRepo().findGuildUserPlaylistFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong(), playlist);
if (gpl == null) { if (gpl == null) {
event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined playlists on this server by the name of " + playlist).queue(); event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined playlists on this server by the name of " + playlist).queue();
return; return;
} }
if (gpl.getGuildPlaylistSongList().isEmpty()) { if (gpl.getGuildPlaylistSongList().isEmpty()) {
event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined songs for playlist " + playlist).queue(); event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined songs for playlist " + playlist).queue();
return; return;
} }
int longestSong = 0; int longestSong = 0;
int longestArtist = 0; int longestArtist = 0;
for(GuildPlaylistSong gpls : gpl.getGuildPlaylistSongList()) { for(GuildPlaylistSong gpls : gpl.getGuildPlaylistSongList()) {
@ -519,11 +559,11 @@ public class GuildMusicHandler {
} }
sb.append("```"); sb.append("```");
event.getChannel().sendMessage("**" + "Current songs in playlists " + playlist + " for " + event.getMember().getAsMention() + "**").queue((succcess)-> event.getChannel().sendMessage("**" + "Current songs in playlists " + playlist + " for " + event.getMember().getAsMention() + "**").queue((succcess)->
event.getChannel().sendMessage(sb.toString()).queue() event.getChannel().sendMessage(sb.toString()).queue()
); );
} }
private void deletePlayList(GuildMessageReceivedEvent event, List<String> commands) { private void deletePlayList(GuildMessageReceivedEvent event, List<String> commands) {
if (commands == null || commands.isEmpty()) { if (commands == null || commands.isEmpty()) {
event.getChannel().sendMessage(event.getAuthor().getAsMention() + " you have to provide the playlist to create a new playlist (no spaces in name) and optionally a list of uuids for songs to add").queue(); event.getChannel().sendMessage(event.getAuthor().getAsMention() + " you have to provide the playlist to create a new playlist (no spaces in name) and optionally a list of uuids for songs to add").queue();
@ -545,7 +585,7 @@ public class GuildMusicHandler {
GuildPlaylist gpl = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdAndPlaylist(guild, userId, playlist); GuildPlaylist gpl = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdAndPlaylist(guild, userId, playlist);
if (commands.isEmpty()) { if (commands.isEmpty()) {
MessageEmbed embed = new EmbedBuilder() MessageEmbed embed = new EmbedBuilder()
.setColor(Color.RED) .setColor(Color.RED)
.setTitle("Delete Playlist " + playlist) .setTitle("Delete Playlist " + playlist)
@ -553,27 +593,27 @@ public class GuildMusicHandler {
.setTimestamp(OffsetDateTime.now()) .setTimestamp(OffsetDateTime.now())
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl()) .setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
.build(); .build();
event.getChannel().sendMessage(embed).queue((msg) -> { event.getChannel().sendMessage(embed).queue((msg) -> {
ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId()); ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId());
handler.setExpiresIn(TimeUnit.MINUTES, 1); handler.setExpiresIn(TimeUnit.MINUTES, 1);
handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListConfirm(gpl, event.getMember().getAsMention(), msg)); handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListConfirm(gpl, event.getMember().getAsMention(), msg));
handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg)); handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg));
reactionHandler.addReactionListener(guildId, msg, handler); reactionHandler.addReactionListener(guildId, msg, handler);
}); });
return; return;
} }
List<GuildPlaylistSong> songs = guildSongRepoService.getGuildPlaylistSongRepo().findByGuildPlaylistAndSongIds(gpl, commands); List<GuildPlaylistSong> songs = guildSongRepoService.getGuildPlaylistSongRepo().findByGuildPlaylistAndSongIds(gpl, commands);
StringBuilder sb = new StringBuilder("Are you sure you want to delete the following songs from playlist " + playlist + "?\n"); StringBuilder sb = new StringBuilder("Are you sure you want to delete the following songs from playlist " + playlist + "?\n");
for (GuildPlaylistSong gpls : songs) { for (GuildPlaylistSong gpls : songs) {
GuildSong gs = gpls.getGuildSong(); GuildSong gs = gpls.getGuildSong();
sb.append(String.format("**%s** by __%s__%n", gs.getSong().getTitle(), gs.getSong().getArtist())); sb.append(String.format("**%s** by __%s__%n", gs.getSong().getTitle(), gs.getSong().getArtist()));
} }
MessageEmbed embed = new EmbedBuilder() MessageEmbed embed = new EmbedBuilder()
.setColor(Color.RED) .setColor(Color.RED)
.setTitle("Delete Playlist Songs") .setTitle("Delete Playlist Songs")
@ -581,14 +621,14 @@ public class GuildMusicHandler {
.setTimestamp(OffsetDateTime.now()) .setTimestamp(OffsetDateTime.now())
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl()) .setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
.build(); .build();
event.getChannel().sendMessage(embed).queue((msg) -> { event.getChannel().sendMessage(embed).queue((msg) -> {
ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId()); ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId());
handler.setExpiresIn(TimeUnit.MINUTES, 1); handler.setExpiresIn(TimeUnit.MINUTES, 1);
handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListSongConfirm(songs, event.getMember().getAsMention(), msg)); handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListSongConfirm(songs, event.getMember().getAsMention(), msg));
handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg)); handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg));
reactionHandler.addReactionListener(guildId, msg, handler); reactionHandler.addReactionListener(guildId, msg, handler);
}); });
} }
@ -618,7 +658,7 @@ public class GuildMusicHandler {
} finally { } finally {
deleteMessage(msg); deleteMessage(msg);
} }
} }
private void addPlayList(GuildMessageReceivedEvent event, List<String> commands) { private void addPlayList(GuildMessageReceivedEvent event, List<String> commands) {
@ -702,7 +742,7 @@ public class GuildMusicHandler {
event.getChannel().sendMessage(embed).queue(); event.getChannel().sendMessage(embed).queue();
} }
private void deleteMessage(Message msg) { private void deleteMessage(Message msg) {
try { try {
msg.delete().complete(); msg.delete().complete();

View File

@ -78,6 +78,10 @@ public class TrackScheduler extends AudioEventAdapter implements AudioEventListe
trackQueue.add(at); trackQueue.add(at);
} }
} }
public void clearTracks() {
trackQueue.clear();
}
public void noMatches() { public void noMatches() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
@ -106,6 +110,5 @@ public class TrackScheduler extends AudioEventAdapter implements AudioEventListe
public boolean playing() { public boolean playing() {
return started; return started;
} }
} }