Add auto-disconnect from vocal when bot is alone

This commit is contained in:
Unknown 2017-11-06 16:22:15 +01:00
parent 167c0e9472
commit 9a5507fc1b
3 changed files with 20 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package net.Broken;
import net.Broken.Commandes.Move;
import net.Broken.Commandes.Music;
import net.Broken.Commandes.Spam;
import net.Broken.Outils.AntiSpam;
import net.Broken.Outils.MessageTimeOut;
@ -70,15 +71,20 @@ public class BotListener extends ListenerAdapter {
}
@Override
public void onGuildVoiceLeave(GuildVoiceLeaveEvent event) {
super.onGuildVoiceLeave(event);
if(event.getVoiceState().inVoiceChannel())
if(event.getGuild().getAudioManager().isConnected())
{
logger.debug("User disconnected from voice channel.");
if(event.getVoiceState().getChannel().getMembers().size() == 1){
if(event.getGuild().getAudioManager().getConnectedChannel().getMembers().size() == 1){
logger.debug("I'm alone, close audio connection.");
event.getGuild().getAudioManager().closeAudioConnection();
Music music = (Music) MainBot.commandes.get("music");
music.audio.stop(event);
}
}
}

View File

@ -16,7 +16,7 @@ import java.util.ArrayList;
import java.util.List;
public class Music implements Commande {
AudioM audio;
public AudioM audio;
Logger logger = LogManager.getLogger();
public Music() {
audio = new AudioM();
@ -80,7 +80,6 @@ public class Music implements Commande {
break;
case "stop":
audio.stop(event);
event.getGuild().getAudioManager().closeAudioConnection();
break;
case "info":
audio.info(event);

View File

@ -15,6 +15,7 @@ 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;
import net.dv8tion.jda.core.events.guild.voice.GuildVoiceLeaveEvent;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.managers.AudioManager;
@ -217,7 +218,7 @@ public class AudioM {
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
musicManager.scheduler.stop();
playedChanel = null;
event.getGuild().getAudioManager().closeAudioConnection();
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Arret de la musique!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
@ -226,6 +227,14 @@ public class AudioM {
new MessageTimeOut(messages, MainBot.messageTimeOut).run();
}
public void stop (GuildVoiceLeaveEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild());
musicManager.scheduler.stop();
playedChanel = null;
event.getGuild().getAudioManager().closeAudioConnection();
}
}