Skip to content

Commit 57bb586

Browse files
committed
First steps for new v2.5 methods
1 parent 53a64a1 commit 57bb586

File tree

7 files changed

+331
-6
lines changed

7 files changed

+331
-6
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1515
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
1616
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17-
*
1817
*/
1918
package com.andre601.javabotblockapi;
2019

@@ -63,7 +62,7 @@ public BotBlockAPI(@NotNull Map<String, String> authTokens, int updateInterval){
6362
this.updateInterval = updateInterval;
6463
}
6564

66-
Map<String, String> getAuthTokens(){
65+
public Map<String, String> getAuthTokens(){
6766
return authTokens;
6867
}
6968

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1515
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
1616
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17-
*
1817
*/
1918
package com.andre601.javabotblockapi;
2019

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1515
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
1616
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17-
*
1817
*/
1918
package com.andre601.javabotblockapi;
2019

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1515
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
1616
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17-
*
1817
*/
1918
package com.andre601.javabotblockapi;
2019

src/main/java/com/andre601/javabotblockapi/exceptions/RatelimitedException.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1515
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
1616
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17-
*
1817
*/
1918
package com.andre601.javabotblockapi.exceptions;
2019

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright 2019 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 com.andre601.javabotblockapi.requests;
20+
21+
import com.andre601.javabotblockapi.BotBlockAPI;
22+
import com.andre601.javabotblockapi.exceptions.RatelimitedException;
23+
import net.dv8tion.jda.api.JDA;
24+
import net.dv8tion.jda.api.sharding.ShardManager;
25+
import org.jetbrains.annotations.NotNull;
26+
import org.json.JSONArray;
27+
import org.json.JSONObject;
28+
29+
import java.io.IOException;
30+
import java.util.ArrayList;
31+
import java.util.Arrays;
32+
import java.util.List;
33+
34+
public class PostAction{
35+
36+
private boolean disableCache;
37+
private final RequestHandler REQUEST_HANDLER = new RequestHandler();
38+
39+
public PostAction(){
40+
this.disableCache = false;
41+
}
42+
43+
public PostAction(boolean disableCache){
44+
this.disableCache = disableCache;
45+
}
46+
47+
/**
48+
* Posts the guild count provided through the {@link net.dv8tion.jda.api.JDA JDA instance}.
49+
* <br><b>It's recommended to use {@link #postGuilds(ShardManager, BotBlockAPI) postGuilds(ShardManager, BotBlockAPI}
50+
* if you're using a sharded bot.</b>
51+
*
52+
* <p>When the amount of shards a bot has is bigger than one will shard_id and shard_count be added.
53+
*
54+
* @param jda
55+
* The {@link net.dv8tion.jda.api.JDA JDA instance}.
56+
* @param botBlockAPI
57+
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance}.
58+
*
59+
* @throws IOException
60+
* When the post request couldn't be performed.
61+
* @throws RatelimitedException
62+
* When we exceed the rate-limit of the BotBlock API.
63+
*/
64+
public void postGuilds(@NotNull JDA jda, @NotNull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{
65+
JSONObject json = new JSONObject()
66+
.put("server_count", jda.getGuilds().size())
67+
.put("bot_id", jda.getSelfUser().getId());
68+
69+
if(jda.getShardInfo().getShardTotal() > 1)
70+
json.put("shard_id", jda.getShardInfo().getShardId())
71+
.put("shard_count", jda.getShardInfo().getShardTotal());
72+
73+
botBlockAPI.getAuthTokens().forEach(json::put);
74+
75+
REQUEST_HANDLER.performPOST(json);
76+
}
77+
78+
/**
79+
* Posts the guild count with the provided bot id.
80+
*
81+
* @param botId
82+
* The ID of the bot.
83+
* @param guilds
84+
* The guild count.
85+
* @param botBlockAPI
86+
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance}.
87+
*
88+
* @throws IOException
89+
* When the post request couldn't be performed.
90+
* @throws RatelimitedException
91+
* When we exceed the rate-limit of the BotBlock API.
92+
*/
93+
public void postGuilds(Long botId, int guilds, @NotNull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{
94+
postGuilds(Long.toString(botId), guilds, botBlockAPI);
95+
}
96+
97+
/**
98+
* Posts the guild count from the provided {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager instance}.
99+
* <br>The guild count of each shard will be added as an JSONArray.
100+
*
101+
* @param shardManager
102+
* The {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager instance}.
103+
* @param botBlockAPI
104+
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance}.
105+
*
106+
* @throws IOException
107+
* When the post request couldn't be performed.
108+
* @throws RatelimitedException
109+
* When we exceed the rate-limit of the BotBlock API.
110+
*/
111+
public void postGuilds(@NotNull ShardManager shardManager, @NotNull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{
112+
JDA jda = shardManager.getShardById(0);
113+
if(jda == null)
114+
throw new NullPointerException("Received shard was null!");
115+
116+
JSONObject json = new JSONObject()
117+
.put("server_count", shardManager.getGuilds().size())
118+
.put("bot_id", jda.getSelfUser().getId())
119+
.put("shard_count", shardManager.getShardCache().size());
120+
121+
List<Integer> shards = new ArrayList<>();
122+
for(JDA shard : shardManager.getShards())
123+
shards.add(shard.getGuilds().size());
124+
125+
json.put("shards", new JSONArray(Arrays.deepToString(shards.toArray())));
126+
botBlockAPI.getAuthTokens().forEach(json::put);
127+
128+
REQUEST_HANDLER.performPOST(json);
129+
}
130+
131+
/**
132+
* Posts the guild count with the provided bot id.
133+
*
134+
* @param botId
135+
* The ID of the bot.
136+
* @param guilds
137+
* The guild count.
138+
* @param botBlockAPI
139+
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance}.
140+
*
141+
* @throws IOException
142+
* When the post request couldn't be performed.
143+
* @throws RatelimitedException
144+
* When we exceed the rate-limit of the BotBlock API.
145+
*/
146+
public void postGuilds(@NotNull String botId, int guilds, @NotNull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{
147+
JSONObject json = new JSONObject()
148+
.put("server_count", guilds)
149+
.put("bot_id", botId);
150+
151+
botBlockAPI.getAuthTokens().forEach(json::put);
152+
153+
REQUEST_HANDLER.performPOST(json);
154+
}
155+
156+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright 2019 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 com.andre601.javabotblockapi.requests;
20+
21+
import com.andre601.javabotblockapi.exceptions.RatelimitedException;
22+
import com.github.benmanes.caffeine.cache.Cache;
23+
import com.github.benmanes.caffeine.cache.Caffeine;
24+
import okhttp3.*;
25+
import org.jetbrains.annotations.NotNull;
26+
import org.json.JSONArray;
27+
import org.json.JSONException;
28+
import org.json.JSONObject;
29+
30+
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
import java.util.Map;
34+
import java.util.concurrent.TimeUnit;
35+
36+
public class RequestHandler{
37+
38+
private final String BASE_URL = "https://botblock.org/api/";
39+
private final OkHttpClient CLIENT = new OkHttpClient();
40+
41+
private Cache<String, JSONObject> botCache = Caffeine.newBuilder()
42+
.expireAfterWrite(2, TimeUnit.MINUTES)
43+
.build();
44+
private Cache<String, JSONObject> listCache = Caffeine.newBuilder()
45+
.expireAfterWrite(2, TimeUnit.MINUTES)
46+
.build();
47+
48+
JSONObject performGetBot(@NotNull String id, boolean disableCache){
49+
String url = BASE_URL + "bots/" + id;
50+
51+
if(!disableCache)
52+
return botCache.get(id, k -> {
53+
try{
54+
return performGET(url);
55+
}catch(IOException | RatelimitedException ex){
56+
ex.printStackTrace();
57+
return null;
58+
}
59+
});
60+
61+
try{
62+
return performGET(url);
63+
}catch(IOException | RatelimitedException ex){
64+
ex.printStackTrace();
65+
return null;
66+
}
67+
}
68+
69+
JSONObject performGetList(@NotNull String id, boolean disableCache){
70+
String url = BASE_URL + "lists";
71+
72+
if(!disableCache)
73+
return listCache.get(id, k -> {
74+
try{
75+
return performGET(url);
76+
}catch(IOException | RatelimitedException ex){
77+
ex.printStackTrace();
78+
return null;
79+
}
80+
});
81+
82+
try{
83+
return performGET(url);
84+
}catch(IOException | RatelimitedException ex){
85+
ex.printStackTrace();
86+
return null;
87+
}
88+
}
89+
90+
private JSONObject performGET(@NotNull String url) throws IOException, RatelimitedException{
91+
Request request = new Request.Builder()
92+
.url(url)
93+
.build();
94+
95+
try(Response response = CLIENT.newCall(request).execute()){
96+
ResponseBody body = response.body();
97+
if(body == null)
98+
throw new NullPointerException("Received empty response body.");
99+
100+
if(body.string().isEmpty())
101+
throw new NullPointerException("Received empty response body.");
102+
103+
if(!response.isSuccessful()){
104+
if(response.code() == 429)
105+
throw new RatelimitedException(body.string());
106+
107+
throw new IOException(String.format(
108+
"Could not retrieve information. The server responded with error code %d (%s).",
109+
response.code(),
110+
response.message()
111+
));
112+
}
113+
114+
return new JSONObject(body.string());
115+
}
116+
}
117+
118+
void performPOST(@NotNull JSONObject json) throws IOException, RatelimitedException{
119+
String url = BASE_URL + "count";
120+
121+
RequestBody body = RequestBody.create(json.toString(), null);
122+
Request request = new Request.Builder()
123+
.url(url)
124+
.addHeader("Content-Type", "application/json")
125+
.post(body)
126+
.build();
127+
128+
try(Response response = CLIENT.newCall(request).execute()){
129+
ResponseBody responseBody = response.body();
130+
if(responseBody == null)
131+
throw new NullPointerException("Received empty response body.");
132+
133+
if(responseBody.string().isEmpty())
134+
throw new NullPointerException("Received empty response body.");
135+
136+
if(!response.isSuccessful()){
137+
if(response.code() == 429)
138+
throw new RatelimitedException(responseBody.string());
139+
140+
throw new IOException(String.format(
141+
"Could not post Guild count. The server responded with error code %d (%s)",
142+
response.code(),
143+
response.message()
144+
));
145+
}
146+
147+
JSONObject responseJson = new JSONObject(responseBody);
148+
if(responseJson.has("failure")){
149+
JSONObject failure = responseJson.getJSONObject("failure");
150+
151+
List<String> sites = new ArrayList<>();
152+
for(String key : failure.keySet()){
153+
try{
154+
JSONArray array = failure.getJSONArray(key);
155+
sites.add(String.format(
156+
"Site: %s, Code: %d, Message: %s",
157+
key,
158+
array.getInt(0),
159+
array.getString(1)
160+
));
161+
}catch(JSONException ex){
162+
Map<String, Object> notFound = failure.toMap();
163+
sites.add("Errors: " + notFound.toString());
164+
}
165+
}
166+
167+
throw new IOException(String.format(
168+
"One or multiple post requests failed! Response(s): %s",
169+
String.join(", ", sites)
170+
));
171+
}
172+
}
173+
}
174+
}

0 commit comments

Comments
 (0)