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.GuildMemberRoleRemoveEvent;
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.priv.PrivateMessageReceivedEvent;
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
public void onMessageReceived(MessageReceivedEvent event) {
// ----------------------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.VoiceChannel;
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.List;
public class Music implements Commande {
AudioM audio;
Logger logger = LogManager.getLogger();
public Music() {
audio = new AudioM();
}
@ -30,15 +34,24 @@ public class Music implements Commande {
switch (args[0]){
case "play":
event.getTextChannel().sendTyping().queue();
if(args.length>=3){
List<VoiceChannel> chanels = event.getGuild().getVoiceChannelsByName(args[1], true);
if(chanels.size() >= 1){
VoiceChannel chanel = chanels.get(0);
audio.loadAndPlay(event,chanel,args[2]);
if(args.length>=2){
if(event.getMember().getVoiceState().inVoiceChannel()){
VoiceChannel voiceChanel = event.getMember().getVoiceState().getChannel();
logger.info("Connecting to "+voiceChanel.getName()+"...");
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{
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>(){{
add(message);
add(event.getMessage());
@ -80,8 +93,16 @@ public class Music implements Commande {
break;
case "add":
event.getTextChannel().sendTyping().queue();
if(args.length>=2){
audio.add(event,args[1]);
if(args.length ==2){
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{
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();
}
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:
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Arguments inconu!")).complete();
List<Message> messages = new ArrayList<Message>(){{
@ -115,7 +152,7 @@ public class Music implements Commande {
@Override
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;
public class AudioM {
GuildMusicManager musicManager;
AudioPlayerManager playerManager;
VoiceChannel playedChanel;
int listTimeOut = 30;
private GuildMusicManager musicManager;
private AudioPlayerManager playerManager;
private VoiceChannel playedChanel;
private int listTimeOut = 30;
private int listExtremLimit = 300;
public AudioM() {
this.playerManager = new DefaultAudioPlayerManager();
@ -33,7 +34,7 @@ public class AudioM {
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());
playedChanel = voiceChannel;
@ -46,8 +47,7 @@ public class AudioM {
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
play(event.getGuild(), voiceChannel, musicManager, track);
play(event.getGuild(), voiceChannel, musicManager, track, onHead);
}
@Override
@ -62,15 +62,12 @@ public class AudioM {
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
int i = 0;
for(AudioTrack track : playlist.getTracks()){
play(event.getGuild(), voiceChannel, musicManager, track);
play(event.getGuild(), voiceChannel, musicManager, track,onHead);
i++;
if(i>30)
if((i>=playlistLimit && i!=-1) || i>listExtremLimit)
break;
}
}
@Override
@ -108,10 +105,13 @@ public class AudioM {
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) {
if(!guild.getAudioManager().isConnected())
guild.getAudioManager().openAudioConnection(channel);
if(!onHead)
musicManager.scheduler.queue(track);
else
musicManager.scheduler.addNext(track);
}
public void skipTrack(MessageReceivedEvent event) {
@ -196,9 +196,10 @@ public class AudioM {
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){
loadAndPlay(event,playedChanel, url);
loadAndPlay(event,playedChanel, url, playListLimit,onHead);
}
else
{
@ -211,6 +212,7 @@ public class AudioM {
}
}
public void stop (MessageReceivedEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
musicManager.scheduler.stop();

View File

@ -8,7 +8,9 @@ import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
/**
@ -16,7 +18,7 @@ import java.util.concurrent.LinkedBlockingQueue;
*/
public class TrackScheduler extends AudioEventAdapter {
private final AudioPlayer player;
private final BlockingQueue<AudioTrack> queue;
private final BlockingDeque<AudioTrack> queue;
/**
* @param player The audio player this scheduler uses
@ -24,7 +26,7 @@ public class TrackScheduler extends AudioEventAdapter {
public TrackScheduler(AudioPlayer player) {
this.player = player;
player.setVolume(25);
this.queue = new LinkedBlockingQueue<>();
this.queue = new LinkedBlockingDeque<>();
}
/**
@ -40,6 +42,14 @@ public class TrackScheduler extends AudioEventAdapter {
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() {
player.setPaused(true);