Skip to content

Commit 03b5143

Browse files
authored
Merge pull request #172 from botblock/change/add-checks
Add checks and improve javadocs
2 parents 4aced40 + b2c75b4 commit 03b5143

File tree

7 files changed

+599
-262
lines changed

7 files changed

+599
-262
lines changed

build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins{
99
id 'net.kyori.blossom' version '1.1.0'
1010
}
1111

12-
def ver = new Version(major: 6, minor: 1, revision: 0)
12+
def ver = new Version(major: 6, minor: 2, patch: 0)
1313

1414
allprojects {
1515
apply plugin: 'com.jfrog.bintray'
@@ -62,6 +62,7 @@ allprojects {
6262

6363
// Discord Libs
6464
"https://ci.dv8tion.net/job/JDA/javadoc/",
65+
"https://docs.javacord.org/api/v/3.0.7/overview-summary.html",
6566

6667
// Java 8
6768
"https://docs.oracle.com/javase/8/docs/api/",
@@ -255,14 +256,14 @@ publishing {
255256
}
256257

257258
class Version{
258-
String major, minor, revision
259+
String major, minor, patch
259260

260261
static String getBuild(){
261262
System.getenv("BUILD_NUMBER") ? "_" + System.getenv("BUILD_NUMBER") :
262263
System.getProperty("BUILD_NUMBER") ? "_" + System.getProperty("BUILD_NUMBER") : ""
263264
}
264265

265266
String toString(){
266-
"$major.$minor.${revision}$build"
267+
"$major.$minor.${patch}$build"
267268
}
268269
}

core/src/main/java/org/botblock/javabotblockapi/core/annotations/PlannedRemoval.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
* <br>This is paired with the {@link java.lang.Deprecated Deprecated} and
2626
* {@link org.botblock.javabotblockapi.core.annotations.DeprecatedSince DeprecatedSince} annotations.
2727
*
28-
* <p>This annotation will always contain {@link #version() the version} in which the annotated Object will be removed.
28+
* <p>This annotation will always contain the {@link #major() major}, {@link #minor()} and {@link #patch() patch} version
29+
* in which the annotated Object will be removed.
30+
* <br>For example will {@code @PlannedRemoval(major = 6, minor = 5, patch = 0)} indocate an Object for removal on version
31+
* 6.5.0.
2932
*
3033
* @since 5.2.2
3134
*/
@@ -35,9 +38,23 @@
3538
public @interface PlannedRemoval{
3639

3740
/**
38-
* The version for when the annotated Object will be removed.
41+
* The major version for when the annotated Object will be removed.
3942
*
40-
* @return The version for when this Object will be removed.
43+
* @return The major version for when this Object will be removed.
4144
*/
42-
String version();
45+
int major();
46+
47+
/**
48+
* The minor version for when the annotated Object will be removed.
49+
*
50+
* @return The minor version for when this Object will be removed.
51+
*/
52+
int minor();
53+
54+
/**
55+
* The patch version for when the annotated Object will be removed.
56+
*
57+
* @return The path version for when this Object will be removed.
58+
*/
59+
int patch();
4360
}

javacord/src/main/java/org/botblock/javabotblockapi/javacord/PostAction.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.botblock.javabotblockapi.javacord;
2020

2121
import org.botblock.javabotblockapi.core.BotBlockAPI;
22+
import org.botblock.javabotblockapi.core.CheckUtil;
2223
import org.botblock.javabotblockapi.core.exceptions.RatelimitedException;
2324
import org.botblock.javabotblockapi.requests.handler.RequestHandler;
2425
import org.javacord.api.DiscordApi;
@@ -112,6 +113,11 @@ public void disableAutoPost(@Nullable BotBlockAPI botBlockAPI){
112113
* finish, or to time out after a specified time frame.
113114
*
114115
* <p>This method may throw a {@link java.lang.InterruptedException InterruptedException} in the terminal.
116+
*
117+
* <p>Following Exceptions can be thrown from the {@link org.botblock.javabotblockapi.core.CheckUtil CheckUtil}:
118+
* <ul>
119+
* <li>{@link java.lang.IllegalStateException IllegalStateException} - When the provided time param is 0 or lower.</li>
120+
* </ul>
115121
*
116122
* @param time
117123
* The amount of time to wait for scheduled executions to finish before the Scheduler would time out.
@@ -121,6 +127,8 @@ public void disableAutoPost(@Nullable BotBlockAPI botBlockAPI){
121127
* @see java.util.concurrent.ScheduledExecutorService#awaitTermination(long, TimeUnit)
122128
*/
123129
public void disableAutoPost(long time, @Nonnull TimeUnit timeUnit){
130+
CheckUtil.condition(time <= 0, "time may not be less or equal to 0!");
131+
124132
try{
125133
scheduler.shutdown();
126134
scheduler.awaitTermination(time, timeUnit);
@@ -139,16 +147,23 @@ public void disableAutoPost(long time, @Nonnull TimeUnit timeUnit){
139147
* <p>The scheduler will wait an initial delay of 1 minute and then performs a task every n minutes, where n is the
140148
* time set in {@link org.botblock.javabotblockapi.core.BotBlockAPI.Builder#setUpdateDelay(Integer) BotBlockAPI.Builder.setUpdateDelay(Integer)}
141149
* (default is 30 minutes).
150+
*
151+
* <p>Following Exceptions can be thrown from the {@link org.botblock.javabotblockapi.core.CheckUtil CheckUtil}:
152+
* <ul>
153+
* <li>{@link java.lang.IllegalStateException IllegalStateException} - When the provided DiscordApis are 0 or less.</li>
154+
* </ul>
142155
*
143-
* @param apis
156+
* @param discordApis
144157
* The {@link org.javacord.api.DiscordApi DiscordApi instances} to post stats from.
145158
* @param botBlockAPI
146159
* The {@link org.botblock.javabotblockapi.core.BotBlockAPI BotBlockAPI instance} to use.
147160
*/
148-
public void enableAutoPost(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi... apis){
161+
public void enableAutoPost(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi... discordApis){
162+
CheckUtil.condition(discordApis.length <= 0, "At least one DiscordApi instance needs to be provided!");
163+
149164
scheduler.scheduleAtFixedRate(() -> {
150165
try{
151-
postGuilds(botBlockAPI, apis);
166+
postGuilds(botBlockAPI, discordApis);
152167
}catch(IOException | RatelimitedException ex){
153168
ex.printStackTrace();
154169
}
@@ -161,8 +176,13 @@ public void enableAutoPost(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi
161176
*
162177
* <p>If the provided DiscordApi instance is a sharded Bot (Amount of shards is larger than 1) will the request
163178
* contain the {@code shards} array alongside a {@code shard_count} field.
179+
*
180+
* <p>Following Exceptions can be thrown from the {@link org.botblock.javabotblockapi.core.CheckUtil CheckUtil}:
181+
* <ul>
182+
* <li>{@link java.lang.IllegalStateException IllegalStateException} - When the provided DiscordApis are 0 or less.</li>
183+
* </ul>
164184
*
165-
* @param apis
185+
* @param discordApis
166186
* The {@link org.javacord.api.DiscordApi DiscordApi instances} to post stats from.
167187
* @param botBlockAPI
168188
* The {@link org.botblock.javabotblockapi.core.BotBlockAPI BotBlockAPI instance} to use.
@@ -172,22 +192,24 @@ public void enableAutoPost(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi
172192
* @throws org.botblock.javabotblockapi.core.exceptions.RatelimitedException
173193
* When we get rate limited by the BotBlock API (returns error code 429).
174194
*/
175-
public void postGuilds(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi... apis) throws IOException, RatelimitedException{
195+
public void postGuilds(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi... discordApis) throws IOException, RatelimitedException{
196+
CheckUtil.condition(discordApis.length <= 0, "At least one DiscordApi instance needs to be provided!");
197+
176198
JSONObject json = new JSONObject()
177-
.put("bot_id", apis[0].getYourself().getId());
199+
.put("bot_id", discordApis[0].getYourself().getId());
178200

179-
if(apis.length > 1){
180-
int guilds = Arrays.stream(apis).map(DiscordApi::getServers).mapToInt(Collection::size).sum();
201+
if(discordApis.length > 1){
202+
int guilds = Arrays.stream(discordApis).map(DiscordApi::getServers).mapToInt(Collection::size).sum();
181203
json.put("server_count", guilds)
182-
.put("shard_count", apis.length);
204+
.put("shard_count", discordApis.length);
183205

184206
List<Integer> shards = new ArrayList<>();
185-
for(DiscordApi api : apis)
207+
for(DiscordApi api : discordApis)
186208
shards.add(api.getServers().size());
187209

188210
json.put("shards", new JSONArray(Arrays.deepToString(shards.toArray())));
189211
}else{
190-
json.put("server_count", apis[0].getServers().size());
212+
json.put("server_count", discordApis[0].getServers().size());
191213
}
192214

193215
botBlockAPI.getTokens().forEach(json::put);

jda/src/main/java/org/botblock/javabotblockapi/jda/PostAction.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ public void disableAutoPost(@Nullable BotBlockAPI botBlockAPI){
134134
*
135135
* <p>This method may throw a {@link java.lang.InterruptedException InterruptedException} in the terminal.
136136
*
137+
* <p>Following Exceptions can be thrown from the {@link org.botblock.javabotblockapi.core.CheckUtil CheckUtil}:
138+
* <ul>
139+
* <li>{@link java.lang.IllegalStateException IllegalStateException} - When the provided time param is 0 or lower.</li>
140+
* </ul>
141+
*
137142
* @param time
138143
* The amount of time to wait for scheduled executions to finish before the Scheduler would time out.
139144
* @param timeUnit
@@ -144,6 +149,8 @@ public void disableAutoPost(@Nullable BotBlockAPI botBlockAPI){
144149
* @see java.util.concurrent.ScheduledExecutorService#awaitTermination(long, TimeUnit)
145150
*/
146151
public void disableAutoPost(long time, @Nonnull TimeUnit timeUnit){
152+
CheckUtil.condition(time <= 0, "Time may not be less or equal to 0");
153+
147154
try{
148155
scheduler.shutdown();
149156
scheduler.awaitTermination(time, timeUnit);
@@ -255,7 +262,7 @@ public void postGuilds(@Nonnull JDA jda, @Nonnull BotBlockAPI botBlockAPI) throw
255262
*/
256263
public void postGuilds(@Nonnull ShardManager shardManager, @Nonnull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{
257264
JDA shard = shardManager.getShardById(0);
258-
CheckUtil.condition(shard == null, "Received invalid/null Shard (Shard ID: 0)");
265+
CheckUtil.condition(shard == null, "Shard 0 of ShardManager was invalid (null).");
259266

260267
JSONObject json = new JSONObject()
261268
.put("server_count", shardManager.getGuildCache().size())

0 commit comments

Comments
 (0)