Updated
This commit is contained in:
@@ -29,8 +29,18 @@ package net.locusworks.discord.eighttrack.adaptors;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||
@@ -43,13 +53,41 @@ public class MusicListenerAdaptor extends ListenerAdapter {
|
||||
private ApplicationLogger logger;
|
||||
|
||||
private Map<Long, GuildMusicHandler> playerMap;
|
||||
private JDA client;
|
||||
|
||||
public MusicListenerAdaptor(Path musicDir) throws IOException {
|
||||
public MusicListenerAdaptor(Path musicDir, JDA client) throws IOException {
|
||||
this.logger = ApplicationLoggerFactory.getLogger(MusicListenerAdaptor.class);
|
||||
this.playerMap = new ConcurrentHashMap<>();
|
||||
this.musicDir = musicDir;
|
||||
this.client = client;
|
||||
|
||||
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
executor.scheduleWithFixedDelay(()->checkPlayers(), 1, 1, TimeUnit.MINUTES);
|
||||
|
||||
}
|
||||
|
||||
private void checkPlayers() {
|
||||
logger.debug("Checking players to see if anyone is listening");
|
||||
OffsetDateTime now = OffsetDateTime.now();
|
||||
for(Iterator<Entry<Long, GuildMusicHandler>> iterator = playerMap.entrySet().iterator(); iterator.hasNext();) {
|
||||
Entry<Long, GuildMusicHandler> entry = iterator.next();
|
||||
GuildMusicHandler gmh = entry.getValue();
|
||||
long guildId = entry.getKey();
|
||||
|
||||
OffsetDateTime lastPlayed = gmh.getLastPlayed();
|
||||
Duration duration = Duration.between(lastPlayed, now);
|
||||
if (duration.getSeconds() > 300) {
|
||||
Guild guild = client.getGuildById(guildId);
|
||||
if (guild == null) {
|
||||
iterator.remove();
|
||||
continue;
|
||||
}
|
||||
guild.getAudioManager().closeAudioConnection();
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
|
||||
if (event.getAuthor().isBot()) return;
|
||||
@@ -66,6 +104,7 @@ public class MusicListenerAdaptor extends ListenerAdapter {
|
||||
playerMap.put(event.getGuild().getIdLong(), new GuildMusicHandler(musicDir));
|
||||
}
|
||||
GuildMusicHandler gmh = playerMap.get(event.getGuild().getIdLong());
|
||||
gmh.accept((id) -> playerMap.remove(id));
|
||||
|
||||
String command = event.getMessage().getContentRaw().trim().toLowerCase();
|
||||
switch (command) {
|
||||
|
||||
Reference in New Issue
Block a user