@@ -41,142 +41,196 @@ public class RequestHandler {
41
41
private ScheduledExecutorService scheduler = Executors .newSingleThreadScheduledExecutor ();
42
42
private final OkHttpClient CLIENT = new OkHttpClient ();
43
43
44
- private JDA jda = null ;
45
- private ShardManager shardManager = null ;
46
44
private String id = null ;
47
- private BotBlockAPI botBlockAPI ;
48
45
49
46
private JSONObject json = new JSONObject ();
50
47
48
+ public RequestHandler (){}
49
+
51
50
/**
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}.
55
52
*
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.
58
57
*
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.
63
64
*/
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 ();
68
67
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 ());
71
71
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 ())));
75
77
76
78
botBlockAPI .getAuthTokens ().forEach (json ::put );
79
+
80
+ performRequest ();
77
81
}
78
82
79
83
/**
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 .
82
86
*
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.
87
100
*/
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 ();
96
103
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 );
100
106
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 ());
102
110
103
111
botBlockAPI .getAuthTokens ().forEach (json ::put );
112
+
113
+ performRequest ();
104
114
}
105
115
106
116
/**
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.
121
133
*/
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 );
124
136
}
125
137
126
138
/**
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.
139
152
*/
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 {
141
154
json .put ("server_count" , guilds )
142
155
.put ("bot_id" , botId );
143
156
144
157
botBlockAPI .getAuthTokens ().forEach (json ::put );
158
+
159
+ performRequest ();
145
160
}
146
161
147
162
/**
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 .
150
165
*
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.
157
170
*/
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 );
162
179
}
163
180
164
181
/**
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.
166
184
*
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.
170
189
*/
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
+ }
173
199
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
+ }
176
219
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 ){
177
231
scheduler .scheduleAtFixedRate (() -> {
178
- try {
179
- postGuilds ();
232
+ try {
233
+ postGuilds (botId , guilds , botBlockAPI );
180
234
}catch (IOException | RatelimitedException ex ){
181
235
ex .printStackTrace ();
182
236
}
@@ -205,6 +259,9 @@ private void performRequest() throws IOException, RatelimitedException{
205
259
try (Response response = CLIENT .newCall (request ).execute ()){
206
260
Objects .requireNonNull (response .body (), "Received empty body from BotBlocks API." );
207
261
262
+ if (response .body ().string ().isEmpty ())
263
+ throw new NullPointerException ("Received empty body from BotBlocks API." );
264
+
208
265
if (!response .isSuccessful ()){
209
266
if (response .code () == 429 )
210
267
throw new RatelimitedException (response .body ().string ());
0 commit comments