|
|
@ -22,9 +22,8 @@ import net.dv8tion.jda.api.JDABuilder; |
|
|
|
import net.dv8tion.jda.api.OnlineStatus; |
|
|
|
import net.dv8tion.jda.api.entities.Activity; |
|
|
|
import net.dv8tion.jda.api.entities.Guild; |
|
|
|
import net.dv8tion.jda.api.entities.TextChannel; |
|
|
|
import net.dv8tion.jda.api.entities.Member; |
|
|
|
import net.dv8tion.jda.api.entities.VoiceChannel; |
|
|
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; |
|
|
|
|
|
|
|
import javax.security.auth.login.LoginException; |
|
|
|
import java.io.BufferedReader; |
|
|
@ -61,8 +60,9 @@ public final class YoshiBot { |
|
|
|
* LavaPlayer AudioPlayerManager. |
|
|
|
*/ |
|
|
|
public AudioPlayerManager audioPlayerManager; |
|
|
|
|
|
|
|
public Guild guild; |
|
|
|
|
|
|
|
@Getter |
|
|
|
private Guild guild; |
|
|
|
|
|
|
|
public AudioPlayer audioPlayer; |
|
|
|
|
|
|
@ -70,18 +70,11 @@ public final class YoshiBot { |
|
|
|
|
|
|
|
@Getter |
|
|
|
private final Random random = new Random(); |
|
|
|
|
|
|
|
@Getter |
|
|
|
private boolean active; |
|
|
|
|
|
|
|
@Getter |
|
|
|
private VoiceChannel voiceChannel = null; |
|
|
|
|
|
|
|
/** |
|
|
|
* Initialisiert alle dynamisch hinzugefügten und statischen Ressourcen. Startet aber nicht den Bot selbst. |
|
|
|
*/ |
|
|
|
public boolean init(String configPath) { |
|
|
|
active = false; |
|
|
|
return Resources.init(configPath); |
|
|
|
} |
|
|
|
|
|
|
@ -129,7 +122,7 @@ public final class YoshiBot { |
|
|
|
|
|
|
|
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(YoshiBot::setRandomActivity, 0, 10, TimeUnit.HOURS); |
|
|
|
|
|
|
|
if(active) autoConnectToChannel(); |
|
|
|
joinVoiceChannelWithMostMembers(); |
|
|
|
} |
|
|
|
|
|
|
|
public synchronized void stop() { |
|
|
@ -166,47 +159,37 @@ public final class YoshiBot { |
|
|
|
Logger.logInfo("Setze Aktivität auf " + activity); |
|
|
|
} |
|
|
|
|
|
|
|
private void joinVoiceChannel(VoiceChannel vc){ |
|
|
|
if(vc == null) return; |
|
|
|
if(vc.equals(guild.getAudioManager().getConnectedChannel())) return; |
|
|
|
leaveAudioChannel(); |
|
|
|
public synchronized void joinVoiceChannel(VoiceChannel vc){ |
|
|
|
if(vc == null) { |
|
|
|
guild.getAudioManager().closeAudioConnection(); |
|
|
|
return; |
|
|
|
} |
|
|
|
if(guild.getAudioManager().getConnectedChannel() != null && |
|
|
|
vc.getIdLong() == guild.getAudioManager().getConnectedChannel().getIdLong()) return; |
|
|
|
guild.getAudioManager().openAudioConnection(vc); |
|
|
|
voiceChannel = vc; |
|
|
|
} |
|
|
|
|
|
|
|
private void leaveAudioChannel(){ |
|
|
|
if(guild.getAudioManager().getConnectedChannel() == null) return; |
|
|
|
guild.getAudioManager().closeAudioConnection(); |
|
|
|
voiceChannel = null; |
|
|
|
} |
|
|
|
|
|
|
|
public void autoConnectToChannel(){ |
|
|
|
if(!instance.active) return; |
|
|
|
|
|
|
|
VoiceChannel maxMembersVoiceChannel = null; |
|
|
|
public void joinVoiceChannelWithMostMembers(){ |
|
|
|
VoiceChannel maxVoiceChannel = null; |
|
|
|
int maxMembers = 0; |
|
|
|
for(VoiceChannel vc : guild.getVoiceChannels()){ |
|
|
|
if(vc.getMembers().size() > maxMembers){ |
|
|
|
maxMembersVoiceChannel = vc; |
|
|
|
maxMembers = vc.getMembers().size(); |
|
|
|
int membersInChannel = 0; |
|
|
|
for(Member m : vc.getMembers()) |
|
|
|
if(!m.getUser().isBot()) membersInChannel++; |
|
|
|
if(membersInChannel > maxMembers){ |
|
|
|
maxVoiceChannel = vc; |
|
|
|
maxMembers = membersInChannel; |
|
|
|
} |
|
|
|
} |
|
|
|
if(maxMembers == 0) leaveAudioChannel(); |
|
|
|
else joinVoiceChannel(maxMembersVoiceChannel); |
|
|
|
} |
|
|
|
|
|
|
|
public void setActive(boolean a){ |
|
|
|
active = a; |
|
|
|
if(active) autoConnectToChannel(); |
|
|
|
else leaveAudioChannel(); |
|
|
|
if(maxMembers < 1) joinVoiceChannel(null); |
|
|
|
else if(maxMembers > 0 && maxVoiceChannel != null) |
|
|
|
joinVoiceChannel(maxVoiceChannel); |
|
|
|
} |
|
|
|
|
|
|
|
public boolean playSound(File file, VoiceChannel vc){ |
|
|
|
if(active) return true; |
|
|
|
|
|
|
|
if (!file.isFile()) return false; |
|
|
|
|
|
|
|
joinVoiceChannel((vc == null)? voiceChannel : vc); |
|
|
|
joinVoiceChannel(vc); |
|
|
|
|
|
|
|
audioPlayerManager.loadItem(file.getAbsolutePath(), new AudioLoadResultHandlerImpl()); |
|
|
|
|
|
|
@ -214,8 +197,6 @@ public final class YoshiBot { |
|
|
|
} |
|
|
|
|
|
|
|
public boolean sayTTS(String text, VoiceChannel vc) { |
|
|
|
if(!active) return true; |
|
|
|
|
|
|
|
String path = Resources.getPathToTempAudioFile(UUID.randomUUID().toString()); |
|
|
|
|
|
|
|
try { |
|
|
|