updated functionality

#13 Added functionality to allow players to play a song by id
This commit is contained in:
Isaac Parenteau
2019-10-08 21:34:42 -05:00
parent a0021abf07
commit 4ee4a87835
4 changed files with 138 additions and 22 deletions

View File

@ -30,7 +30,11 @@ package net.locusworks.discord.eighttrack.handlers;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -110,15 +114,18 @@ public class DiscordEventHandler extends ListenerAdapter {
GuildMusicHandler gmh = GuildMusicService.getMap().get(event.getGuild().getIdLong()); GuildMusicHandler gmh = GuildMusicService.getMap().get(event.getGuild().getIdLong());
gmh.accept((id) -> GuildMusicService.getMap().remove(id)); gmh.accept((id) -> GuildMusicService.getMap().remove(id));
String command = event.getMessage().getContentRaw().trim().toLowerCase();
List<String> commands = new ArrayList<String>(Arrays.asList(event.getMessage().getContentRaw().trim().toLowerCase().split(" ")));
String command = commands.remove(0);
switch(command) { switch(command) {
case "-upload": case "-upload":
gmh.upload(event); gmh.upload(event);
return; return;
case "-play": case "-play":
gmh.play(event, commands);
gmh.isPlaying(true); gmh.isPlaying(true);
gmh.play(event);
return; return;
case "-stop": case "-stop":
gmh.isPlaying(false); gmh.isPlaying(false);
@ -126,15 +133,20 @@ public class DiscordEventHandler extends ListenerAdapter {
event.getGuild().getAudioManager().closeAudioConnection(); event.getGuild().getAudioManager().closeAudioConnection();
return; return;
case "-next": case "-next":
gmh.next(event); gmh.next(event, commands);
return; return;
case "-repeat": case "-repeat":
gmh.isPlaying(true);
gmh.repeat(event); gmh.repeat(event);
gmh.isPlaying(true);
return; return;
case "-whatsnext": case "-upnext":
gmh.whatsNext(event); gmh.upNext(event, commands);
break; return;
case "-list":
gmh.list(event);
return;
case "-playlist":
gmh.playlist(event, commands);
default: default:
return; return;
} }

View File

@ -40,6 +40,8 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.apache.commons.lang3.StringUtils;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager; import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers; import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
@ -113,7 +115,19 @@ public class GuildMusicHandler {
this.playing.set(playing); this.playing.set(playing);
} }
public void whatsNext(GuildMessageReceivedEvent event) throws Exception { public void upNext(GuildMessageReceivedEvent event) throws Exception {
upNext(event, null);
}
public void upNext(GuildMessageReceivedEvent event, List<String> commands) throws Exception {
if (commands != null && !commands.isEmpty()) {
String uuid = commands.remove(0);
boolean foundSong = findSong(uuid);
if (!foundSong) {
event.getChannel().sendMessageFormat("Unable to find song with identifier: %s", uuid).queue();
return;
}
}
if (ts.peek() == null) { if (ts.peek() == null) {
loadRandomSong(); loadRandomSong();
} }
@ -140,7 +154,15 @@ public class GuildMusicHandler {
} }
public void next(GuildMessageReceivedEvent event) throws Exception { public void next(GuildMessageReceivedEvent event) throws Exception {
next(event, null);
}
public void next(GuildMessageReceivedEvent event, List<String> commands) throws Exception {
if (!playing.get()) {
play(event, commands);
playing.set(true);
return;
}
GuildChannel gc = event.getGuild().getGuildChannelById(ChannelType.VOICE, voiceChannelId); GuildChannel gc = event.getGuild().getGuildChannelById(ChannelType.VOICE, voiceChannelId);
if (gc != null && gc.getMembers().size() == 1) { if (gc != null && gc.getMembers().size() == 1) {
event.getChannel().sendMessage("Going silent since no one is currently listening to the channel").queue(); event.getChannel().sendMessage("Going silent since no one is currently listening to the channel").queue();
@ -151,7 +173,7 @@ public class GuildMusicHandler {
playing.set(false); playing.set(false);
stop(event); stop(event);
play(event); play(event, commands);
playing.set(true); playing.set(true);
} }
@ -161,6 +183,12 @@ public class GuildMusicHandler {
} }
public void play(GuildMessageReceivedEvent event) throws Exception { public void play(GuildMessageReceivedEvent event) throws Exception {
play(event, null);
}
public void play(GuildMessageReceivedEvent event, List<String> commands) throws Exception {
if (playing.get()) return;
VoiceChannel vc = event.getMember().getVoiceState().getChannel(); VoiceChannel vc = event.getMember().getVoiceState().getChannel();
if (vc == null) { if (vc == null) {
event.getChannel().sendMessage(String.format("<@%s> you are not in a voice channel to play music", event.getMember().getId())).queue(); event.getChannel().sendMessage(String.format("<@%s> you are not in a voice channel to play music", event.getMember().getId())).queue();
@ -188,6 +216,16 @@ public class GuildMusicHandler {
manager.setSendingHandler(new EightTrackAudioSendHandler(player)); manager.setSendingHandler(new EightTrackAudioSendHandler(player));
if (commands != null && !commands.isEmpty()) {
String uuid = commands.remove(0);
boolean foundSong = findSong(uuid);
if (!foundSong) {
event.getChannel().sendMessageFormat("Unable to find song with identifier: %s", uuid).queue();
return;
}
stop(event);
}
if (!ts.hasTracks()) { if (!ts.hasTracks()) {
loadRandomSong(); loadRandomSong();
} }
@ -310,16 +348,25 @@ public class GuildMusicHandler {
gs.setDateAdded(new Date()); gs.setDateAdded(new Date());
gs.setGuild(guild); gs.setGuild(guild);
gs.setSong(song); gs.setSong(song);
gs.setUuid(UUID.randomUUID().toString());
String[] uuidArray = UUID.randomUUID().toString().split("-");
gs.setUuid(uuidArray[uuidArray.length - 1]);
guildSongRepoService.getGuildSongRepo().save(gs); guildSongRepoService.getGuildSongRepo().save(gs);
String out = String.format("```%s%n%-10s: %s```", result, "UUID", gs.getUuid());
MessageEmbed embed = new EmbedBuilder() MessageEmbed embed = new EmbedBuilder()
.setColor(Color.GREEN) .setColor(Color.GREEN)
.setThumbnail(event.getGuild().getIconUrl())
.setTitle("Upload Results for " + fileName) .setTitle("Upload Results for " + fileName)
.setDescription(out) .addField("Title", result.getTitle(), true)
.addField("Artist", result.getArtist(), true)
.addField("Album", result.getAlbum(), true)
.addField("Track", result.getTrackNumber(), true)
.addField("Disc", result.getDiscNumber(), true)
.addField("Year", result.getReleaseDate(), true)
.addField("Duration", result.getDurationFormat(), true)
.addField("ID", gs.getUuid(), true)
.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();
@ -327,6 +374,35 @@ public class GuildMusicHandler {
return embed; return embed;
} }
public void list(GuildMessageReceivedEvent event) {
List<GuildSong> gsList = guildSongRepoService.getGuildSongRepo().findByGuild(guildId);
if (gsList == null || gsList.isEmpty()) {
event.getChannel().sendMessage("There is no music for this guild.").queue();
return;
}
int longestSong = 0;
int longestArtist = 0;
for(GuildSong gs : gsList) {
if (gs.getSong().getTitle().length() > longestSong) longestSong = gs.getSong().getTitle().length();
if (gs.getSong().getArtist().length() > longestArtist) longestArtist = gs.getSong().getArtist().length();
}
String fmt = "%6s | %-" + longestSong +"s | %-" + longestArtist +"s | %s%n";
int count = 0;
StringBuilder sb = new StringBuilder();
sb.append("**" + "Currently available songs for " + event.getGuild().getName() + "**\n\n```");
sb.append(String.format(fmt, "Track", "Title", "Artist", "id"));
sb.append(StringUtils.repeat("-", 27 + longestSong + longestArtist)).append("\n");
for(GuildSong gs : gsList) {
sb.append(String.format(fmt, ++count, gs.getSong().getTitle(), gs.getSong().getArtist(), gs.getUuid()));
}
sb.append("```");
event.getChannel().sendMessage(sb.toString()).queue();
}
private void loadRandomSong() throws Exception { private void loadRandomSong() throws Exception {
List<GuildSong> gsList = guildSongRepoService.getGuildSongRepo().findByGuild(guildId); List<GuildSong> gsList = guildSongRepoService.getGuildSongRepo().findByGuild(guildId);
if (gsList == null || gsList.isEmpty()) return; if (gsList == null || gsList.isEmpty()) return;
@ -339,5 +415,29 @@ public class GuildMusicHandler {
apm.loadItem(song.getSong().getFilePath(), ts).get(); apm.loadItem(song.getSong().getFilePath(), ts).get();
} }
private boolean findSong(String uuid) throws Exception {
GuildSong gs = guildSongRepoService.getGuildSongRepo().findByUuid(uuid);
if (gs == null) return false;
apm.loadItem(gs.getSong().getFilePath(), ts).get();
return true;
}
public void playlist(GuildMessageReceivedEvent event, List<String> commands) {
String command = commands.remove(0);
switch(command) {
case "add":
addPlayList(event, commands);
return;
}
} }
private void addPlayList(GuildMessageReceivedEvent event, List<String> commands) {
// TODO Auto-generated method stub
}
}

View File

@ -153,6 +153,10 @@ public class Mp3UploadResults {
return duration; return duration;
} }
public final String getDurationFormat() {
return dateFormat(duration);
}
public boolean validFile() { public boolean validFile() {
return getTitle() != null && !getTitle().isEmpty(); return getTitle() != null && !getTitle().isEmpty();
} }

View File

@ -27,8 +27,8 @@
*/ */
package net.locusworks.discord.eighttrack.scheduler; package net.locusworks.discord.eighttrack.scheduler;
import java.util.Queue; import java.util.Deque;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.Consumer; import java.util.function.Consumer;
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler; import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
@ -42,14 +42,14 @@ import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
public class TrackScheduler extends AudioEventAdapter implements AudioEventListener, AudioLoadResultHandler { public class TrackScheduler extends AudioEventAdapter implements AudioEventListener, AudioLoadResultHandler {
private Queue<AudioTrack> trackQueue; private Deque<AudioTrack> trackQueue;
private boolean started; private boolean started;
private Consumer<AudioTrackEndReason> finished; private Consumer<AudioTrackEndReason> finished;
public TrackScheduler() { public TrackScheduler() {
trackQueue = new LinkedBlockingQueue<AudioTrack>(); trackQueue = new LinkedBlockingDeque<AudioTrack>();
} }
@Override @Override
@ -70,7 +70,7 @@ public class TrackScheduler extends AudioEventAdapter implements AudioEventListe
} }
public void trackLoaded(AudioTrack track) { public void trackLoaded(AudioTrack track) {
trackQueue.add(track); trackQueue.addFirst(track);
} }
public void playlistLoaded(AudioPlaylist playlist) { public void playlistLoaded(AudioPlaylist playlist) {