Search in sources :

Example 1 with TUnmodifiableLongSet

use of gnu.trove.impl.unmodifiable.TUnmodifiableLongSet in project MantaroBot by Mantaro.

the class ShardedMantaro method startUpdaters.

private void startUpdaters() {
    Async.task("Carbonitex post task", carbonitex::handle, 30, TimeUnit.MINUTES);
    if (config.dbotsorgToken != null) {
        Async.task("dbots.org update thread", () -> {
            try {
                long count = MantaroBot.getInstance().getGuildCache().size();
                int[] shards = MantaroBot.getInstance().getShardList().stream().mapToInt(shard -> (int) shard.getGuildCache().size()).toArray();
                discordBotsAPI.postStats(shards);
                log.debug("Updated server count ({}) for discordbots.org", count);
            } catch (Exception ignored) {
            }
        }, 1, TimeUnit.HOURS);
        Async.task("discordbots.org upvotes task", () -> {
            if (config.dbotsorgToken == null)
                return;
            try {
                Request request = new Request.Builder().url("https://discordbots.org/api/bots/213466096718708737/votes?onlyids=1").addHeader("Authorization", config.dbotsorgToken).build();
                Response r = Utils.httpClient.newCall(request).execute();
                ResponseBody body = r.body();
                if (body == null)
                    return;
                // It's definitely String.
                @SuppressWarnings("unchecked") List<String> upvoters = objectMapper.readValue(body.string(), List.class);
                List<Long> upvotersLong = upvoters.stream().map(Long::parseLong).distinct().collect(Collectors.toList());
                TLongSet set = new TLongHashSet();
                set.addAll(upvotersLong);
                discordBotsUpvoters = new TUnmodifiableLongSet(set);
                r.close();
            } catch (Exception ignored) {
            }
        }, 5, TimeUnit.MINUTES);
    } else {
        log.warn("discordbots.org token not set in config, cannot start posting stats!");
    }
    String dbotsToken = config.dbotsToken;
    if (dbotsToken != null) {
        Async.task("bots.discord.pw update Thread", () -> {
            try {
                long count = MantaroBot.getInstance().getGuildCache().size();
                RequestBody body = RequestBody.create(JSON, new JSONObject().put("server_count", count).toString());
                Request request = new Request.Builder().url("https://bots.discord.pw/api/bots/213466096718708737/stats").addHeader("User-Agent", MantaroInfo.USER_AGENT).addHeader("Authorization", dbotsToken).addHeader("Content-Type", "application/json").post(body).build();
                Utils.httpClient.newCall(request).execute().close();
                log.debug("Updated server count for bots.discord.pw");
            } catch (Exception ignored) {
            }
        }, 1, TimeUnit.HOURS);
    }
    for (MantaroShard shard : getShards()) {
        shard.updateStatus();
    }
}
Also used : MantaroInfo(net.kodehawa.mantarobot.MantaroInfo) Getter(lombok.Getter) PostLoadEvent(net.kodehawa.mantarobot.core.listeners.events.PostLoadEvent) Async(br.com.brjdevs.java.utils.async.Async) Utils(net.kodehawa.mantarobot.utils.Utils) SHARD_FETCH_FAILURE(net.kodehawa.mantarobot.utils.ShutdownCodes.SHARD_FETCH_FAILURE) LoadState(net.kodehawa.mantarobot.core.LoadState) ICommandProcessor(net.kodehawa.mantarobot.core.processor.core.ICommandProcessor) ArrayList(java.util.ArrayList) MantaroBot(net.kodehawa.mantarobot.MantaroBot) JSONObject(org.json.JSONObject) okhttp3(okhttp3) TLongHashSet(gnu.trove.set.hash.TLongHashSet) SnowflakeCacheView(net.dv8tion.jda.core.utils.cache.SnowflakeCacheView) Collector(java.util.stream.Collector) MantaroCore(net.kodehawa.mantarobot.core.MantaroCore) Config(net.kodehawa.mantarobot.data.Config) PostingException(com.github.natanbc.discordbotsapi.PostingException) VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel) TLongSet(gnu.trove.set.TLongSet) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SentryHelper(net.kodehawa.mantarobot.utils.SentryHelper) Collectors(java.util.stream.Collectors) DiscordBotsAPI(com.github.natanbc.discordbotsapi.DiscordBotsAPI) MantaroEventManager(net.kodehawa.mantarobot.core.MantaroEventManager) TimeUnit(java.util.concurrent.TimeUnit) ShardWatcher(net.kodehawa.mantarobot.core.shard.watcher.ShardWatcher) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TUnmodifiableLongSet(gnu.trove.impl.unmodifiable.TUnmodifiableLongSet) Carbonitex(net.kodehawa.mantarobot.services.Carbonitex) MantaroData(net.kodehawa.mantarobot.data.MantaroData) LogUtils(net.kodehawa.mantarobot.log.LogUtils) JSONArray(org.json.JSONArray) PostingException(com.github.natanbc.discordbotsapi.PostingException) JSONObject(org.json.JSONObject) TLongSet(gnu.trove.set.TLongSet) TLongHashSet(gnu.trove.set.hash.TLongHashSet) TUnmodifiableLongSet(gnu.trove.impl.unmodifiable.TUnmodifiableLongSet)

Aggregations

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 TUnmodifiableLongSet (gnu.trove.impl.unmodifiable.TUnmodifiableLongSet)1 TLongSet (gnu.trove.set.TLongSet)1 TLongHashSet (gnu.trove.set.hash.TLongHashSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collector (java.util.stream.Collector)1 Collectors (java.util.stream.Collectors)1 Getter (lombok.Getter)1 Slf4j (lombok.extern.slf4j.Slf4j)1 VoiceChannel (net.dv8tion.jda.core.entities.VoiceChannel)1 SnowflakeCacheView (net.dv8tion.jda.core.utils.cache.SnowflakeCacheView)1 MantaroBot (net.kodehawa.mantarobot.MantaroBot)1 MantaroInfo (net.kodehawa.mantarobot.MantaroInfo)1 LoadState (net.kodehawa.mantarobot.core.LoadState)1 MantaroCore (net.kodehawa.mantarobot.core.MantaroCore)1