Files
eight-track/src/main/java/net/locusworks/discord/eighttrack/services/GuildMusicService.java
Isaac Parenteau 2f89362a8b
Some checks failed
Locusworks Team/eight-track/pipeline/head There was a failure building this commit
Refactored how the music handler was being referenced
2020-01-06 21:22:54 -06:00

123 lines
5.0 KiB
Java

/**
*
* Project: Eight Track, File: GuildMusicService.java
*
* Copyright 2019-2020 Locusworks LLC.
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
* any means and imposes fines up to $25,000 for violation. No part of this material
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
* modified, duplicated, adapted or translated into another program language in any
* form or by any means, electronic, mechanical, photocopying, recording, or
* otherwise, without the prior written permission from Locusworks. Locusworks
* affirms that Eight-Track(R) software and data is subject to United States
* Government Purpose Rights. Contact Locusworks, 1313 Lawnview Drive
* Forney TX 75126, (802) 488-0438, for commercial licensing opportunities.
*
* IN NO EVENT SHALL LOCUSWORKS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF LOCUSWORKS HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NO RESPONSIBILITY IS ASSUMED BY
* LOCUSWORKS FOR ITS USE, OR FOR ANY INFRINGEMENTS OF PATENTS OR OTHER RIGHTS OF
* THIRD PARTIES RESULTING FROM ITS USE. LOCUSWORKS SPECIFICALLY DISCLAIMS ANY
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND
* ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
* IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS.
*/
package net.locusworks.discord.eighttrack.services;
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 javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
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.entities.DiscordGuild;
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
import net.locusworks.logger.ApplicationLogger;
import net.locusworks.logger.ApplicationLoggerFactory;
@Service
public class GuildMusicService {
private ApplicationLogger logger;
@Autowired
private RepositoryService guildRepoService;
private static Map<Long, GuildMusicHandler> handlerMap = new ConcurrentHashMap<>();
@PostConstruct
private void init() {
logger = ApplicationLoggerFactory.getLogger(GuildMusicService.class);
for (DiscordGuild dg : guildRepoService.getGuildRepo().findAll()) {
GuildMusicHandler gmh = new GuildMusicHandler(dg.getGuildId(), guildRepoService);
gmh.accept((id) -> handlerMap.remove(id));
handlerMap.put(dg.getGuildId(), gmh);
}
}
/**
* Close out connections that are no long being played. clean up resources
*/
@Scheduled(fixedRate = 5 * 60 * 1000)
private void checkPlayers() {
JDA client = EightTrackService.getClient();
if (client == null) {
logger.warn("Discord client is null. Unable to do cleanup");
return;
}
logger.debug("Checking players to see if anyone is listening");
OffsetDateTime now = OffsetDateTime.now();
for(Iterator<Entry<Long, GuildMusicHandler>> iterator = handlerMap.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.toMillis() > 5 * 60 * 1000) {
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();
}
}
}
}
public GuildMusicHandler getMusicHandler(Long guildId) {
if (!handlerMap.containsKey(guildId)) {
GuildMusicHandler gmh = new GuildMusicHandler(guildId, guildRepoService);
gmh.accept((id) -> handlerMap.remove(id));
handlerMap.put(guildId, gmh);
}
return handlerMap.get(guildId);
}
}