|
| 1 | +/* |
| 2 | + * Copyright 2019 - 2020 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 org.botblock.javabotblockapi.javacord; |
| 20 | + |
| 21 | +import org.botblock.javabotblockapi.core.BotBlockAPI; |
| 22 | +import org.botblock.javabotblockapi.core.exceptions.RatelimitedException; |
| 23 | +import org.botblock.javabotblockapi.requests.handler.RequestHandler; |
| 24 | +import org.javacord.api.DiscordApi; |
| 25 | +import org.javacord.api.Javacord; |
| 26 | +import org.json.JSONArray; |
| 27 | +import org.json.JSONObject; |
| 28 | + |
| 29 | +import javax.annotation.Nonnull; |
| 30 | +import javax.annotation.Nullable; |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.List; |
| 35 | +import java.util.concurrent.ScheduledExecutorService; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | + |
| 38 | +/** |
| 39 | + * Class used to perform POST requests towards the <a href="https://botblock.org/api/docs#count" target="_blank">/api/count</a> |
| 40 | + * endpoint of BotBlock. |
| 41 | + * |
| 42 | + * <p>The class offers options to post either |
| 43 | + */ |
| 44 | +public class PostAction{ |
| 45 | + private final RequestHandler requestHandler; |
| 46 | + private final ScheduledExecutorService scheduler; |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates a new instance of this class. |
| 50 | + * <br>This will set the UserAgent used for POST request to {@code <botname>-<discriminator>/<api-version> (Javacord) DBots/<id>} |
| 51 | + * using the provided {@link org.javacord.api.DiscordApi DiscordApi instance}. |
| 52 | + * |
| 53 | + * @param api |
| 54 | + * The {@link org.javacord.api.DiscordApi DiscordApi instance} used to set the UserAgent. |
| 55 | + */ |
| 56 | + public PostAction(DiscordApi api){ |
| 57 | + this.requestHandler = new RequestHandler(String.format( |
| 58 | + "%s-%s/API_VERSION (Javacord) DBots/%s", |
| 59 | + api.getYourself().getName(), |
| 60 | + api.getYourself().getDiscriminator(), |
| 61 | + api.getYourself().getId() |
| 62 | + )); |
| 63 | + this.scheduler = requestHandler.getScheduler(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Disables the automatic posting of Stats. |
| 68 | + * <br>This essentially just performs a {@link java.util.concurrent.ScheduledExecutorService#shutdown() ScheduledExecutorService.shutdown()} |
| 69 | + * by calling the {@link #disableAutoPost(BotBlockAPI) disableAutoPost(null)} method. |
| 70 | + * |
| 71 | + * <p>Note that using this method will NOT make the scheduler wait for previously scheduled tasks to complete. |
| 72 | + * <br>If you want to wait for the tasks to complete use {@link #disableAutoPost(BotBlockAPI) disableAutoPost(BotBlockAPI)} or |
| 73 | + * {@link #disableAutoPost(long, TimeUnit) disableAutoPost(long, TimeUnit)} instead. |
| 74 | + * |
| 75 | + * @see java.util.concurrent.ScheduledExecutorService#shutdown() |
| 76 | + */ |
| 77 | + public void disableAutoPost(){ |
| 78 | + disableAutoPost(null); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Disables the automatic posting of Stats. |
| 83 | + * <br>Unlike {@link #disableAutoPost() disableAutoPost()} can you make the scheduler wait for all scheduled tasks to |
| 84 | + * finish, or to time out after n minutes by providing the {@link org.botblock.javabotblockapi.core.BotBlockAPI BotBlock instance}. |
| 85 | + * |
| 86 | + * <p>Passing null as argument will just perform a {@link java.util.concurrent.ScheduledExecutorService#shutdown() ScheduledExecutorService.shutdown()} |
| 87 | + * similar to what the disableAutoPost() method does. |
| 88 | + * |
| 89 | + * <p>If you want to use a different delay than what you've set in the BotBlockAPI instance, can you use |
| 90 | + * {@link #disableAutoPost(long, TimeUnit) disableAutoPost(long, TimeUnit)} instead. |
| 91 | + * |
| 92 | + * <p>This method may throw a {@link java.lang.InterruptedException InterruptedException} in the terminal. |
| 93 | + * |
| 94 | + * @param botBlockAPI |
| 95 | + * The {@link org.botblock.javabotblockapi.core.BotBlockAPI BotBlockAPI instance} or null to just perform a shutdown. |
| 96 | + * |
| 97 | + * @see java.util.concurrent.ScheduledExecutorService#shutdown() |
| 98 | + * @see java.util.concurrent.ScheduledExecutorService#awaitTermination(long, TimeUnit) |
| 99 | + */ |
| 100 | + public void disableAutoPost(@Nullable BotBlockAPI botBlockAPI){ |
| 101 | + if(botBlockAPI != null){ |
| 102 | + disableAutoPost(botBlockAPI.getUpdateDelay(), TimeUnit.MINUTES); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + scheduler.shutdown(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Disables the automatic posting of Stats. |
| 111 | + * <br>Unlike {@link #disableAutoPost() disableAutoPost()} can you make the scheduler wait for all scheduled tasks to |
| 112 | + * finish, or to time out after a specified time frame. |
| 113 | + * |
| 114 | + * <p>This method may throw a {@link java.lang.InterruptedException InterruptedException} in the terminal. |
| 115 | + * |
| 116 | + * @param time |
| 117 | + * The amount of time to wait for scheduled executions to finish before the Scheduler would time out. |
| 118 | + * @param timeUnit |
| 119 | + * The {@link java.util.concurrent.TimeUnit TimeUnit} to use. |
| 120 | + * |
| 121 | + * @see java.util.concurrent.ScheduledExecutorService#awaitTermination(long, TimeUnit) |
| 122 | + */ |
| 123 | + public void disableAutoPost(long time, @Nonnull TimeUnit timeUnit){ |
| 124 | + try{ |
| 125 | + scheduler.shutdown(); |
| 126 | + scheduler.awaitTermination(time, timeUnit); |
| 127 | + }catch(InterruptedException ex){ |
| 128 | + ex.printStackTrace(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Starts a {@link java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(Runnable, long, long, TimeUnit) scheduleAtFixedRate} |
| 134 | + * task, which will post the statistics of the provided {@link org.javacord.api.DiscordApi DiscordApi instance} every n minutes. |
| 135 | + * |
| 136 | + * <p>If the post can't be performed - either by getting a {@link org.botblock.javabotblockapi.core.exceptions.RatelimitedException RateLimitedException} |
| 137 | + * or by getting an {@link java.io.IOException IOException} - will the exception be caught and a Stacktrace printed. |
| 138 | + * |
| 139 | + * <p>The scheduler will wait an initial delay of 1 minute and then performs a task every n minutes, where n is the |
| 140 | + * time set in {@link org.botblock.javabotblockapi.core.BotBlockAPI.Builder#setUpdateDelay(Integer) BotBlockAPI.Builder.setUpdateDelay(Integer)} |
| 141 | + * (default is 30 minutes). |
| 142 | + * |
| 143 | + * @param api |
| 144 | + * The {@link org.javacord.api.DiscordApi DiscordApi instance} to post stats from. |
| 145 | + * @param botBlockAPI |
| 146 | + * The {@link org.botblock.javabotblockapi.core.BotBlockAPI BotBlockAPI instance} to use. |
| 147 | + */ |
| 148 | + public void enableAutoPost(@Nonnull DiscordApi api, @Nonnull BotBlockAPI botBlockAPI){ |
| 149 | + scheduler.scheduleAtFixedRate(() -> { |
| 150 | + try{ |
| 151 | + postGuilds(api, botBlockAPI); |
| 152 | + }catch(IOException | RatelimitedException ex){ |
| 153 | + ex.printStackTrace(); |
| 154 | + } |
| 155 | + }, 1, botBlockAPI.getUpdateDelay(), TimeUnit.MINUTES); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Performs a POST request towards the BotBlock API using the information from the provided |
| 160 | + * {@link org.javacord.api.DiscordApi DiscordApi} and {@link org.botblock.javabotblockapi.core.BotBlockAPI BotBlock} instances. |
| 161 | + * |
| 162 | + * <p>If the provided DiscordApi instance is a sharded Bot (Amount of shards is larger than 1) will the request |
| 163 | + * contain the {@code shards} array alongside a {@code shard_count} field. |
| 164 | + * |
| 165 | + * @param api |
| 166 | + * The {@link org.javacord.api.DiscordApi DiscordApi instance} to post stats from. |
| 167 | + * @param botBlockAPI |
| 168 | + * The {@link org.botblock.javabotblockapi.core.BotBlockAPI BotBlockAPI instance} to use. |
| 169 | + * |
| 170 | + * @throws java.io.IOException |
| 171 | + * When the POST request wasn't successful. |
| 172 | + * @throws org.botblock.javabotblockapi.core.exceptions.RatelimitedException |
| 173 | + * When we get rate limited by the BotBlock API (returns error code 429). |
| 174 | + */ |
| 175 | + public void postGuilds(@Nonnull DiscordApi api, @Nonnull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{ |
| 176 | + JSONObject json = new JSONObject() |
| 177 | + .put("server_count", api.getServers().size()) |
| 178 | + .put("bot_id", api.getYourself().getId()); |
| 179 | + |
| 180 | + List<Long> shards = new ArrayList<>(); |
| 181 | + for(int i = 0; i < (api.getTotalShards() -1); i++) |
| 182 | + shards.add(1L); |
| 183 | + |
| 184 | + json.put("shards", new JSONArray(Arrays.deepToString(shards.toArray()))); |
| 185 | + botBlockAPI.getTokens().forEach(json::put); |
| 186 | + |
| 187 | + requestHandler.performPOST(json, botBlockAPI.getTokens().size()); |
| 188 | + } |
| 189 | +} |
0 commit comments