Reworked how mp3s are being read and loaded int player
This commit is contained in:
		@@ -25,7 +25,7 @@
 | 
			
		||||
 * IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 | 
			
		||||
 * ENHANCEMENTS, OR MODIFICATIONS.
 | 
			
		||||
 */
 | 
			
		||||
package net.locusworks.discord.eighttrack.handlers;
 | 
			
		||||
package net.locusworks.discord.eighttrack.audio;
 | 
			
		||||
 | 
			
		||||
import java.nio.ByteBuffer;
 | 
			
		||||
 | 
			
		||||
@@ -25,23 +25,22 @@
 | 
			
		||||
 * IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 | 
			
		||||
 * ENHANCEMENTS, OR MODIFICATIONS.
 | 
			
		||||
 */
 | 
			
		||||
package net.locusworks.discord.eighttrack.handlers;
 | 
			
		||||
package net.locusworks.discord.eighttrack.audio;
 | 
			
		||||
 | 
			
		||||
import java.io.ByteArrayInputStream;
 | 
			
		||||
import java.io.ByteArrayOutputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.nio.file.Files;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
import java.nio.file.Paths;
 | 
			
		||||
 | 
			
		||||
import org.apache.tika.exception.TikaException;
 | 
			
		||||
import org.apache.tika.metadata.Metadata;
 | 
			
		||||
import org.apache.tika.parser.ParseContext;
 | 
			
		||||
import org.apache.tika.parser.mp3.Mp3Parser;
 | 
			
		||||
import org.apache.tika.sax.BodyContentHandler;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
import org.xml.sax.SAXException;
 | 
			
		||||
import com.mpatric.mp3agic.ID3v2;
 | 
			
		||||
import com.mpatric.mp3agic.InvalidDataException;
 | 
			
		||||
import com.mpatric.mp3agic.Mp3File;
 | 
			
		||||
import com.mpatric.mp3agic.UnsupportedTagException;
 | 
			
		||||
 | 
			
		||||
@Component
 | 
			
		||||
public class Mp3UploadHandler {
 | 
			
