2017-10-22 23:48:13 +02:00
package net.Broken.audio ;
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler ;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager ;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager ;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers ;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException ;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist ;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack ;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo ;
import net.Broken.MainBot ;
import net.Broken.Outils.EmbedMessageUtils ;
import net.Broken.Outils.MessageTimeOut ;
import net.dv8tion.jda.core.entities.Guild ;
import net.dv8tion.jda.core.entities.Message ;
import net.dv8tion.jda.core.entities.TextChannel ;
import net.dv8tion.jda.core.entities.VoiceChannel ;
2017-11-06 16:22:15 +01:00
import net.dv8tion.jda.core.events.guild.voice.GuildVoiceLeaveEvent ;
2017-10-22 23:48:13 +02:00
import net.dv8tion.jda.core.events.message.MessageReceivedEvent ;
import net.dv8tion.jda.core.managers.AudioManager ;
2017-11-09 11:09:54 +01:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2017-10-22 23:48:13 +02:00
import java.util.ArrayList ;
2018-01-12 23:50:13 +01:00
import java.util.Collections ;
2017-10-22 23:48:13 +02:00
import java.util.List ;
public class AudioM {
2018-01-12 19:14:43 +01:00
2017-11-04 18:27:18 +01:00
private GuildMusicManager musicManager ;
private AudioPlayerManager playerManager ;
private VoiceChannel playedChanel ;
private int listTimeOut = 30 ;
private int listExtremLimit = 300 ;
2017-11-09 11:09:54 +01:00
private Logger logger = LogManager . getLogger ( ) ;
2017-12-28 17:19:25 +01:00
private Guild guild ;
2017-10-22 23:48:13 +02:00
2017-12-28 17:19:25 +01:00
public AudioM ( Guild guild ) {
2017-10-22 23:48:13 +02:00
this . playerManager = new DefaultAudioPlayerManager ( ) ;
AudioSourceManagers . registerRemoteSources ( playerManager ) ;
AudioSourceManagers . registerLocalSource ( playerManager ) ;
2017-12-28 17:19:25 +01:00
this . guild = guild ;
2017-10-22 23:48:13 +02:00
}
2017-12-28 17:19:25 +01:00
public void loadAndPlay ( MessageReceivedEvent event , VoiceChannel voiceChannel , final String trackUrl , int playlistLimit , boolean onHead ) {
GuildMusicManager musicManager = getGuildAudioPlayer ( guild ) ;
2017-10-22 23:48:13 +02:00
playedChanel = voiceChannel ;
playerManager . loadItemOrdered ( musicManager , trackUrl , new AudioLoadResultHandler ( ) {
@Override
public void trackLoaded ( AudioTrack track ) {
2017-11-09 11:09:54 +01:00
logger . info ( " Single Track detected! " ) ;
2017-12-28 17:19:25 +01:00
2017-10-22 23:48:13 +02:00
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicOk ( " Ajout de " + track . getInfo ( ) . title + " à la file d'attente! " ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
2017-12-12 23:29:49 +01:00
new MessageTimeOut ( messages , MainBot . messageTimeOut ) . start ( ) ;
2017-12-28 17:19:25 +01:00
play ( guild , voiceChannel , musicManager , track , onHead ) ;
2017-10-22 23:48:13 +02:00
}
@Override
public void playlistLoaded ( AudioPlaylist playlist ) {
2017-11-09 11:09:54 +01:00
logger . info ( " Playlist detected! Limit: " + playlistLimit ) ;
2017-10-22 23:48:13 +02:00
AudioTrack firstTrack = playlist . getSelectedTrack ( ) ;
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicOk ( " Ajout de " + firstTrack . getInfo ( ) . title + " et les 30 premiers titres à la file d'attente! " ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
2017-12-12 23:29:49 +01:00
new MessageTimeOut ( messages , MainBot . messageTimeOut ) . start ( ) ;
2017-12-28 17:19:25 +01:00
playListLoader ( playlist , playlistLimit , onHead ) ;
2017-10-22 23:48:13 +02:00
}
@Override
public void noMatches ( ) {
2017-11-09 11:09:54 +01:00
logger . warn ( " Cant find media! " ) ;
2017-10-22 23:48:13 +02:00
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicError ( " Musique introuvable! " ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
2017-12-12 23:29:49 +01:00
new MessageTimeOut ( messages , MainBot . messageTimeOut ) . start ( ) ;
2017-10-22 23:48:13 +02:00
}
@Override
public void loadFailed ( FriendlyException exception ) {
2017-11-09 11:09:54 +01:00
logger . error ( " Cant load media! " ) ;
logger . error ( exception . getMessage ( ) ) ;
2017-10-22 23:48:13 +02:00
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicError ( " Erreur de lecture! " ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
2017-12-12 23:29:49 +01:00
new MessageTimeOut ( messages , MainBot . messageTimeOut ) . start ( ) ;
2017-10-22 23:48:13 +02:00
}
} ) ;
}
2017-12-28 17:19:25 +01:00
public void playListLoader ( AudioPlaylist playlist , int playlistLimit , boolean onHead ) {
int i = 0 ;
2018-01-12 23:50:13 +01:00
List < AudioTrack > tracks = playlist . getTracks ( ) ;
if ( onHead )
Collections . reverse ( tracks ) ;
2017-12-28 17:19:25 +01:00
for ( AudioTrack track : playlist . getTracks ( ) ) {
play ( guild , playedChanel , musicManager , track , onHead ) ;
i + + ;
if ( ( i > = playlistLimit & & i ! = - 1 ) | | i > listExtremLimit )
break ;
}
}
2017-10-22 23:48:13 +02:00
2018-01-12 19:14:43 +01:00
public GuildMusicManager getGuildAudioPlayer ( Guild guild ) {
2017-10-22 23:48:13 +02:00
if ( musicManager = = null ) {
musicManager = new GuildMusicManager ( playerManager ) ;
}
guild . getAudioManager ( ) . setSendingHandler ( musicManager . getSendHandler ( ) ) ;
return musicManager ;
}
2017-12-28 17:19:25 +01:00
public void play ( Guild guild , VoiceChannel channel , GuildMusicManager musicManager , AudioTrack track , boolean onHead ) {
2017-11-04 18:27:18 +01:00
if ( ! guild . getAudioManager ( ) . isConnected ( ) )
guild . getAudioManager ( ) . openAudioConnection ( channel ) ;
if ( ! onHead )
musicManager . scheduler . queue ( track ) ;
else
musicManager . scheduler . addNext ( track ) ;
2017-10-22 23:48:13 +02:00
}
public void skipTrack ( MessageReceivedEvent event ) {
GuildMusicManager musicManager = getGuildAudioPlayer ( event . getGuild ( ) ) ;
musicManager . scheduler . nextTrack ( ) ;
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicOk ( " Musique suivante! " ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
2017-12-12 23:29:49 +01:00
new MessageTimeOut ( messages , MainBot . messageTimeOut ) . start ( ) ;
2017-10-22 23:48:13 +02:00
}
public void pause ( MessageReceivedEvent event ) {
GuildMusicManager musicManager = getGuildAudioPlayer ( event . getGuild ( ) ) ;
musicManager . scheduler . pause ( ) ;
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicOk ( " Musique en pause ! " ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
2017-12-12 23:29:49 +01:00
new MessageTimeOut ( messages , MainBot . messageTimeOut ) . start ( ) ;
2017-10-22 23:48:13 +02:00
}
public void resume ( MessageReceivedEvent event ) {
GuildMusicManager musicManager = getGuildAudioPlayer ( event . getGuild ( ) ) ;
musicManager . scheduler . resume ( ) ;
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicOk ( " Reprise de la piste en cour ! " ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
2017-12-12 23:29:49 +01:00
new MessageTimeOut ( messages , MainBot . messageTimeOut ) . start ( ) ;
2017-10-22 23:48:13 +02:00
}
public void info ( MessageReceivedEvent event ) {
GuildMusicManager musicManager = getGuildAudioPlayer ( event . getGuild ( ) ) ;
AudioTrackInfo info = musicManager . scheduler . getInfo ( ) ;
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicOk ( info . title + " \ n " + info . uri ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
2017-12-12 23:29:49 +01:00
new MessageTimeOut ( messages , MainBot . messageTimeOut ) . start ( ) ;
2017-10-22 23:48:13 +02:00
}
public void flush ( MessageReceivedEvent event ) {
GuildMusicManager musicManager = getGuildAudioPlayer ( event . getGuild ( ) ) ;
musicManager . scheduler . flush ( ) ;
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicOk ( " RAZ de la playlist! " ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
2017-12-12 23:29:49 +01:00
new MessageTimeOut ( messages , MainBot . messageTimeOut ) . start ( ) ;
2017-10-22 23:48:13 +02:00
}
public void list ( MessageReceivedEvent event ) {
GuildMusicManager musicManager = getGuildAudioPlayer ( event . getGuild ( ) ) ;
List < AudioTrackInfo > list = musicManager . scheduler . getList ( ) ;
StringBuilder resp = new StringBuilder ( ) ;
if ( list . size ( ) = = 0 ) {
resp . append ( " Oh mon dieux! \ nElle est vide! \ n:astonished: " ) ;
}
else
{
for ( AudioTrackInfo trackInfo : list ) {
resp . append ( " - " ) ;
resp . append ( trackInfo . title ) ;
resp . append ( " \ n " ) ;
}
}
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicOk ( " Playlist: \ n \ n " + resp . toString ( ) ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
2017-12-12 23:29:49 +01:00
new MessageTimeOut ( messages , listTimeOut ) . start ( ) ;
2017-10-22 23:48:13 +02:00
}
2017-11-04 18:27:18 +01:00
public void add ( MessageReceivedEvent event , String url , int playListLimit , boolean onHead ) {
2017-10-22 23:48:13 +02:00
if ( playedChanel ! = null ) {
2017-11-04 18:27:18 +01:00
loadAndPlay ( event , playedChanel , url , playListLimit , onHead ) ;
2017-10-22 23:48:13 +02:00
}
else
{
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicError ( " Aucune lecture en cour! " ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
2017-12-12 23:29:49 +01:00
new MessageTimeOut ( messages , MainBot . messageTimeOut ) . start ( ) ;
2017-10-22 23:48:13 +02:00
}
}
2017-11-04 18:27:18 +01:00
2017-10-22 23:48:13 +02:00
public void stop ( MessageReceivedEvent event ) {
musicManager . scheduler . stop ( ) ;
2017-12-28 17:19:25 +01:00
musicManager . scheduler . flush ( ) ;
2017-12-26 16:25:35 +01:00
if ( event ! = null ) {
Message message = event . getTextChannel ( ) . sendMessage ( EmbedMessageUtils . getMusicOk ( " Arret de la musique! " ) ) . complete ( ) ;
List < Message > messages = new ArrayList < Message > ( ) { {
add ( message ) ;
add ( event . getMessage ( ) ) ;
} } ;
new MessageTimeOut ( messages , MainBot . messageTimeOut ) . start ( ) ;
}
2017-10-22 23:48:13 +02:00
}
2017-11-06 16:22:15 +01:00
public void stop ( GuildVoiceLeaveEvent event ) {
GuildMusicManager musicManager = getGuildAudioPlayer ( event . getGuild ( ) ) ;
musicManager . scheduler . stop ( ) ;
playedChanel = null ;
event . getGuild ( ) . getAudioManager ( ) . closeAudioConnection ( ) ;
}
2017-12-12 23:29:49 +01:00
public GuildMusicManager getMusicManager ( ) throws NullMusicManager , NotConectedException {
if ( musicManager = = null )
throw new NullMusicManager ( ) ;
else if ( playedChanel = = null )
throw new NotConectedException ( ) ;
return musicManager ;
}
2017-12-28 17:19:25 +01:00
public Guild getGuild ( ) {
return guild ;
}
2017-10-22 23:48:13 +02:00
2017-12-28 17:19:25 +01:00
public AudioPlayerManager getPlayerManager ( ) {
return playerManager ;
}
public VoiceChannel getPlayedChanel ( ) {
return playedChanel ;
}
2018-01-12 19:14:43 +01:00
public void setPlayedChanel ( VoiceChannel playedChanel ) {
this . playedChanel = playedChanel ;
}
2017-10-22 23:48:13 +02:00
}