Feature Major updates and bug fixes
Some checks failed
Locusworks Team/eight-track/pipeline/head There was a failure building this commit

#4 Added logic to disconnect the bot if no song is playing or no one is
listening

#10 Added functionality to play song from local source and ability to
upload songs

#10 No longer cashes songs in memory but queries the database for random
song

#10 Whatsnext will push a song on the queue if it doesn't exist

#10 Next will get a random song from the database
This commit is contained in:
Isaac Parenteau
2019-10-07 23:48:05 -05:00
parent fe4c25cc4c
commit 2ac25f64aa
6 changed files with 92 additions and 72 deletions

View File

@@ -42,6 +42,7 @@ import org.springframework.stereotype.Service;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.locusworks.discord.eighttrack.database.repos.GuildRepository;
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
@@ -58,15 +59,15 @@ public class DatabaseCleanupService {
@Autowired
private GuildRepository guildRepo;
@Autowired
private GuildMusicService musicService;
private JDA client;
public void setClient(JDA client) {
this.client = client;
}
/**
* Close out connections that are no long being played. clean up resources
*/
@Scheduled(fixedRate = 5 * MINUTE)
private void checkPlayers() {
if (client == null) {
@@ -76,22 +77,35 @@ public class DatabaseCleanupService {
logger.debug("Checking players to see if anyone is listening");
OffsetDateTime now = OffsetDateTime.now();
for(Iterator<Entry<Long, GuildMusicHandler>> iterator = musicService.entrySet().iterator(); iterator.hasNext();) {
for(Iterator<Entry<Long, GuildMusicHandler>> iterator = GuildMusicService.getMap().entrySet().iterator(); iterator.hasNext();) {
Entry<Long, GuildMusicHandler> entry = iterator.next();
GuildMusicHandler gmh = entry.getValue();
long guildId = entry.getKey();
Guild guild = client.getGuildById(guildId);
if (guild == null) {
iterator.remove();
continue;
}
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;
}
if (duration.toMillis() > 5 * MINUTE) {
guild.getAudioManager().closeAudioConnection();
iterator.remove();
}
Long voiceChannelId = gmh.getCurrentVoiceChannelId();
if (voiceChannelId == null) {
guild.getAudioManager().closeAudioConnection();
iterator.remove();
} else {
VoiceChannel vc = guild.getVoiceChannelById(voiceChannelId);
if (vc != null && vc.getMembers().size() == 1) {
guild.getAudioManager().closeAudioConnection();
iterator.remove();
}
}
}
}