@@ -41,142 +41,196 @@ public class RequestHandler {
4141 private ScheduledExecutorService scheduler = Executors .newSingleThreadScheduledExecutor ();
4242 private final OkHttpClient CLIENT = new OkHttpClient ();
4343
44- private JDA jda = null ;
45- private ShardManager shardManager = null ;
4644 private String id = null ;
47- private BotBlockAPI botBlockAPI ;
4845
4946 private JSONObject json = new JSONObject ();
5047
48+ public RequestHandler (){}
49+
5150 /**
52- * Creates an instance of this class and sets the {@link net.dv8tion.jda.core.JDA JDA instance} and
53- * {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance}.
54- * <br>If the JDA instance is part of sharding (Has ShardInfo) then {@code shard_id} and {@code shard_count} are set too.
51+ * Posts guilds from the provided {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager}.
5552 *
56- * <p>It is recommended to use {@link #RequestHandler(ShardManager, BotBlockAPI) RequestHandler(ShardManager, BotBlockAPI)}
57- * when having a sharded bot.
53+ * @param shardManager
54+ * The {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager instance} that should be used.
55+ * @param botBlockAPI
56+ * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
5857 *
59- * @param jda
60- * The {@link net.dv8tion.jda.core.JDA JDA instance} to use. Can't be null.
61- * @param botBlockAPI
62- * An instance of {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI}. Can't be null.
58+ * @throws IOException
59+ * When the post request couldn't be performed properly.
60+ * @throws RatelimitedException
61+ * When the Bot (IP or ID) got ratelimited.
62+ *
63+ * @see #postGuilds(JDA, BotBlockAPI) for posting with JDA.
6364 */
64- public RequestHandler (@ NotNull JDA jda , @ NotNull BotBlockAPI botBlockAPI ){
65- this .jda = jda ;
66- this .botBlockAPI = botBlockAPI ;
67- this .id = jda .getSelfUser ().getId ();
65+ public void postGuilds (@ NotNull ShardManager shardManager , @ NotNull BotBlockAPI botBlockAPI ) throws IOException , RatelimitedException {
66+ this .id = shardManager .getShardById (0 ).getSelfUser ().getId ();
6867
69- json .put ("server_count" , jda .getGuildCache ().size ())
70- .put ("bot_id" , id );
68+ json .put ("server_count" , shardManager .getGuilds ().size ())
69+ .put ("bot_id" , id )
70+ .put ("shard_count" , shardManager .getShards ().size ());
7171
72- if (jda .getShardInfo () != null )
73- json .put ("shard_id" , jda .getShardInfo ().getShardId ())
74- .put ("shard_count" , jda .getShardInfo ().getShardTotal ());
72+ List <Integer > shards = new ArrayList <>();
73+ for (JDA jda : shardManager .getShards ())
74+ shards .add (jda .getGuilds ().size ());
75+
76+ json .put ("shards" , new JSONArray (Arrays .deepToString (shards .toArray ())));
7577
7678 botBlockAPI .getAuthTokens ().forEach (json ::put );
79+
80+ performRequest ();
7781 }
7882
7983 /**
80- * Creates an instance of this class and sets the {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager instance} and
81- * {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} .
84+ * Posts the guilds from the provided {@link net.dv8tion.jda.core.JDA JDA}.
85+ * <br>If the Bot is sharded (JDA has ShardInfo) then the {@code shard_id} and {@code shard_count} are posted too .
8286 *
83- * @param shardManager
84- * The {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager instance} to use. Can't be null.
85- * @param botBlockAPI
86- * An instance of {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI}. Can't be null.
87+ * <p>If you use this on a sharded bot, better use {@link #postGuilds(ShardManager, BotBlockAPI)}.
88+ *
89+ * @param jda
90+ * The {@link net.dv8tion.jda.core.JDA JDA instance} that should be used.
91+ * @param botBlockAPI
92+ * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
93+ *
94+ * @throws IOException
95+ * When the post request couldn't be performed properly.
96+ * @throws RatelimitedException
97+ * When the Bot (IP or ID) got ratelimited.
98+ *
99+ * @see #postGuilds(ShardManager, BotBlockAPI) for posting with ShardManager.
87100 */
88- public RequestHandler (@ NotNull ShardManager shardManager , @ NotNull BotBlockAPI botBlockAPI ){
89- this .shardManager = shardManager ;
90- this .botBlockAPI = botBlockAPI ;
91- this .id = shardManager .getShardById (0 ).getSelfUser ().getId ();
92-
93- json .put ("server_count" , shardManager .getGuildCache ().size ())
94- .put ("bot_id" , id )
95- .put ("shard_count" , shardManager .getShardCache ().size ());
101+ public void postGuilds (@ NotNull JDA jda , @ NotNull BotBlockAPI botBlockAPI ) throws IOException , RatelimitedException {
102+ this .id = jda .getSelfUser ().getId ();
96103
97- List <Integer > shards = new ArrayList <>();
98- for (JDA jda : shardManager .getShards ())
99- shards .add (jda .getGuilds ().size ());
104+ json .put ("server_count" , jda .getGuildCache ().size ())
105+ .put ("bot_id" , id );
100106
101- json .put ("shards" , new JSONArray (Arrays .deepToString (shards .toArray ())));
107+ if (jda .getShardInfo () != null )
108+ json .put ("shard_id" , jda .getShardInfo ().getShardId ())
109+ .put ("shard_count" , jda .getShardInfo ().getShardTotal ());
102110
103111 botBlockAPI .getAuthTokens ().forEach (json ::put );
112+
113+ performRequest ();
104114 }
105115
106116 /**
107- * Creates an instance of this class and sets the bots ID, the guild count and the
108- * {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI}.
109- * <br>This is essentially a shortcut to {@link #RequestHandler(String, int, BotBlockAPI)}.
110- *
111- * @param botId
112- * The ID of the bot.
113- * @param guilds
114- * The guild count.
115- * @param botBlockAPI
116- * An instance of {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI}. Can't be null.
117- *
118- * @see #RequestHandler(String, int, BotBlockAPI) for full method.
119- * @see #RequestHandler(ShardManager, BotBlockAPI) for use with ShardManager.
120- * @see #RequestHandler(JDA, BotBlockAPI) for use with JDA.
117+ * Posts the provided guilds from the provided Bot id.
118+ * <br>This is a shortcut to {@link #postGuilds(String, int, BotBlockAPI)}.
119+ *
120+ * @param botId
121+ * The ID (as long) of the bot.
122+ * @param guilds
123+ * The guilds the bot is in.
124+ * @param botBlockAPI
125+ * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
126+ *
127+ * @throws IOException
128+ * When the post request couldn't be performed properly.
129+ * @throws RatelimitedException
130+ * When the Bot (IP or ID) got ratelimited.
131+ *
132+ * @see #postGuilds(String, int, BotBlockAPI) for the full method.
121133 */
122- public RequestHandler ( long botId , int guilds , @ NotNull BotBlockAPI botBlockAPI ){
123- new RequestHandler (Long .toString (botId ), guilds , botBlockAPI );
134+ public void postGuilds ( Long botId , int guilds , @ NotNull BotBlockAPI botBlockAPI ) throws IOException , RatelimitedException {
135+ postGuilds (Long .toString (botId ), guilds , botBlockAPI );
124136 }
125137
126138 /**
127- * Creates an instance of this class and sets the bots ID, the guild count and the
128- * {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI}.
129- *
130- * @param botId
131- * The ID of the bot. Can't be null.
132- * @param guilds
133- * The guild count.
134- * @param botBlockAPI
135- * An instance of {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI}. Can't be null.
136- *
137- * @see #RequestHandler(ShardManager, BotBlockAPI) for use with ShardManager.
138- * @see #RequestHandler(JDA, BotBlockAPI) for use with JDA.
139+ * Posts the provided guilds from the provided Bot id.
140+ *
141+ * @param botId
142+ * The ID (as String) of the bot.
143+ * @param guilds
144+ * The guilds the bot is in.
145+ * @param botBlockAPI
146+ * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
147+ *
148+ * @throws IOException
149+ * When the post request couldn't be performed properly.
150+ * @throws RatelimitedException
151+ * When the Bot (IP or ID) got ratelimited.
139152 */
140- public RequestHandler (@ NotNull String botId , int guilds , @ NotNull BotBlockAPI botBlockAPI ){
153+ public void postGuilds (@ NotNull String botId , int guilds , @ NotNull BotBlockAPI botBlockAPI ) throws IOException , RatelimitedException {
141154 json .put ("server_count" , guilds )
142155 .put ("bot_id" , botId );
143156
144157 botBlockAPI .getAuthTokens ().forEach (json ::put );
158+
159+ performRequest ();
145160 }
146161
147162 /**
148- * Performs a request to post the saved informations to the BotBlock API.
149- * <br>Informations are set through calling RequestHandler and provide the informations through it .
163+ * Starts a scheduler that posts the guilds from the provided {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager}
164+ * every X minutes .
150165 *
151- * @throws IOException
152- * When the request failed.
153- * @throws RatelimitedException
154- * When we got ratelimited by the API.
155- * @throws NullPointerException
156- * When {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI} is null.
166+ * @param shardManager
167+ * The {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager instance} that should be used.
168+ * @param botBlockAPI
169+ * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
157170 */
158- public void postGuilds () throws IOException , RatelimitedException {
159- Objects .requireNonNull (botBlockAPI , "BotBlockAPI may not be null." );
160-
161- performRequest ();
171+ public void startAutoPosting (@ NotNull ShardManager shardManager , @ NotNull BotBlockAPI botBlockAPI ){
172+ scheduler .scheduleAtFixedRate (() -> {
173+ try {
174+ postGuilds (shardManager , botBlockAPI );
175+ }catch (IOException | RatelimitedException ex ){
176+ ex .printStackTrace ();
177+ }
178+ }, botBlockAPI .getUpdateInterval (), botBlockAPI .getUpdateInterval (), TimeUnit .MINUTES );
162179 }
163180
164181 /**
165- * Starts a scheduler to auto-post the guild counts to the BotBlock API.
182+ * Starts a scheduler that posts the guilds from the provided {@link net.dv8tion.jda.core.JDA JDA}
183+ * every X minutes.
166184 *
167- * @throws NullPointerException
168- * When {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI} is null or both
169- * {@link net.dv8tion.jda.core.JDA JDA} and {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} are null.
185+ * @param jda
186+ * The {@link net.dv8tion.jda.core.JDA JDA instance} that should be used.
187+ * @param botBlockAPI
188+ * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
170189 */
171- public void startAutoPosting (){
172- Objects .requireNonNull (botBlockAPI , "BotBlockAPI may not be null." );
190+ public void startAutoPosting (@ NotNull JDA jda , @ NotNull BotBlockAPI botBlockAPI ){
191+ scheduler .scheduleAtFixedRate (() -> {
192+ try {
193+ postGuilds (jda , botBlockAPI );
194+ }catch (IOException | RatelimitedException ex ){
195+ ex .printStackTrace ();
196+ }
197+ }, botBlockAPI .getUpdateInterval (), botBlockAPI .getUpdateInterval (), TimeUnit .MINUTES );
198+ }
173199
174- if (!ObjectUtils .anyNotNull (jda , shardManager ))
175- throw new NullPointerException ("startAutoPost() requires either JDA or ShardManager!" );
200+ /**
201+ * Starts a scheduler that posts the provided guilds of the provided bot id every X minutes.
202+ *
203+ * @param botId
204+ * The ID (as Long) of the bot.
205+ * @param guilds
206+ * The guilds the bot is in.
207+ * @param botBlockAPI
208+ * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
209+ */
210+ public void startAutoPosting (Long botId , int guilds , @ NotNull BotBlockAPI botBlockAPI ){
211+ scheduler .scheduleAtFixedRate (() -> {
212+ try {
213+ postGuilds (botId , guilds , botBlockAPI );
214+ }catch (IOException | RatelimitedException ex ){
215+ ex .printStackTrace ();
216+ }
217+ }, botBlockAPI .getUpdateInterval (), botBlockAPI .getUpdateInterval (), TimeUnit .MINUTES );
218+ }
176219
220+ /**
221+ * Starts a scheduler that posts the provided guilds of the provided bot id every X minutes.
222+ *
223+ * @param botId
224+ * The ID (as String) of the bot.
225+ * @param guilds
226+ * The guilds the bot is in.
227+ * @param botBlockAPI
228+ * The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
229+ */
230+ public void startAutoPosting (@ NotNull String botId , int guilds , @ NotNull BotBlockAPI botBlockAPI ){
177231 scheduler .scheduleAtFixedRate (() -> {
178- try {
179- postGuilds ();
232+ try {
233+ postGuilds (botId , guilds , botBlockAPI );
180234 }catch (IOException | RatelimitedException ex ){
181235 ex .printStackTrace ();
182236 }
@@ -205,6 +259,9 @@ private void performRequest() throws IOException, RatelimitedException{
205259 try (Response response = CLIENT .newCall (request ).execute ()){
206260 Objects .requireNonNull (response .body (), "Received empty body from BotBlocks API." );
207261
262+ if (response .body ().string ().isEmpty ())
263+ throw new NullPointerException ("Received empty body from BotBlocks API." );
264+
208265 if (!response .isSuccessful ()){
209266 if (response .code () == 429 )
210267 throw new RatelimitedException (response .body ().string ());
0 commit comments