Improve audio command

This commit is contained in:
Unknown 2017-11-04 18:27:18 +01:00
parent 11ac5e1ca3
commit 55a7be441a
4 changed files with 93 additions and 30 deletions

View File

@ -15,6 +15,7 @@ import net.dv8tion.jda.core.events.ReadyEvent;
import net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.core.events.guild.member.GuildMemberRoleRemoveEvent; import net.dv8tion.jda.core.events.guild.member.GuildMemberRoleRemoveEvent;
import net.dv8tion.jda.core.events.guild.voice.GuildVoiceJoinEvent; import net.dv8tion.jda.core.events.guild.voice.GuildVoiceJoinEvent;
import net.dv8tion.jda.core.events.guild.voice.GuildVoiceLeaveEvent;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.events.message.priv.PrivateMessageReceivedEvent; import net.dv8tion.jda.core.events.message.priv.PrivateMessageReceivedEvent;
import net.dv8tion.jda.core.exceptions.HierarchyException; import net.dv8tion.jda.core.exceptions.HierarchyException;
@ -69,6 +70,19 @@ public class BotListener extends ListenerAdapter {
} }
@Override
public void onGuildVoiceLeave(GuildVoiceLeaveEvent event) {
super.onGuildVoiceLeave(event);
if(event.getVoiceState().inVoiceChannel())
{
logger.debug("User disconnected from voice channel.");
if(event.getVoiceState().getChannel().getMembers().size() == 1){
logger.debug("I'm alone, close audio connection.");
event.getGuild().getAudioManager().closeAudioConnection();
}
}
}
@Override @Override
public void onMessageReceived(MessageReceivedEvent event) { public void onMessageReceived(MessageReceivedEvent event) {
// ----------------------Test pour eviter eco de commande------------------------- // ----------------------Test pour eviter eco de commande-------------------------

View File

@ -8,12 +8,16 @@ import net.Broken.audio.AudioM;
import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.VoiceChannel; import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent; import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Music implements Commande { public class Music implements Commande {
AudioM audio; AudioM audio;
Logger logger = LogManager.getLogger();
public Music() { public Music() {
audio = new AudioM(); audio = new AudioM();
} }
@ -30,15 +34,24 @@ public class Music implements Commande {
switch (args[0]){ switch (args[0]){
case "play": case "play":
event.getTextChannel().sendTyping().queue(); event.getTextChannel().sendTyping().queue();
if(args.length>=3){ if(args.length>=2){
List<VoiceChannel> chanels = event.getGuild().getVoiceChannelsByName(args[1], true); if(event.getMember().getVoiceState().inVoiceChannel()){
if(chanels.size() >= 1){ VoiceChannel voiceChanel = event.getMember().getVoiceState().getChannel();
VoiceChannel chanel = chanels.get(0); logger.info("Connecting to "+voiceChanel.getName()+"...");
audio.loadAndPlay(event,chanel,args[2]); if(args.length ==2){
audio.loadAndPlay(event,voiceChanel,args[1],30,false);
}
else if(args.length == 3){
try{
int limit = Integer.parseInt(args[2]);
audio.loadAndPlay(event,voiceChanel,args[1],limit,false);
}catch (NumberFormatException e){
audio.loadAndPlay(event,voiceChanel,args[1],30,false);
}
}
} }
else{ else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Chanel introuvable!")).complete(); Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Non connecté sur un chanel vocal!")).complete();
List<Message> messages = new ArrayList<Message>(){{ List<Message> messages = new ArrayList<Message>(){{
add(message); add(message);
add(event.getMessage()); add(event.getMessage());
@ -80,8 +93,16 @@ public class Music implements Commande {
break; break;
case "add": case "add":
event.getTextChannel().sendTyping().queue(); event.getTextChannel().sendTyping().queue();
if(args.length>=2){ if(args.length ==2){
audio.add(event,args[1]); audio.add(event,args[1],30,false);
}
else if(args.length == 3){
try{
int limit = Integer.parseInt(args[2]);
audio.add(event,args[1],limit,false);
}catch (NumberFormatException e){
audio.add(event,args[1],30,false);
}
} }
else{ else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Arguments manquant!")).complete(); Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Arguments manquant!")).complete();
@ -92,6 +113,22 @@ public class Music implements Commande {
new MessageTimeOut(messages, MainBot.messageTimeOut).run(); new MessageTimeOut(messages, MainBot.messageTimeOut).run();
} }
break; break;
case "addNext":
event.getTextChannel().sendTyping().queue();
if(args.length >=2){
audio.add(event,args[1],1,true);
}
else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Arguments manquant!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
break;
default: default:
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Arguments inconu!")).complete(); Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Arguments inconu!")).complete();
List<Message> messages = new ArrayList<Message>(){{ List<Message> messages = new ArrayList<Message>(){{
@ -115,7 +152,7 @@ public class Music implements Commande {
@Override @Override
public String help(String[] args) { public String help(String[] args) {
return "`//music play <ChanelVocal> <url>`\n:arrow_right:\t*Let's dance! Deffinit le chat vocal à utiliser.`//music add`.*\n\n`//music pause`\n:arrow_right:\t*Mise en pause de la piste en cours.*\n\n`//music resume`\n:arrow_right:\t*Reprise de la lecture de la piste en cours.*\n\n`//music next`\n:arrow_right:\t*Change le piste en cours.*\n\n`//music stop`\n:arrow_right:\t*Arrête la piste en cours.*\n\n`//music info`\n:arrow_right:\t*Affiche les infos de la piste en cours.*\n\n`//music flush`\n:arrow_right:\t*Supprime la playlist en cours.*\n\n`//music list`\n:arrow_right:\t*Affiche la playlist en cours.*\n\n`//music add <url>`\n:arrow_right:\t*Ajoute l'url à la playlist en cour.*"; return "`//music play <url>`\n:arrow_right:\t*Let's dance! Deffinit le chat vocal à utiliser.*\n\n`//music pause`\n:arrow_right:\t*Mise en pause de la piste en cours.*\n\n`//music resume`\n:arrow_right:\t*Reprise de la lecture de la piste en cours.*\n\n`//music next`\n:arrow_right:\t*Change le piste en cours.*\n\n`//music stop`\n:arrow_right:\t*Arrête la piste en cours.*\n\n`//music info`\n:arrow_right:\t*Affiche les infos de la piste en cours.*\n\n`//music flush`\n:arrow_right:\t*Supprime la playlist en cours.*\n\n`//music list`\n:arrow_right:\t*Affiche la playlist en cours.*\n\n`//music add(Next) <url>`\n:arrow_right:\t*Ajoute l'url à la playlist en cour.*";
} }

View File

@ -22,10 +22,11 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class AudioM { public class AudioM {
GuildMusicManager musicManager; private GuildMusicManager musicManager;
AudioPlayerManager playerManager; private AudioPlayerManager playerManager;
VoiceChannel playedChanel; private VoiceChannel playedChanel;
int listTimeOut = 30; private int listTimeOut = 30;
private int listExtremLimit = 300;
public AudioM() { public AudioM() {
this.playerManager = new DefaultAudioPlayerManager(); this.playerManager = new DefaultAudioPlayerManager();
@ -33,7 +34,7 @@ public class AudioM {
AudioSourceManagers.registerLocalSource(playerManager); AudioSourceManagers.registerLocalSource(playerManager);
} }
public void loadAndPlay(MessageReceivedEvent event, VoiceChannel voiceChannel, final String trackUrl) { public void loadAndPlay(MessageReceivedEvent event, VoiceChannel voiceChannel, final String trackUrl,int playlistLimit,boolean onHead) {
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild()); GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
playedChanel = voiceChannel; playedChanel = voiceChannel;
@ -46,8 +47,7 @@ public class AudioM {
add(event.getMessage()); add(event.getMessage());
}}; }};
new MessageTimeOut(messages, MainBot.messageTimeOut).run(); new MessageTimeOut(messages, MainBot.messageTimeOut).run();
play(event.getGuild(), voiceChannel, musicManager, track, onHead);
play(event.getGuild(), voiceChannel, musicManager, track);
} }
@Override @Override
@ -62,15 +62,12 @@ public class AudioM {
new MessageTimeOut(messages, MainBot.messageTimeOut).run(); new MessageTimeOut(messages, MainBot.messageTimeOut).run();
int i = 0; int i = 0;
for(AudioTrack track : playlist.getTracks()){ for(AudioTrack track : playlist.getTracks()){
play(event.getGuild(), voiceChannel, musicManager, track); play(event.getGuild(), voiceChannel, musicManager, track,onHead);
i++; i++;
if(i>30) if((i>=playlistLimit && i!=-1) || i>listExtremLimit)
break; break;
} }
} }
@Override @Override
@ -108,10 +105,13 @@ public class AudioM {
return musicManager; return musicManager;
} }
private void play(Guild guild, VoiceChannel channel, GuildMusicManager musicManager, AudioTrack track) { private void play(Guild guild, VoiceChannel channel, GuildMusicManager musicManager, AudioTrack track,boolean onHead) {
guild.getAudioManager().openAudioConnection(channel); if(!guild.getAudioManager().isConnected())
guild.getAudioManager().openAudioConnection(channel);
musicManager.scheduler.queue(track); if(!onHead)
musicManager.scheduler.queue(track);
else
musicManager.scheduler.addNext(track);
} }
public void skipTrack(MessageReceivedEvent event) { public void skipTrack(MessageReceivedEvent event) {
@ -196,9 +196,10 @@ public class AudioM {
new MessageTimeOut(messages, listTimeOut).run(); new MessageTimeOut(messages, listTimeOut).run();
} }
public void add(MessageReceivedEvent event,String url) {
public void add(MessageReceivedEvent event,String url, int playListLimit, boolean onHead) {
if(playedChanel != null){ if(playedChanel != null){
loadAndPlay(event,playedChanel, url); loadAndPlay(event,playedChanel, url, playListLimit,onHead);
} }
else else
{ {
@ -211,6 +212,7 @@ public class AudioM {
} }
} }
public void stop (MessageReceivedEvent event) { public void stop (MessageReceivedEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild()); GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
musicManager.scheduler.stop(); musicManager.scheduler.stop();

View File

@ -8,7 +8,9 @@ import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
/** /**
@ -16,7 +18,7 @@ import java.util.concurrent.LinkedBlockingQueue;
*/ */
public class TrackScheduler extends AudioEventAdapter { public class TrackScheduler extends AudioEventAdapter {
private final AudioPlayer player; private final AudioPlayer player;
private final BlockingQueue<AudioTrack> queue; private final BlockingDeque<AudioTrack> queue;
/** /**
* @param player The audio player this scheduler uses * @param player The audio player this scheduler uses
@ -24,7 +26,7 @@ public class TrackScheduler extends AudioEventAdapter {
public TrackScheduler(AudioPlayer player) { public TrackScheduler(AudioPlayer player) {
this.player = player; this.player = player;
player.setVolume(25); player.setVolume(25);
this.queue = new LinkedBlockingQueue<>(); this.queue = new LinkedBlockingDeque<>();
} }
/** /**
@ -40,6 +42,14 @@ public class TrackScheduler extends AudioEventAdapter {
queue.offer(track); queue.offer(track);
} }
} }
public void addNext(AudioTrack track) {
// Calling startTrack with the noInterrupt set to true will start the track only if nothing is currently playing. If
// something is playing, it returns false and does nothing. In that case the player was already playing so this
// track goes to the queue instead.
if (!player.startTrack(track, true)) {
queue.addFirst(track);
}
}
public void pause() { public void pause() {
player.setPaused(true); player.setPaused(true);