Skip to content
This repository was archived by the owner on Jul 26, 2019. It is now read-only.

Commit 09275ee

Browse files
committed
Remove JDA and ShardManager from BotBlockAPI
- Let the RequestHandle handle it.
1 parent 47ee0c7 commit 09275ee

File tree

4 files changed

+24
-215
lines changed

4 files changed

+24
-215
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins{
77
}
88

99
group = 'com.andre601'
10-
version = '1.0.1'
10+
version = '1.0.2'
1111

1212
sourceCompatibility = 1.8
1313

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
bintrayUser=
2-
bintrayApiKey=
1+
bintrayUser=andre601
2+
bintrayApiKey=0dff36a653798e30b2c9a8f1198cee460671e4fc

src/main/java/com/andre601/javabotblockapi/BotBlockAPI.java

Lines changed: 2 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,15 @@
1818
*/
1919
package com.andre601.javabotblockapi;
2020

21-
import net.dv8tion.jda.bot.sharding.ShardManager;
22-
import net.dv8tion.jda.core.JDA;
2321
import org.apache.commons.lang3.ObjectUtils;
2422
import org.jetbrains.annotations.NotNull;
2523

2624
import java.util.HashMap;
2725
import java.util.Map;
2826

2927
public class BotBlockAPI{
30-
private Map<String, String> authTokens = new HashMap<>();
31-
28+
private Map<String, String> authTokens;
3229
private int updateInterval;
33-
private boolean jdaDisabled;
34-
private JDA jda;
35-
private ShardManager shardManager;
3630

3731
/**
3832
* Creates an instance of BotBlockAPI with the provided api tokens (as Map) and update interval.
@@ -45,65 +39,12 @@ public class BotBlockAPI{
4539
public BotBlockAPI(@NotNull Map<String, String> authTokens, int updateInterval){
4640
this.authTokens = authTokens;
4741
this.updateInterval = updateInterval;
48-
this.jdaDisabled = true;
49-
this.jda = null;
50-
this.shardManager = null;
51-
}
52-
53-
/**
54-
* Creates an instance of BotBlockAPI with the provided api tokens (as Map), update interval and
55-
* {@link net.dv8tion.jda.core.JDA JDA instance}.
56-
*
57-
* @param authTokens
58-
* A Map of sites and their tokens. May not be null.
59-
* @param updateInterval
60-
* The update interval to set.
61-
* @param jda
62-
* An instance of {@link net.dv8tion.jda.core.JDA JDA}. May not be null.
63-
*/
64-
public BotBlockAPI(@NotNull Map<String, String> authTokens, int updateInterval, @NotNull JDA jda){
65-
this.authTokens = authTokens;
66-
this.updateInterval = updateInterval;
67-
this.jdaDisabled = false;
68-
this.jda = jda;
69-
this.shardManager = null;
70-
}
71-
72-
/**
73-
* Creates an instance of BotBlockAPI with the provided api tokens (as Map), update interval and
74-
* {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager instance}.
75-
*
76-
* @param authTokens
77-
* A Map of sites and their tokens. May not be null.
78-
* @param updateInterval
79-
* The update interval to set.
80-
* @param shardManager
81-
* An instance of {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager}.
82-
*/
83-
public BotBlockAPI(@NotNull Map<String, String> authTokens, int updateInterval, @NotNull ShardManager shardManager){
84-
this.authTokens = authTokens;
85-
this.updateInterval = updateInterval;
86-
this.jdaDisabled = false;
87-
this.jda = null;
88-
this.shardManager = shardManager;
8942
}
9043

9144
Map<String, String> getAuthTokens(){
9245
return authTokens;
9346
}
9447

95-
boolean isJdaDisabled(){
96-
return jdaDisabled;
97-
}
98-
99-
JDA getJDA(){
100-
return jda;
101-
}
102-
103-
ShardManager getShardManager(){
104-
return shardManager;
105-
}
106-
10748
int getUpdateInterval(){
10849
return updateInterval;
10950
}
@@ -113,37 +54,13 @@ int getUpdateInterval(){
11354
*/
11455
public static class Builder{
11556
private Map<String, String> authTokens = new HashMap<>();
116-
11757
private int updateInterval = 30;
118-
private boolean jdaDisabled = false;
119-
private JDA jda = null;
120-
private ShardManager shardManager = null;
12158

12259
/**
12360
* Empty Builder class
12461
*/
12562
public Builder(){}
12663

127-
/**
128-
* Constructor that also sets the {@link net.dv8tion.jda.core.JDA JDA instance}.
129-
*
130-
* @param jda
131-
* The instance of {@link net.dv8tion.jda.core.JDA JDA}.
132-
*/
133-
public Builder(JDA jda){
134-
this.jda = jda;
135-
}
136-
137-
/**
138-
* Constructor that also sets the {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager instance}.
139-
*
140-
* @param shardManager
141-
* The instance of {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager}.
142-
*/
143-
public Builder(ShardManager shardManager){
144-
this.shardManager = shardManager;
145-
}
146-
14764
/**
14865
* Adds the provided Site name and token to the Map.
14966
* <br>If there is already an entry with the same key, it will be overwritten.
@@ -210,100 +127,13 @@ public Builder setUpdateInteval(int updateInterval){
210127
return this;
211128
}
212129

213-
/**
214-
* Sets if an instance of {@link net.dv8tion.jda.core.JDA JDA} or {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager}
215-
* need to be set.
216-
* <br>{@code true} means that it is *NOT* required to set. Default is false.
217-
*
218-
* <p><b>This will be ignored when either {@link #setJDA(JDA)} or {@link #setShardManager(ShardManager)} are set!</b>
219-
*
220-
* @param disable
221-
* The boolean to set if JDA/ShardManager is required. True means it's *NOT* required.
222-
*
223-
* @return The Builder after the boolean was set. Useful for chaining.
224-
*/
225-
public Builder disableJDA(boolean disable){
226-
this.jdaDisabled = disable;
227-
228-
return this;
229-
}
230-
231-
/**
232-
* Sets the instance of {@link net.dv8tion.jda.core.JDA JDA}. This will be ignored when {@link #setShardManager(ShardManager)}
233-
* is used!
234-
* <br>It will also disable {@link #disableJDA(boolean)} (set it to false) when it was set.
235-
*
236-
* <p>You can as an alternative define JDA directly through the constructor.
237-
*
238-
* <p><b>Example:</b>
239-
* <pre><code>
240-
* JDA jda = // Getting the JDA from somewhere
241-
*
242-
* BotBlockAPI api = new BotBlockAPI.Builder(jda) // Setting the JDA
243-
* // Adding sites through addAuthToken(String, String) and the build it with build()
244-
* </code></pre>
245-
*
246-
* @param jda
247-
* The instance of {@link net.dv8tion.jda.core.JDA JDA} to use. May not be null.
248-
*
249-
* @return The Builder after JDA was set. Useful for chaining.
250-
*/
251-
public Builder setJDA(@NotNull JDA jda){
252-
if(jdaDisabled)
253-
jdaDisabled = false;
254-
255-
this.jda = jda;
256-
257-
return this;
258-
}
259-
260-
/**
261-
* Sets the instance of {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager}.
262-
* <br>This will disable {@link #disableJDA(boolean)} (set it to false) when it was set.
263-
*
264-
* <p>You can as an alternative define ShardManager directly through the constructor.
265-
*
266-
* <p><b>Example:</b>
267-
* <pre><code>
268-
* ShardManager shardManager = // Getting the ShardManager from somewhere
269-
*
270-
* BotBlockAPI api = new BotBlockAPI.Builder(shardManager) // Setting the ShardManager.
271-
* // Adding sites through addAuthToken(String, String) and the build it with build()
272-
* </code></pre>
273-
*
274-
* @param shardManager
275-
* The instance of {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} to use. May not be null.
276-
*
277-
* @return The Builder after ShardManager was set. Useful for chaining.
278-
*/
279-
public Builder setShardManager(@NotNull ShardManager shardManager){
280-
if(jdaDisabled)
281-
jdaDisabled = false;
282-
283-
this.shardManager = shardManager;
284-
285-
return this;
286-
}
287-
288130
/**
289131
* Builds the instance of {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI}.
290132
*
291-
* @throws NullPointerException
292-
* When JDA nor ShardManager are set (null) and {@link #disableJDA(boolean)} is false.
293-
*
294133
* @return The built, usable {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI}.
295134
*/
296135
public BotBlockAPI build(){
297-
if(shardManager != null)
298-
return new BotBlockAPI(authTokens, updateInterval, shardManager);
299-
else
300-
if(jda != null)
301-
return new BotBlockAPI(authTokens, updateInterval, jda);
302-
else
303-
if(jdaDisabled)
304-
return new BotBlockAPI(authTokens, updateInterval);
305-
else
306-
throw new NullPointerException("disableJDA(Boolean) is false and JDA as-well as ShardManager are null!");
136+
return new BotBlockAPI(authTokens, updateInterval);
307137
}
308138
}
309139
}

src/main/java/com/andre601/javabotblockapi/RequestHandler.java

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@
2828
import org.apache.commons.lang3.ObjectUtils;
2929
import org.jetbrains.annotations.NotNull;
3030
import org.json.JSONArray;
31+
import org.json.JSONException;
3132
import org.json.JSONObject;
3233

3334
import java.io.IOException;
34-
import java.util.ArrayList;
35-
import java.util.Arrays;
36-
import java.util.List;
37-
import java.util.Objects;
35+
import java.util.*;
3836
import java.util.concurrent.Executors;
3937
import java.util.concurrent.ScheduledExecutorService;
4038
import java.util.concurrent.TimeUnit;
@@ -50,24 +48,6 @@ public class RequestHandler {
5048

5149
private JSONObject json = new JSONObject();
5250

53-
/**
54-
* Creates an instance of this class and sets the values defined in {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI}.
55-
* <br>This can only be used when you set either a ShardManager or a JDA instance through their respective method.
56-
*
57-
* @param botBlockAPI
58-
* An instance of {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI}. Can't be null.
59-
*/
60-
public RequestHandler(@NotNull BotBlockAPI botBlockAPI){
61-
if(botBlockAPI.getShardManager() != null){
62-
new RequestHandler(botBlockAPI.getShardManager(), botBlockAPI);
63-
}else
64-
if(botBlockAPI.getJDA() != null){
65-
new RequestHandler(botBlockAPI.getJDA(), botBlockAPI);
66-
}else{
67-
throw new NullPointerException("You need to set a JDA or ShardManager instance with BotBlockAPI!");
68-
}
69-
}
70-
7151
/**
7252
* Creates an instance of this class and sets the {@link net.dv8tion.jda.core.JDA JDA instance} and
7353
* {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance}.
@@ -116,7 +96,7 @@ public RequestHandler(@NotNull ShardManager shardManager, @NotNull BotBlockAPI b
11696

11797
List<Integer> shards = new ArrayList<>();
11898
for(JDA jda : shardManager.getShards())
119-
shards.add((int)jda.getGuildCache().size());
99+
shards.add(jda.getGuilds().size());
120100

121101
json.put("shards", new JSONArray(Arrays.deepToString(shards.toArray())));
122102

@@ -146,7 +126,6 @@ public RequestHandler(long botId, int guilds, @NotNull BotBlockAPI botBlockAPI){
146126
/**
147127
* Creates an instance of this class and sets the bots ID, the guild count and the
148128
* {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI}.
149-
* <br>This is essentially a shortcut to {@link #RequestHandler(String, int, BotBlockAPI)}.
150129
*
151130
* @param botId
152131
* The ID of the bot. Can't be null.
@@ -185,17 +164,15 @@ public void postGuilds() throws IOException, RatelimitedException{
185164
/**
186165
* Starts a scheduler to auto-post the guild counts to the BotBlock API.
187166
*
188-
* @throws IllegalStateException
189-
* When {@link com.andre601.javabotblockapi.BotBlockAPI.Builder#disableJDA(boolean) BotBlockAPI.Builder#disableJDA(Boolean)}
190-
* is true.
191167
* @throws NullPointerException
192-
* When {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI} is null.
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.
193170
*/
194171
public void startAutoPosting(){
195172
Objects.requireNonNull(botBlockAPI, "BotBlockAPI may not be null.");
196173

197-
if(botBlockAPI.isJdaDisabled())
198-
throw new IllegalStateException("startAutoPosting can't be used while disableJDA(Boolean) is true!");
174+
if(!ObjectUtils.anyNotNull(jda, shardManager))
175+
throw new NullPointerException("startAutoPost() requires either JDA or ShardManager!");
199176

200177
scheduler.scheduleAtFixedRate(() -> {
201178
try {
@@ -217,9 +194,6 @@ private void performRequest() throws IOException, RatelimitedException{
217194
Objects.requireNonNull(json, "JSON may not be null.");
218195
Objects.requireNonNull(id, "Id may not be null.");
219196

220-
if(!ObjectUtils.anyNotNull(jda, shardManager) && !botBlockAPI.isJdaDisabled())
221-
throw new NullPointerException("ShardManager AND JDA can't be null! Either set one or use disableJDA(true).");
222-
223197
RequestBody body = RequestBody.create(null, json.toString());
224198
Request request = new Request.Builder()
225199
.url("https://botblock.org/api/count")
@@ -248,13 +222,18 @@ private void performRequest() throws IOException, RatelimitedException{
248222

249223
List<String> sites = new ArrayList<>();
250224
for(String key : failures.keySet()){
251-
JSONArray array = failures.getJSONArray(key);
252-
sites.add(String.format(
253-
"Name: %s, Error code: %d, Error Message: %s",
254-
key,
255-
array.getInt(0),
256-
array.getString(1)
257-
));
225+
try {
226+
JSONArray array = failures.getJSONArray(key);
227+
sites.add(String.format(
228+
"Name: %s, Error code: %d, Error Message: %s",
229+
key,
230+
array.getInt(0),
231+
array.getString(1)
232+
));
233+
}catch(JSONException ex){
234+
Map<String, Object> notFound = failures.toMap();
235+
sites.add("Errors: " + notFound.toString());
236+
}
258237
}
259238

260239
throw new IOException(String.format(

0 commit comments

Comments
 (0)