|
| 1 | +/* |
| 2 | + * Copyright 2019 Andre601 |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 5 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 6 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 7 | + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | + * |
| 9 | + * The above copyright notice and this permission notice shall be included in all copies or substantial |
| 10 | + * portions of the Software. |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 13 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 14 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 15 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 16 | + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.andre601.javabotblockapi.requests; |
| 20 | + |
| 21 | +import com.andre601.javabotblockapi.BotBlockAPI; |
| 22 | +import com.andre601.javabotblockapi.exceptions.RatelimitedException; |
| 23 | +import net.dv8tion.jda.api.JDA; |
| 24 | +import net.dv8tion.jda.api.sharding.ShardManager; |
| 25 | +import org.jetbrains.annotations.NotNull; |
| 26 | +import org.json.JSONArray; |
| 27 | +import org.json.JSONObject; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +public class PostAction{ |
| 35 | + |
| 36 | + private boolean disableCache; |
| 37 | + private final RequestHandler REQUEST_HANDLER = new RequestHandler(); |
| 38 | + |
| 39 | + public PostAction(){ |
| 40 | + this.disableCache = false; |
| 41 | + } |
| 42 | + |
| 43 | + public PostAction(boolean disableCache){ |
| 44 | + this.disableCache = disableCache; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Posts the guild count provided through the {@link net.dv8tion.jda.api.JDA JDA instance}. |
| 49 | + * <br><b>It's recommended to use {@link #postGuilds(ShardManager, BotBlockAPI) postGuilds(ShardManager, BotBlockAPI} |
| 50 | + * if you're using a sharded bot.</b> |
| 51 | + * |
| 52 | + * <p>When the amount of shards a bot has is bigger than one will shard_id and shard_count be added. |
| 53 | + * |
| 54 | + * @param jda |
| 55 | + * The {@link net.dv8tion.jda.api.JDA JDA instance}. |
| 56 | + * @param botBlockAPI |
| 57 | + * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance}. |
| 58 | + * |
| 59 | + * @throws IOException |
| 60 | + * When the post request couldn't be performed. |
| 61 | + * @throws RatelimitedException |
| 62 | + * When we exceed the rate-limit of the BotBlock API. |
| 63 | + */ |
| 64 | + public void postGuilds(@NotNull JDA jda, @NotNull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{ |
| 65 | + JSONObject json = new JSONObject() |
| 66 | + .put("server_count", jda.getGuilds().size()) |
| 67 | + .put("bot_id", jda.getSelfUser().getId()); |
| 68 | + |
| 69 | + if(jda.getShardInfo().getShardTotal() > 1) |
| 70 | + json.put("shard_id", jda.getShardInfo().getShardId()) |
| 71 | + .put("shard_count", jda.getShardInfo().getShardTotal()); |
| 72 | + |
| 73 | + botBlockAPI.getAuthTokens().forEach(json::put); |
| 74 | + |
| 75 | + REQUEST_HANDLER.performPOST(json); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Posts the guild count with the provided bot id. |
| 80 | + * |
| 81 | + * @param botId |
| 82 | + * The ID of the bot. |
| 83 | + * @param guilds |
| 84 | + * The guild count. |
| 85 | + * @param botBlockAPI |
| 86 | + * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance}. |
| 87 | + * |
| 88 | + * @throws IOException |
| 89 | + * When the post request couldn't be performed. |
| 90 | + * @throws RatelimitedException |
| 91 | + * When we exceed the rate-limit of the BotBlock API. |
| 92 | + */ |
| 93 | + public void postGuilds(Long botId, int guilds, @NotNull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{ |
| 94 | + postGuilds(Long.toString(botId), guilds, botBlockAPI); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Posts the guild count from the provided {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager instance}. |
| 99 | + * <br>The guild count of each shard will be added as an JSONArray. |
| 100 | + * |
| 101 | + * @param shardManager |
| 102 | + * The {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager instance}. |
| 103 | + * @param botBlockAPI |
| 104 | + * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance}. |
| 105 | + * |
| 106 | + * @throws IOException |
| 107 | + * When the post request couldn't be performed. |
| 108 | + * @throws RatelimitedException |
| 109 | + * When we exceed the rate-limit of the BotBlock API. |
| 110 | + */ |
| 111 | + public void postGuilds(@NotNull ShardManager shardManager, @NotNull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{ |
| 112 | + JDA jda = shardManager.getShardById(0); |
| 113 | + if(jda == null) |
| 114 | + throw new NullPointerException("Received shard was null!"); |
| 115 | + |
| 116 | + JSONObject json = new JSONObject() |
| 117 | + .put("server_count", shardManager.getGuilds().size()) |
| 118 | + .put("bot_id", jda.getSelfUser().getId()) |
| 119 | + .put("shard_count", shardManager.getShardCache().size()); |
| 120 | + |
| 121 | + List<Integer> shards = new ArrayList<>(); |
| 122 | + for(JDA shard : shardManager.getShards()) |
| 123 | + shards.add(shard.getGuilds().size()); |
| 124 | + |
| 125 | + json.put("shards", new JSONArray(Arrays.deepToString(shards.toArray()))); |
| 126 | + botBlockAPI.getAuthTokens().forEach(json::put); |
| 127 | + |
| 128 | + REQUEST_HANDLER.performPOST(json); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Posts the guild count with the provided bot id. |
| 133 | + * |
| 134 | + * @param botId |
| 135 | + * The ID of the bot. |
| 136 | + * @param guilds |
| 137 | + * The guild count. |
| 138 | + * @param botBlockAPI |
| 139 | + * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance}. |
| 140 | + * |
| 141 | + * @throws IOException |
| 142 | + * When the post request couldn't be performed. |
| 143 | + * @throws RatelimitedException |
| 144 | + * When we exceed the rate-limit of the BotBlock API. |
| 145 | + */ |
| 146 | + public void postGuilds(@NotNull String botId, int guilds, @NotNull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{ |
| 147 | + JSONObject json = new JSONObject() |
| 148 | + .put("server_count", guilds) |
| 149 | + .put("bot_id", botId); |
| 150 | + |
| 151 | + botBlockAPI.getAuthTokens().forEach(json::put); |
| 152 | + |
| 153 | + REQUEST_HANDLER.performPOST(json); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments