Search in sources :

Example 6 with TLongSet

use of gnu.trove.set.TLongSet in project PorkBot by DaMatrix.

the class AudioUtils method init.

public static void init() {
    youTube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {

        @Override
        public void initialize(HttpRequest request) {
        }
    }).setApplicationName("youtube-cmdline-search-sample").build();
    devKey = KeyGetter.getDevKey();
    musicManagers = new TSynchronizedLongObjectMap<>(new TLongObjectHashMap<>());
    playerManager = new DefaultAudioPlayerManager();
    AudioSourceManagers.registerRemoteSources(playerManager);
    AudioSourceManagers.registerLocalSource(playerManager);
    PorkBot.timer.schedule(new TimerTask() {

        @Override
        public void run() {
            TLongSet toRemove = new TLongHashSet();
            long now = System.currentTimeMillis();
            musicManagers.forEachEntry((key, value) -> {
                A: {
                    if (value.channel == null) {
                        // System.out.println("Removing guild because: missing channel");
                        toRemove.add(key);
                        break A;
                    }
                    if (value.created + 10000 < now && (value.channel.getMembers().size() < 2 || (value.manager.scheduler.queue.size() == 0 && value.manager.player.getPlayingTrack() == null))) {
                        // nobody's in the channel
                        // System.out.println("Removing guild because: empty");
                        value.manager.player.destroy();
                        value.channel.getGuild().getAudioManager().closeAudioConnection();
                        toRemove.add(key);
                        break A;
                    }
                }
                return true;
            });
            toRemove.forEach(l -> {
                musicManagers.remove(l);
                return true;
            });
            // not sure why or how this is happening, but it does apparently
            List<AudioManager> list = ShardUtils.getConnectedVoice();
            for (AudioManager manager : list) {
                if (manager.getConnectedChannel().getMembers().size() < 2) {
                    manager.closeAudioConnection();
                }
            }
        }
    }, 10000, 5000);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) LoadingCache(com.google.common.cache.LoadingCache) Member(net.dv8tion.jda.core.entities.Member) TextChannel(net.dv8tion.jda.core.entities.TextChannel) MessageUtils(net.daporkchop.porkbot.util.MessageUtils) ShardUtils(net.daporkchop.porkbot.util.ShardUtils) PorkBot(net.daporkchop.porkbot.PorkBot) HttpRequest(com.google.api.client.http.HttpRequest) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) TSynchronizedLongObjectMap(gnu.trove.impl.sync.TSynchronizedLongObjectMap) TLongHashSet(gnu.trove.set.hash.TLongHashSet) KeyGetter(net.daporkchop.porkbot.util.KeyGetter) TimerTask(java.util.TimerTask) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel) DefaultAudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager) TLongSet(gnu.trove.set.TLongSet) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) AudioManager(net.dv8tion.jda.core.managers.AudioManager) YouTube(com.google.api.services.youtube.YouTube) IOException(java.io.IOException) AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) CacheLoader(com.google.common.cache.CacheLoader) AudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager) TimeUnit(java.util.concurrent.TimeUnit) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) Guild(net.dv8tion.jda.core.entities.Guild) List(java.util.List) SearchListResponse(com.google.api.services.youtube.model.SearchListResponse) AudioSourceManagers(com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers) TLongObjectMap(gnu.trove.map.TLongObjectMap) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) CacheBuilder(com.google.common.cache.CacheBuilder) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) DefaultAudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) YouTube(com.google.api.services.youtube.YouTube) AudioManager(net.dv8tion.jda.core.managers.AudioManager) TimerTask(java.util.TimerTask) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) TLongSet(gnu.trove.set.TLongSet) TLongHashSet(gnu.trove.set.hash.TLongHashSet) List(java.util.List) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Aggregations

TLongSet (gnu.trove.set.TLongSet)6 TLongHashSet (gnu.trove.set.hash.TLongHashSet)5 TLongArrayList (gnu.trove.list.array.TLongArrayList)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 VoiceChannel (net.dv8tion.jda.core.entities.VoiceChannel)2 Async (br.com.brjdevs.java.utils.async.Async)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DiscordBotsAPI (com.github.natanbc.discordbotsapi.DiscordBotsAPI)1 PostingException (com.github.natanbc.discordbotsapi.PostingException)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 YouTube (com.google.api.services.youtube.YouTube)1 SearchListResponse (com.google.api.services.youtube.model.SearchListResponse)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1