		||||
@@ -53,6 +52,7 @@ public class Mp3UploadHandler {
 | 
			
		||||
 | 
			
		||||
  public Mp3UploadResults parse(InputStream is) throws IOException {
 | 
			
		||||
    byte[] data;
 | 
			
		||||
    String id = null;
 | 
			
		||||
    try(InputStream in = is; ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
 | 
			
		||||
      int read = 0;
 | 
			
		||||
      byte[] buffer = new byte[1024 * 1024];
 | 
			
		||||
@@ -60,29 +60,28 @@ public class Mp3UploadHandler {
 | 
			
		||||
        baos.write(buffer, 0, read);
 | 
			
		||||
      }
 | 
			
		||||
      data = baos.toByteArray();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    try (InputStream in = new ByteArrayInputStream(data)) {
 | 
			
		||||
      BodyContentHandler handler = new BodyContentHandler();
 | 
			
		||||
      Metadata metadata = new Metadata();
 | 
			
		||||
      ParseContext context = new ParseContext();
 | 
			
		||||
 | 
			
		||||
      Mp3Parser parser = new Mp3Parser();
 | 
			
		||||
 | 
			
		||||
      parser.parse(in, handler, metadata, context);
 | 
			
		||||
      
 | 
			
		||||
      return new Mp3UploadResults(metadata, data);
 | 
			
		||||
      id = UUID.nameUUIDFromBytes(data).toString();
 | 
			
		||||
      Files.write(Paths.get(id), data);
 | 
			
		||||
      Mp3File mp3 = new Mp3File(Paths.get(id));
 | 
			
		||||
      ID3v2 tag = mp3.getId3v2Tag();
 | 
			
		||||
      
 | 
			
		||||
      return new Mp3UploadResults(tag, data);
 | 
			
		||||
    } catch (Exception ex) {
 | 
			
		||||
      throw new IOException(ex);
 | 
			
		||||
      return new Mp3UploadResults(ex);
 | 
			
		||||
    } finally {
 | 
			
		||||
      if (id != null) {
 | 
			
		||||
        Files.deleteIfExists(Paths.get(id));
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public static void main(String args[]) throws IOException, SAXException, TikaException {
 | 
			
		||||
    String file = "E:\\Music2\\Alan Walker\\Itinerary_ Dallas.pdf";
 | 
			
		||||
  public static void main(String args[]) throws IOException, SAXException, UnsupportedTagException, InvalidDataException {
 | 
			
		||||
    String file = "E:\\Music2\\AronChupa\\01 I'm an Albatraoz.mp3";
 | 
			
		||||
    Path path = Paths.get(file);
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    Mp3UploadHandler fuh = new Mp3UploadHandler();
 | 
			
		||||
    fuh.parse(path);
 | 
			
		||||
    System.out.println(fuh.parse(path));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -25,34 +25,12 @@
 | 
			
		||||
 * IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 | 
			
		||||
 * ENHANCEMENTS, OR MODIFICATIONS.
 | 
			
		||||
 */
 | 
			
		||||
package net.locusworks.discord.eighttrack.handlers;
 | 
			
		||||
package net.locusworks.discord.eighttrack.audio;
 | 
			
		||||
 | 
			
		||||
import org.apache.tika.metadata.Metadata;
 | 
			
		||||
import com.mpatric.mp3agic.ID3v2;
 | 
			
		||||
 | 
			
		||||
public class Mp3UploadResults {
 | 
			
		||||
  
 | 
			
		||||
  private enum MetaDataField {
 | 
			
		||||
    GENRE("xmpDM:genre"),
 | 
			
		||||
    COMPOSER("xmpDM:composer"),
 | 
			
		||||
    ALBUM("xmpDM:album"),
 | 
			
		||||
    TRACK_NUMBER("xmpDM:trackNumber"),
 | 
			
		||||
    DISC_NUMBER("xmpDM:discNumber"),
 | 
			
		||||
    ARTIST("xmpDM:artist"),
 | 
			
		||||
    TITLE("title"),
 | 
			
		||||
    RELEASE_DATE("xmpDM:releaseDate"),
 | 
			
		||||
    DURATION("xmpDM:duration");
 | 
			
		||||
    
 | 
			
		||||
    private String value;
 | 
			
		||||
    
 | 
			
		||||
    private MetaDataField(String value) {
 | 
			
		||||
      this.value = value;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public String getValue() {
 | 
			
		||||
      return this.value;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  private String genre;
 | 
			
		||||
  private String composure;
 | 
			
		||||
  private String album;
 | 
			
		||||
@@ -61,29 +39,28 @@ public class Mp3UploadResults {
 | 
			
		||||
  private String artist;
 | 
			
		||||
  private String title;
 | 
			
		||||
  private String releaseDate;
 | 
			
		||||
  private Long duration;
 | 
			
		||||
  
 | 
			
		||||
  private byte[] rawData;
 | 
			
		||||
  private Throwable err;
 | 
			
		||||
  
 | 
			
		||||
  public Mp3UploadResults( Metadata metadata, byte[] data) {
 | 
			
		||||
    parseResults(metadata);
 | 
			
		||||
  public Mp3UploadResults(ID3v2 tag, byte[] data) {
 | 
			
		||||
    parseResults(tag);
 | 
			
		||||
    this.rawData = data;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public Mp3UploadResults(Throwable err) {
 | 
			
		||||
    this.err = err;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private void parseResults(Metadata metadata) {
 | 
			
		||||
    genre = metadata.get(MetaDataField.GENRE.getValue());
 | 
			
		||||
    composure = metadata.get(MetaDataField.COMPOSER.getValue());
 | 
			
		||||
    album = metadata.get(MetaDataField.ALBUM.getValue());
 | 
			
		||||
    trackNumber = metadata.get(MetaDataField.TRACK_NUMBER.getValue());
 | 
			
		||||
    discNumber = metadata.get(MetaDataField.DISC_NUMBER.getValue());
 | 
			
		||||
    artist = metadata.get(MetaDataField.ARTIST.getValue());
 | 
			
		||||
    title = metadata.get(MetaDataField.TITLE.getValue());
 | 
			
		||||
    releaseDate = metadata.get(MetaDataField.RELEASE_DATE.getValue());
 | 
			
		||||
    
 | 
			
		||||
    String durationStr = metadata.get(MetaDataField.DURATION.getValue()).trim();
 | 
			
		||||
    if (durationStr != null) {
 | 
			
		||||
      duration = Double.valueOf(metadata.get(MetaDataField.DURATION.getValue())).longValue();
 | 
			
		||||
    }
 | 
			
		||||
  private void parseResults(ID3v2 metadata) {
 | 
			
		||||
    genre = metadata.getGenreDescription();
 | 
			
		||||
    composure = metadata.getComposer();
 | 
			
		||||
    album = metadata.getAlbum();
 | 
			
		||||
    trackNumber = metadata.getTrack();
 | 
			
		||||
    discNumber = metadata.getPartOfSet();
 | 
			
		||||
    artist = metadata.getArtist();
 | 
			
		||||
    title = metadata.getTitle();
 | 
			
		||||
    releaseDate = metadata.getDate();
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public final byte[] getData() {
 | 
			
		||||
@@ -146,23 +123,24 @@ public class Mp3UploadResults {
 | 
			
		||||
    return releaseDate;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @return the duration
 | 
			
		||||
   */
 | 
			
		||||
  public final Long getDuration() {
 | 
			
		||||
    return duration;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public final String getDurationFormat() {
 | 
			
		||||
    return dateFormat(duration);
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public boolean validFile() {
 | 
			
		||||
    return getTitle() != null && !getTitle().isEmpty();
 | 
			
		||||
    return (getTitle() != null && !getTitle().isEmpty() && getArtist() != null && !getArtist().trim().isEmpty()) || this.err != null;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public boolean hasError() {
 | 
			
		||||
    return this.err != null;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public Throwable getError() {
 | 
			
		||||
    return this.err;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  @Override
 | 
			
		||||
  public String toString() {
 | 
			
		||||
    if (this.hasError()) {
 | 
			
		||||
      return "Unable to parse file due to exception: " + this.err.getMessage();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    StringBuilder sb = new StringBuilder();
 | 
			
		||||
    if (getTitle() != null)
 | 
			
		||||
      sb.append(String.format("%-10s: %s%n", "Title", getTitle()));
 | 
			
		||||
@@ -185,32 +163,10 @@ public class Mp3UploadResults {
 | 
			
		||||
    if (getGenre() != null)
 | 
			
		||||
      sb.append(String.format("%-10s: %s%n", "Genre", getGenre()));
 | 
			
		||||
    
 | 
			
		||||
    if (duration != null)
 | 
			
		||||
      sb.append(String.format("%-10s: %s%n", "Duration", dateFormat(getDuration())));
 | 
			
		||||
    
 | 
			
		||||
    return sb.toString();
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public void clear() {
 | 
			
		||||
    rawData = new byte[0];
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  private String dateFormat(long timeInMilliSeconds) {
 | 
			
		||||
    long seconds = timeInMilliSeconds / 1000;
 | 
			
		||||
    long minutes = seconds / 60;
 | 
			
		||||
    long hours = minutes / 60;
 | 
			
		||||
    long days = hours / 24;
 | 
			
		||||
    
 | 
			
		||||
    long sec = seconds % 60;
 | 
			
		||||
    long min =  minutes % 60;
 | 
			
		||||
    long hr  = hours % 24;
 | 
			
		||||
    
 | 
			
		||||
    String time = "" + sec;
 | 
			
		||||
    if (min > 0) time = min + ":" + time;
 | 
			
		||||
    if (hr > 0) time = hr + ":" + time;
 | 
			
		||||
    if (days > 0) time = days + ":" + time;
 | 
			
		||||
    
 | 
			
		||||
    return time;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,74 @@
 | 
			
		||||
package net.locusworks.discord.eighttrack.audio;
 | 
			
		||||
 | 
			
		||||
import java.lang.reflect.Field;
 | 
			
		||||
import java.util.concurrent.ExecutionException;
 | 
			
		||||
import java.util.concurrent.LinkedBlockingDeque;
 | 
			
		||||
import java.util.function.Consumer;
 | 
			
		||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
 | 
			
		||||
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayer;
 | 
			
		||||
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
 | 
			
		||||
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
 | 
			
		||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
 | 
			
		||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
 | 
			
		||||
 | 
			
		||||
public class TrackManager extends DefaultAudioPlayer  {
 | 
			
		||||
  
 | 
			
		||||
  private LinkedBlockingDeque<String> queue;
 | 
			
		||||
  private AudioPlayerManager playerManager;
 | 
			
		||||
  private TrackScheduler trackScheduler;
 | 
			
		||||
  private String currentItem;
 | 
			
		||||
  
 | 
			
		||||
  public TrackManager() {
 | 
			
		||||
    super(new DefaultAudioPlayerManager());
 | 
			
		||||
    this.queue = new LinkedBlockingDeque<String>();
 | 
			
		||||
    this.trackScheduler = new TrackScheduler();
 | 
			
		||||
    
 | 
			
		||||
    addListener(trackScheduler);
 | 
			
		||||
    
 | 
			
		||||
    try {
 | 
			
		||||
        Field f = this.getClass().getSuperclass().getDeclaredField("manager");
 | 
			
		||||
        f.setAccessible(true);
 | 
			
		||||
        this.playerManager = (AudioPlayerManager) f.get(this);
 | 
			
		||||
    } catch (SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
 | 
			
		||||
      throw new IllegalArgumentException(e);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    AudioSourceManagers.registerLocalSource(this.playerManager);
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public void queueLast(String track) {
 | 
			
		||||
    queue.addLast(track);
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public void queueNext(String track) {
 | 
			
		||||
    queue.addFirst(track);
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public boolean hasTracks() {
 | 
			
		||||
    return queue.peek() != null && !queue.isEmpty();
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public String getCurrentItem() {
 | 
			
		||||
    return currentItem;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public AudioTrack play() {
 | 
			
		||||
    if (!queue.isEmpty()) {
 | 
			
		||||
      try {
 | 
			
		||||
        currentItem = queue.poll();
 | 
			
		||||
        playerManager.loadItem(currentItem, trackScheduler).get();
 | 
			
		||||
        return trackScheduler.getNextTrack();
 | 
			
		||||
      } catch (InterruptedException | ExecutionException e) {}
 | 
			
		||||
    }
 | 
			
		||||
    return null;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public void setFinishedCallback(Consumer<AudioTrackEndReason> callback) {
 | 
			
		||||
    trackScheduler.setFinishedCallback(callback);
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public void clearTracks() {
 | 
			
		||||
    queue.clear();
 | 
			
		||||
    trackScheduler.clearTracks();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -25,7 +25,7 @@
 | 
			
		||||
 * IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 | 
			
		||||
 * ENHANCEMENTS, OR MODIFICATIONS.
 | 
			
		||||
 */
 | 
			
		||||
package net.locusworks.discord.eighttrack.scheduler;
 | 
			
		||||
package net.locusworks.discord.eighttrack.audio;
 | 
			
		||||
 | 
			
		||||
import java.util.Deque;
 | 
			
		||||
import java.util.concurrent.LinkedBlockingDeque;
 | 
			
		||||
@@ -1,5 +0,0 @@
 | 
			
		||||
package net.locusworks.discord.eighttrack.enums;
 | 
			
		||||
 | 
			
		||||
public enum UserCommands {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -47,6 +47,7 @@ import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
 | 
			
		||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
 | 
			
		||||
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
 | 
			
		||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
 | 
			
		||||
import net.locusworks.discord.eighttrack.audio.Mp3UploadHandler;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
 | 
			
		||||
import net.locusworks.discord.eighttrack.services.ConfigurationService;
 | 
			
		||||
import net.locusworks.discord.eighttrack.services.GuildMusicService;
 | 
			
		||||
@@ -147,7 +148,7 @@ public class DiscordEventHandler extends ListenerAdapter {
 | 
			
		||||
    String command = commands.remove(0);
 | 
			
		||||
    
 | 
			
		||||
    switch(command) {
 | 
			
		||||
      case "-upload":
 | 
			
		||||
      case "+upload":
 | 
			
		||||
        gmh.upload(event);
 | 
			
		||||
        return;
 | 
			
		||||
      case "-play":
 | 
			
		||||
 
 | 
			
		||||
@@ -29,7 +29,6 @@ package net.locusworks.discord.eighttrack.handlers;
 | 
			
		||||
 | 
			
		||||
import java.awt.Color;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.math.BigInteger;
 | 
			
		||||
import java.nio.file.Files;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
import java.time.OffsetDateTime;
 | 
			
		||||
@@ -46,14 +45,12 @@ import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
 | 
			
		||||
 | 
			
		||||
import net.dv8tion.jda.api.EmbedBuilder;
 | 
			
		||||
import net.dv8tion.jda.api.Permission;
 | 
			
		||||
import net.dv8tion.jda.api.entities.ChannelType;
 | 
			
		||||
import net.dv8tion.jda.api.entities.Guild;
 | 
			
		||||
import net.dv8tion.jda.api.entities.GuildChannel;
 | 
			
		||||
import net.dv8tion.jda.api.entities.Message;
 | 
			
		||||
import net.dv8tion.jda.api.entities.Message.Attachment;
 | 
			
		||||
@@ -64,13 +61,16 @@ import net.dv8tion.jda.api.exceptions.ErrorResponseException;
 | 
			
		||||
import net.dv8tion.jda.api.managers.AudioManager;
 | 
			
		||||
import net.dv8tion.jda.api.requests.ErrorResponse;
 | 
			
		||||
import net.locusworks.crypto.utils.HashUtils;
 | 
			
		||||
import net.locusworks.discord.eighttrack.audio.EightTrackAudioSendHandler;
 | 
			
		||||
import net.locusworks.discord.eighttrack.audio.Mp3UploadHandler;
 | 
			
		||||
import net.locusworks.discord.eighttrack.audio.Mp3UploadResults;
 | 
			
		||||
import net.locusworks.discord.eighttrack.audio.TrackManager;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylistSong;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.Song;
 | 
			
		||||
import net.locusworks.discord.eighttrack.listeners.ReactionListener;
 | 
			
		||||
import net.locusworks.discord.eighttrack.scheduler.TrackScheduler;
 | 
			
		||||
import net.locusworks.discord.eighttrack.services.GuildSongRepoService;
 | 
			
		||||
import net.locusworks.discord.eighttrack.utils.Reactions;
 | 
			
		||||
import net.locusworks.logger.ApplicationLogger;
 | 
			
		||||
@@ -79,10 +79,7 @@ import net.locusworks.logger.ApplicationLoggerFactory;
 | 
			
		||||
public class GuildMusicHandler {
 | 
			
		||||
 | 
			
		||||
  private Path musicDir;
 | 
			
		||||
  private TrackScheduler ts;
 | 
			
		||||
  private DefaultAudioPlayerManager apm;
 | 
			
		||||
  private ApplicationLogger logger;
 | 
			
		||||
  private AudioPlayer player;
 | 
			
		||||
  private AtomicBoolean playing;
 | 
			
		||||
  private Long voiceChannelId;
 | 
			
		||||
 | 
			
		||||
@@ -95,6 +92,8 @@ public class GuildMusicHandler {
 | 
			
		||||
  private long guildId;
 | 
			
		||||
 | 
			
		||||
  private GuildPlaylist currentPlaylist;
 | 
			
		||||
  
 | 
			
		||||
  private TrackManager trackManager;
 | 
			
		||||
 | 
			
		||||
  public GuildMusicHandler(Path musicDir, long guildId, Mp3UploadHandler uploadHandler, ReactionHandler reactionHandler, GuildSongRepoService guildSongRepoService) throws IOException {
 | 
			
		||||
    this.logger = ApplicationLoggerFactory.getLogger(GuildMusicHandler.class);
 | 
			
		||||
@@ -105,13 +104,7 @@ public class GuildMusicHandler {
 | 
			
		||||
    this.guildSongRepoService = guildSongRepoService;
 | 
			
		||||
    this.guildId = guildId;
 | 
			
		||||
    this.reactionHandler = reactionHandler;
 | 
			
		||||
    this.apm = new DefaultAudioPlayerManager();
 | 
			
		||||
 | 
			
		||||
    AudioSourceManagers.registerLocalSource(apm);
 | 
			
		||||
    this.player = apm.createPlayer();
 | 
			
		||||
 | 
			
		||||
    this.ts = new TrackScheduler();
 | 
			
		||||
    player.addListener(ts);
 | 
			
		||||
    this.trackManager = new TrackManager();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void playlist(GuildMessageReceivedEvent event, List<String> commands) throws Exception {
 | 
			
		||||
@@ -168,10 +161,10 @@ public class GuildMusicHandler {
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    if (ts.peek() == null) {
 | 
			
		||||
    if (!trackManager.hasTracks()) {
 | 
			
		||||
      loadRandomSong();
 | 
			
		||||
    }
 | 
			
		||||
    AudioTrack track = ts.peek();
 | 
			
		||||
    AudioTrack track = trackManager.play();
 | 
			
		||||
    MessageEmbed embed = new EmbedBuilder()
 | 
			
		||||
        .setAuthor(event.getMember().getEffectiveName(), null, event.getAuthor().getAvatarUrl())
 | 
			
		||||
        .setTitle("Next Up:")
 | 
			
		||||
@@ -183,7 +176,7 @@ public class GuildMusicHandler {
 | 
			
		||||
 | 
			
		||||
  public void repeat(GuildMessageReceivedEvent event) throws Exception {
 | 
			
		||||
    next(event);
 | 
			
		||||
    ts.setFinishedCallback((ater) -> {
 | 
			
		||||
    trackManager.setFinishedCallback((ater) -> {
 | 
			
		||||
      if (!playing.get()) return;
 | 
			
		||||
      try {
 | 
			
		||||
        next(event);
 | 
			
		||||
@@ -222,7 +215,7 @@ public class GuildMusicHandler {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void stop(GuildMessageReceivedEvent event, boolean stoppedFromRepeat) {
 | 
			
		||||
    player.stopTrack();
 | 
			
		||||
    trackManager.stopTrack();
 | 
			
		||||
    voiceChannelId = null;
 | 
			
		||||
    if (!stoppedFromRepeat) {
 | 
			
		||||
      currentPlaylist = null;
 | 
			
		||||
@@ -261,7 +254,7 @@ public class GuildMusicHandler {
 | 
			
		||||
    AudioManager manager = event.getGuild().getAudioManager();
 | 
			
		||||
    manager.openAudioConnection(vc);
 | 
			
		||||
 | 
			
		||||
    manager.setSendingHandler(new EightTrackAudioSendHandler(player));
 | 
			
		||||
    manager.setSendingHandler(new EightTrackAudioSendHandler(trackManager));
 | 
			
		||||
 | 
			
		||||
    if (commands != null && !commands.isEmpty()) {
 | 
			
		||||
      String uuid = commands.remove(0);
 | 
			
		||||
@@ -273,14 +266,18 @@ public class GuildMusicHandler {
 | 
			
		||||
      stop(event, true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!ts.hasTracks()) {
 | 
			
		||||
    if (!trackManager.hasTracks()) {
 | 
			
		||||
      loadRandomSong();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (ts.hasTracks()) {
 | 
			
		||||
    if (trackManager.hasTracks()) {
 | 
			
		||||
      lastPlayed = OffsetDateTime.now(); 
 | 
			
		||||
 | 
			
		||||
      AudioTrack track = ts.getNextTrack();
 | 
			
		||||
      AudioTrack track = trackManager.play();
 | 
			
		||||
      if (track == null) {
 | 
			
		||||
        logger.warn("Unable to find track: " + trackManager.getCurrentItem()) ;
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      MessageEmbed embed = new EmbedBuilder()
 | 
			
		||||
          .setAuthor(event.getMember().getEffectiveName(), null, event.getAuthor().getAvatarUrl())
 | 
			
		||||
@@ -290,7 +287,7 @@ public class GuildMusicHandler {
 | 
			
		||||
          .build();
 | 
			
		||||
 | 
			
		||||
      event.getChannel().sendMessage(embed).queue();
 | 
			
		||||
      player.playTrack(track);
 | 
			
		||||
      trackManager.playTrack(track);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -358,10 +355,19 @@ public class GuildMusicHandler {
 | 
			
		||||
    sb.append("```");
 | 
			
		||||
    event.getChannel().sendMessage(sb.toString()).queue();
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  public void adminPlaylist(GuildMessageReceivedEvent event, List<String> commands) {
 | 
			
		||||
    if(!isAdmin(event)) return;
 | 
			
		||||
    if (!checkCommands(event, commands, event.getMember().getAsMention() + ", valid commands are +playst list, play, delete")) return;
 | 
			
		||||
    
 | 
			
		||||
    String command = commands.remove(0);
 | 
			
		||||
    switch (command) {
 | 
			
		||||
      case "list":
 | 
			
		||||
        listAllPlaylists(event);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void delete(GuildMessageReceivedEvent event, List<String> commands) {
 | 
			
		||||
@@ -393,11 +399,11 @@ public class GuildMusicHandler {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ts.clearTracks();
 | 
			
		||||
    trackManager.clearTracks();
 | 
			
		||||
 | 
			
		||||
    for(GuildPlaylistSong gpls : guildSongRepoService.getGuildPlaylistSongRepo().findByGuildPlaylist(currentPlaylist)) {
 | 
			
		||||
      Song s = gpls.getGuildSong().getSong();
 | 
			
		||||
      apm.loadItem(s.getFilePath(), ts).get();
 | 
			
		||||
      trackManager.queueLast(s.getFilePath());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    playing.set(false);
 | 
			
		||||
@@ -433,7 +439,7 @@ public class GuildMusicHandler {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    String hash = HashUtils.hash("SHA-1", result.getData());
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    GuildSong gs = guildSongRepoService.getGuildSongRepo().findByGuildAndSongHash(event.getGuild().getIdLong(), hash);
 | 
			
		||||
    if (gs != null) {
 | 
			
		||||
      MessageEmbed embed = new EmbedBuilder()
 | 
			
		||||
@@ -446,7 +452,7 @@ public class GuildMusicHandler {
 | 
			
		||||
          .build();
 | 
			
		||||
      return embed;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    Song song = guildSongRepoService.getSongRepo().findByFileHash(hash);
 | 
			
		||||
    if (song == null) {
 | 
			
		||||
@@ -471,8 +477,6 @@ public class GuildMusicHandler {
 | 
			
		||||
      song.setArtist(result.getArtist());
 | 
			
		||||
      song.setDateAdded(new Date());
 | 
			
		||||
      song.setDiscNumber(result.getDiscNumber());
 | 
			
		||||
      if (result.getDuration() != null)
 | 
			
		||||
        song.setDuration(BigInteger.valueOf(result.getDuration()));
 | 
			
		||||
      song.setFileHash(hash);
 | 
			
		||||
      song.setFilePath(output.toAbsolutePath().toString());
 | 
			
		||||
      song.setGenre(result.getGenre());
 | 
			
		||||
@@ -494,21 +498,23 @@ public class GuildMusicHandler {
 | 
			
		||||
 | 
			
		||||
    guildSongRepoService.getGuildSongRepo().save(gs);
 | 
			
		||||
 | 
			
		||||
    MessageEmbed embed = new EmbedBuilder()
 | 
			
		||||
    EmbedBuilder builder = new EmbedBuilder()
 | 
			
		||||
        .setColor(Color.GREEN)
 | 
			
		||||
        .setThumbnail(event.getGuild().getIconUrl())
 | 
			
		||||
        .setTitle("Upload Results for " + fileName)
 | 
			
		||||
        .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();
 | 
			
		||||
        .addField("Title", result.getTitle(), true);
 | 
			
		||||
 | 
			
		||||
    if (!StringUtils.isBlank(result.getArtist()))         builder.addField("Artist", result.getArtist(), true);
 | 
			
		||||
    if (!StringUtils.isBlank(result.getAlbum())) 		      builder.addField("Album", result.getAlbum(), true);
 | 
			
		||||
    if (!StringUtils.isBlank(result.getTrackNumber())) 	  builder.addField("Track", result.getTrackNumber(), true);
 | 
			
		||||
    if (!StringUtils.isBlank(result.getDiscNumber())) 	  builder.addField("Disc", result.getDiscNumber(), true);
 | 
			
		||||
    if (!StringUtils.isBlank(result.getReleaseDate()))    builder.addField("Year", result.getReleaseDate(), true);
 | 
			
		||||
 | 
			
		||||
    builder.addField("ID", gs.getUuid(), true);
 | 
			
		||||
 | 
			
		||||
    MessageEmbed embed = builder.build();
 | 
			
		||||
 | 
			
		||||
    return embed;
 | 
			
		||||
  } 
 | 
			
		||||
@@ -615,10 +621,16 @@ public class GuildMusicHandler {
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  private void listAllPlaylists(GuildMessageReceivedEvent event) {
 | 
			
		||||
    // TODO Auto-generated method stub
 | 
			
		||||
    
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private void deleteUserPlayList(GuildMessageReceivedEvent event, List<String> commands) {
 | 
			
		||||
    // TODO Auto-generated method stub
 | 
			
		||||
    System.out.println(commands.stream().collect(Collectors.joining(", ")));
 | 
			
		||||
    Guild guild = event.getGuild();
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private void deleteGuildSongsConfirm(List<GuildSong> songs, String user, Message msg) {
 | 
			
		||||
@@ -823,19 +835,17 @@ public class GuildMusicHandler {
 | 
			
		||||
    int item = random.nextInt(gsList.size());
 | 
			
		||||
 | 
			
		||||
    GuildSong song = gsList.get(item);
 | 
			
		||||
 | 
			
		||||
    apm.loadItem(song.getSong().getFilePath(), ts).get();
 | 
			
		||||
    trackManager.queueNext(song.getSong().getFilePath());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  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();
 | 
			
		||||
 | 
			
		||||
    trackManager.queueNext(gs.getSong().getFilePath());
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  private boolean isAdmin(GuildMessageReceivedEvent event) {
 | 
			
		||||
    if (!event.getMessage().getMember().hasPermission(Permission.ADMINISTRATOR, Permission.MANAGE_SERVER)) {
 | 
			
		||||
      event.getChannel().sendMessage(String.format("Sorry <@%s> i can't do that. *psst: you have to have manage server, or administrator role*", event.getMember().getIdLong())).queue();
 | 
			
		||||
@@ -843,7 +853,7 @@ public class GuildMusicHandler {
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  private boolean checkCommands(GuildMessageReceivedEvent event, List<String> commands, String msg) {
 | 
			
		||||
    if (commands == null || commands.isEmpty()) {
 | 
			
		||||
      event.getChannel().sendMessage(msg).queue();
 | 
			
		||||
@@ -860,5 +870,5 @@ public class GuildMusicHandler {
 | 
			
		||||
      if (ex.getErrorResponse() != ErrorResponse.UNKNOWN_MESSAGE) throw ex;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -74,7 +74,5 @@ public class EightTrackLauncher implements ApplicationRunner {
 | 
			
		||||
    logger.info("Starting Eight-Track");
 | 
			
		||||
    
 | 
			
		||||
    service.begin();
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -117,7 +117,7 @@ public class ConfigurationService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (changed) {
 | 
			
		||||
      PropertiesManager.saveConfiguration(configuration, eightTrackConf, "Patch Repository properties file");
 | 
			
		||||
      PropertiesManager.saveConfiguration(configuration, eightTrackConf, "Eight Track properties file");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    lastModified = Files.getLastModifiedTime(eightTrackConf).toMillis();
 | 
			
		||||
@@ -133,7 +133,7 @@ public class ConfigurationService {
 | 
			
		||||
   * @throws Exception general exception
 | 
			
		||||
   */
 | 
			
		||||
  public void saveToConf(Properties confs) throws Exception {
 | 
			
		||||
    PropertiesManager.saveConfiguration(confs, eightTrackConf, "Patch Repository properties file");
 | 
			
		||||
    PropertiesManager.saveConfiguration(confs, eightTrackConf, "Eight Track properties file");
 | 
			
		||||
    logger.info("Saved config file: " + eightTrackConf + ", " + confs.size() + " entries");
 | 
			
		||||
    loadConf();
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user