@ -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());
|
||||
@ -481,24 +521,24 @@ public class GuildMusicHandler {
|
||||
}
|
||||
sb.append("```");
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
String playlist = commands.remove(0);
|
||||
|
||||
|
||||
GuildPlaylist gpl = guildSongRepoService.getGuildPlaylistRepo().findGuildUserPlaylistFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong(), playlist);
|
||||
if (gpl == null) {
|
||||
event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined playlists on this server by the name of " + playlist).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (gpl.getGuildPlaylistSongList().isEmpty()) {
|
||||
event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined songs for playlist " + playlist).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int longestSong = 0;
|
||||
int longestArtist = 0;
|
||||
for(GuildPlaylistSong gpls : gpl.getGuildPlaylistSongList()) {
|
||||
@ -519,11 +559,11 @@ public class GuildMusicHandler {
|
||||
}
|
||||
sb.append("```");
|
||||
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) {
|
||||
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();
|
||||
@ -545,7 +585,7 @@ public class GuildMusicHandler {
|
||||
GuildPlaylist gpl = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdAndPlaylist(guild, userId, playlist);
|
||||
|
||||
if (commands.isEmpty()) {
|
||||
|
||||
|
||||
MessageEmbed embed = new EmbedBuilder()
|
||||
.setColor(Color.RED)
|
||||
.setTitle("Delete Playlist " + playlist)
|
||||
@ -553,27 +593,27 @@ public class GuildMusicHandler {
|
||||
.setTimestamp(OffsetDateTime.now())
|
||||
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
||||
.build();
|
||||
|
||||
|
||||
event.getChannel().sendMessage(embed).queue((msg) -> {
|
||||
|
||||
|
||||
ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId());
|
||||
handler.setExpiresIn(TimeUnit.MINUTES, 1);
|
||||
handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListConfirm(gpl, event.getMember().getAsMention(), msg));
|
||||
handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg));
|
||||
|
||||
|
||||
reactionHandler.addReactionListener(guildId, msg, handler);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
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");
|
||||
for (GuildPlaylistSong gpls : songs) {
|
||||
GuildSong gs = gpls.getGuildSong();
|
||||
sb.append(String.format("**%s** by __%s__%n", gs.getSong().getTitle(), gs.getSong().getArtist()));
|
||||
}
|
||||
|
||||
|
||||
MessageEmbed embed = new EmbedBuilder()
|
||||
.setColor(Color.RED)
|
||||
.setTitle("Delete Playlist Songs")
|
||||
@ -581,14 +621,14 @@ public class GuildMusicHandler {
|
||||
.setTimestamp(OffsetDateTime.now())
|
||||
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
||||
.build();
|
||||
|
||||
|
||||
event.getChannel().sendMessage(embed).queue((msg) -> {
|
||||
|
||||
|
||||
ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId());
|
||||
handler.setExpiresIn(TimeUnit.MINUTES, 1);
|
||||
handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListSongConfirm(songs, event.getMember().getAsMention(), msg));
|
||||
handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg));
|
||||
|
||||
|
||||
reactionHandler.addReactionListener(guildId, msg, handler);
|
||||
});
|
||||
}
|
||||
@ -618,7 +658,7 @@ public class GuildMusicHandler {
|
||||
} finally {
|
||||
deleteMessage(msg);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void addPlayList(GuildMessageReceivedEvent event, List<String> commands) {
|
||||
@ -702,7 +742,7 @@ public class GuildMusicHandler {
|
||||
|
||||
event.getChannel().sendMessage(embed).queue();
|
||||
}
|
||||
|
||||
|
||||
private void deleteMessage(Message msg) {
|
||||
try {
|
||||
msg.delete().complete();
|
||||
|
@ -78,6 +78,10 @@ public class TrackScheduler extends AudioEventAdapter implements AudioEventListe
|
||||
trackQueue.add(at);
|
||||
}
|
||||
}
|
||||
|
||||
public void clearTracks() {
|
||||
trackQueue.clear();
|
||||
}
|
||||
|
||||
public void noMatches() {
|
||||
// TODO Auto-generated method stub
|
||||
@ -106,6 +110,5 @@ public class TrackScheduler extends AudioEventAdapter implements AudioEventListe
|
||||
public boolean playing() {
|
||||
return started;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user