|
30 | 30 | import java.util.ArrayList; |
31 | 31 | import java.util.Arrays; |
32 | 32 | import java.util.List; |
| 33 | +import java.util.concurrent.Executors; |
| 34 | +import java.util.concurrent.ScheduledExecutorService; |
| 35 | +import java.util.concurrent.TimeUnit; |
33 | 36 |
|
| 37 | +/** |
| 38 | + * Class to perform post actions with. |
| 39 | + * |
| 40 | + * <p>You can {@link #enableAutoPost(JDA, BotBlockAPI) post automatically} or {@link #postGuilds(JDA, BotBlockAPI) post manually}. |
| 41 | + */ |
34 | 42 | public class PostAction{ |
35 | 43 |
|
36 | | - private boolean disableCache; |
37 | 44 | private final RequestHandler REQUEST_HANDLER = new RequestHandler(); |
| 45 | + private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Empty constructor to create the PostAction instance. |
| 49 | + */ |
| 50 | + public PostAction(){} |
| 51 | + |
| 52 | + /** |
| 53 | + * Shuts down the scheduler, to stop the automatic posting. |
| 54 | + */ |
| 55 | + public void disableAutoPost(){ |
| 56 | + scheduler.shutdown(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Starts posting of the guild count each n minutes. |
| 61 | + * <br>The delay in which this happens is set using <code>{@link com.andre601.javabotblockapi.BotBlockAPI.Builder#setUpdateInteval(int)} BotBlockAPI.Builder#setUpdateInterval(int)}</code> |
| 62 | + * |
| 63 | + * <p><b>The scheduler will stop (cancel) the task, when an Exception appears!</b> |
| 64 | + * |
| 65 | + * @param jda |
| 66 | + * The {@link net.dv8tion.jda.api.JDA JDA instance} to use. |
| 67 | + * @param botBlockAPI |
| 68 | + * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} to use. |
| 69 | + */ |
| 70 | + public void enableAutoPost(@NotNull JDA jda, @NotNull BotBlockAPI botBlockAPI){ |
| 71 | + scheduler.scheduleAtFixedRate(() -> { |
| 72 | + try{ |
| 73 | + postGuilds(jda, botBlockAPI); |
| 74 | + }catch(IOException | RatelimitedException ex){ |
| 75 | + ex.printStackTrace(); |
| 76 | + } |
| 77 | + }, botBlockAPI.getUpdateInterval(), botBlockAPI.getUpdateInterval(), TimeUnit.MINUTES); |
| 78 | + } |
38 | 79 |
|
39 | | - public PostAction(){ |
40 | | - this.disableCache = false; |
| 80 | + /** |
| 81 | + * Starts posting of the guild count each n minutes. |
| 82 | + * <br>The delay in which this happens is set using <code>{@link com.andre601.javabotblockapi.BotBlockAPI.Builder#setUpdateInteval(int)} BotBlockAPI.Builder#setUpdateInterval(int)}</code> |
| 83 | + * |
| 84 | + * <p><b>The scheduler will stop (cancel) the task, when an Exception appears!</b> |
| 85 | + * |
| 86 | + * @param botId |
| 87 | + * The ID of the bot as Long. |
| 88 | + * @param guilds |
| 89 | + * The guild count. |
| 90 | + * @param botBlockAPI |
| 91 | + * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} to use. |
| 92 | + */ |
| 93 | + public void enableAutoPost(Long botId, int guilds, @NotNull BotBlockAPI botBlockAPI){ |
| 94 | + scheduler.scheduleAtFixedRate(() -> { |
| 95 | + try{ |
| 96 | + postGuilds(botId, guilds, botBlockAPI); |
| 97 | + }catch(IOException | RatelimitedException ex){ |
| 98 | + ex.printStackTrace(); |
| 99 | + } |
| 100 | + }, botBlockAPI.getUpdateInterval(), botBlockAPI.getUpdateInterval(), TimeUnit.MINUTES); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Starts posting of the guild count each n minutes. |
| 105 | + * <br>The delay in which this happens is set using <code>{@link com.andre601.javabotblockapi.BotBlockAPI.Builder#setUpdateInteval(int)} BotBlockAPI.Builder#setUpdateInterval(int)}</code> |
| 106 | + * |
| 107 | + * <p><b>The scheduler will stop (cancel) the task, when an Exception appears!</b> |
| 108 | + * |
| 109 | + * @param shardManager |
| 110 | + * The {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager instance} to use. |
| 111 | + * @param botBlockAPI |
| 112 | + * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} to use. |
| 113 | + */ |
| 114 | + public void enableAutoPost(@NotNull ShardManager shardManager, @NotNull BotBlockAPI botBlockAPI){ |
| 115 | + scheduler.scheduleAtFixedRate(() -> { |
| 116 | + try{ |
| 117 | + postGuilds(shardManager, botBlockAPI); |
| 118 | + }catch(IOException | RatelimitedException ex){ |
| 119 | + ex.printStackTrace(); |
| 120 | + } |
| 121 | + }, botBlockAPI.getUpdateInterval(), botBlockAPI.getUpdateInterval(), TimeUnit.MINUTES); |
41 | 122 | } |
42 | 123 |
|
43 | | - public PostAction(boolean disableCache){ |
44 | | - this.disableCache = disableCache; |
| 124 | + /** |
| 125 | + * Starts posting of the guild count each n minutes. |
| 126 | + * <br>The delay in which this happens is set using <code>{@link com.andre601.javabotblockapi.BotBlockAPI.Builder#setUpdateInteval(int)} BotBlockAPI.Builder#setUpdateInterval(int)}</code> |
| 127 | + * |
| 128 | + * <p><b>The scheduler will stop (cancel) the task, when an Exception appears!</b> |
| 129 | + * |
| 130 | + * @param botId |
| 131 | + * The ID of the bot as String. |
| 132 | + * @param guilds |
| 133 | + * The guild count. |
| 134 | + * @param botBlockAPI |
| 135 | + * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} to use. |
| 136 | + */ |
| 137 | + public void enableAutoPost(@NotNull String botId, int guilds, @NotNull BotBlockAPI botBlockAPI){ |
| 138 | + scheduler.scheduleAtFixedRate(() -> { |
| 139 | + try{ |
| 140 | + postGuilds(botId, guilds, botBlockAPI); |
| 141 | + }catch(IOException | RatelimitedException ex){ |
| 142 | + ex.printStackTrace(); |
| 143 | + } |
| 144 | + }, botBlockAPI.getUpdateInterval(), botBlockAPI.getUpdateInterval(), TimeUnit.MINUTES); |
45 | 145 | } |
46 | 146 |
|
47 | 147 | /** |
|
0 commit comments