updated functionality
#13 Added functionality to allow players to play a song by id
This commit is contained in:
@ -30,7 +30,11 @@ package net.locusworks.discord.eighttrack.handlers;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -109,16 +113,19 @@ public class DiscordEventHandler extends ListenerAdapter {
|
||||
|
||||
GuildMusicHandler gmh = GuildMusicService.getMap().get(event.getGuild().getIdLong());
|
||||
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) {
|
||||
case "-upload":
|
||||
gmh.upload(event);
|
||||
return;
|
||||
case "-play":
|
||||
gmh.play(event, commands);
|
||||
gmh.isPlaying(true);
|
||||
gmh.play(event);
|
||||
return;
|
||||
case "-stop":
|
||||
gmh.isPlaying(false);
|
||||
@ -126,15 +133,20 @@ public class DiscordEventHandler extends ListenerAdapter {
|
||||
event.getGuild().getAudioManager().closeAudioConnection();
|
||||
return;
|
||||
case "-next":
|
||||
gmh.next(event);
|
||||
gmh.next(event, commands);
|
||||
return;
|
||||
case "-repeat":
|
||||
gmh.isPlaying(true);
|
||||
gmh.repeat(event);
|
||||
gmh.isPlaying(true);
|
||||
return;
|
||||
case "-whatsnext":
|
||||
gmh.whatsNext(event);
|
||||
break;
|
||||
case "-upnext":
|
||||
gmh.upNext(event, commands);
|
||||
return;
|
||||
case "-list":
|
||||
gmh.list(event);
|
||||
return;
|
||||
case "-playlist":
|
||||
gmh.playlist(event, commands);
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
@ -40,6 +40,8 @@ import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
||||
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
|
||||
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
|
||||
@ -112,8 +114,20 @@ public class GuildMusicHandler {
|
||||
public void isPlaying(boolean playing) {
|
||||
this.playing.set(playing);
|
||||
}
|
||||
|
||||
public void upNext(GuildMessageReceivedEvent event) throws Exception {
|
||||
upNext(event, null);
|
||||
}
|
||||
|
||||
public void whatsNext(GuildMessageReceivedEvent event) throws Exception {
|
||||
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) {
|
||||
loadRandomSong();
|
||||
}
|
||||
@ -138,9 +152,17 @@ public class GuildMusicHandler {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
if (gc != null && gc.getMembers().size() == 1) {
|
||||
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);
|
||||
stop(event);
|
||||
play(event);
|
||||
play(event, commands);
|
||||
playing.set(true);
|
||||
}
|
||||
|
||||
@ -159,8 +181,14 @@ public class GuildMusicHandler {
|
||||
player.stopTrack();
|
||||
voiceChannelId = null;
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
if (vc == null) {
|
||||
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));
|
||||
|
||||
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()) {
|
||||
loadRandomSong();
|
||||
}
|
||||
@ -310,16 +348,25 @@ public class GuildMusicHandler {
|
||||
gs.setDateAdded(new Date());
|
||||
gs.setGuild(guild);
|
||||
gs.setSong(song);
|
||||
gs.setUuid(UUID.randomUUID().toString());
|
||||
|
||||
String[] uuidArray = UUID.randomUUID().toString().split("-");
|
||||
|
||||
gs.setUuid(uuidArray[uuidArray.length - 1]);
|
||||
|
||||
guildSongRepoService.getGuildSongRepo().save(gs);
|
||||
|
||||
String out = String.format("```%s%n%-10s: %s```", result, "UUID", gs.getUuid());
|
||||
|
||||
MessageEmbed embed = new EmbedBuilder()
|
||||
.setColor(Color.GREEN)
|
||||
.setThumbnail(event.getGuild().getIconUrl())
|
||||
.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())
|
||||
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
||||
.build();
|
||||
@ -327,6 +374,35 @@ public class GuildMusicHandler {
|
||||
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 {
|
||||
List<GuildSong> gsList = guildSongRepoService.getGuildSongRepo().findByGuild(guildId);
|
||||
if (gsList == null || gsList.isEmpty()) return;
|
||||
@ -339,5 +415,29 @@ public class GuildMusicHandler {
|
||||
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
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -153,6 +153,10 @@ public class Mp3UploadResults {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public final String getDurationFormat() {
|
||||
return dateFormat(duration);
|
||||
}
|
||||
|
||||
public boolean validFile() {
|
||||
return getTitle() != null && !getTitle().isEmpty();
|
||||
}
|
||||
|
@ -27,8 +27,8 @@
|
||||
*/
|
||||
package net.locusworks.discord.eighttrack.scheduler;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.Deque;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
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 {
|
||||
|
||||
private Queue<AudioTrack> trackQueue;
|
||||
private Deque<AudioTrack> trackQueue;
|
||||
|
||||
private boolean started;
|
||||
|
||||
private Consumer<AudioTrackEndReason> finished;
|
||||
|
||||
public TrackScheduler() {
|
||||
trackQueue = new LinkedBlockingQueue<AudioTrack>();
|
||||
trackQueue = new LinkedBlockingDeque<AudioTrack>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -70,7 +70,7 @@ public class TrackScheduler extends AudioEventAdapter implements AudioEventListe
|
||||
}
|
||||
|
||||
public void trackLoaded(AudioTrack track) {
|
||||
trackQueue.add(track);
|
||||
trackQueue.addFirst(track);
|
||||
}
|
||||
|
||||
public void playlistLoaded(AudioPlaylist playlist) {
|
||||
|
Reference in New Issue
Block a user