Skip to content

Commit ffe9e83

Browse files
committed
Update branch to v3.0
- Finish implementing of PostAction. - Make RequestHandler deprecated.
1 parent 57bb586 commit ffe9e83

File tree

5 files changed

+158
-8
lines changed

5 files changed

+158
-8
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins{
77
}
88

99
group = 'com.andre601'
10-
version = '2.3.9'
10+
version = '3.0.0'
1111

1212
sourceCompatibility = targetCompatibility = 1.8
1313

@@ -82,7 +82,7 @@ publishing{
8282
}
8383
}
8484
publications{
85-
gpr(MavenPublications){
85+
gpr(MavenPublication){
8686
from(components.java)
8787
}
8888
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public Map<String, String> getAuthTokens(){
6666
return authTokens;
6767
}
6868

69-
int getUpdateInterval(){
69+
public int getUpdateInterval(){
7070
return updateInterval;
7171
}
7272

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@
4747
* <li>{@link #getBotInfo(ShardManager, String) Getting a single list a bot is on}.</li>
4848
* <li>{@link #getOwners(ShardManager) Getting the owners of a bot.}</li>
4949
* </ul>
50+
*
51+
* @deprecated Use {@link com.andre601.javabotblockapi.requests.PostAction PostAction} or {@link com.andre601.javabotblockapi.requests.GetAction GetAction} respectively.
5052
*/
53+
@Deprecated
5154
public class RequestHandler {
5255
private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
5356
private final OkHttpClient CLIENT = new OkHttpClient();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
22+
/**
23+
* Class to perform GET actions with.
24+
*/
25+
public class GetAction{
26+
27+
private final RequestHandler REQUEST_HANDLER = new RequestHandler();
28+
private boolean disableCache;
29+
30+
/**
31+
* Empty constructor to get the instance of GetAction.
32+
*/
33+
public GetAction(){
34+
this.disableCache = false;
35+
}
36+
37+
/**
38+
* Constructor to get the instance of GetAction.
39+
*
40+
* @param disableCache
41+
* If the caching should be disabled. {@code true} means the cache will be <b>disabled</b>.
42+
* <br>It's recommended to NOT disable caching.
43+
*/
44+
public GetAction(boolean disableCache){
45+
this.disableCache = disableCache;
46+
}
47+
}

src/main/java/com/andre601/javabotblockapi/requests/PostAction.java

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,118 @@
3030
import java.util.ArrayList;
3131
import java.util.Arrays;
3232
import java.util.List;
33+
import java.util.concurrent.Executors;
34+
import java.util.concurrent.ScheduledExecutorService;
35+
import java.util.concurrent.TimeUnit;
3336

37+
/**
38+
* Class to perform post actions with.
39+
*
40+
* <p>You can {@link #enableAutoPost(JDA, BotBlockAPI) post automatically} or {@link #postGuilds(JDA, BotBlockAPI) post manually}.
41+
*/
3442
public class PostAction{
3543

36-
private boolean disableCache;
3744
private final RequestHandler REQUEST_HANDLER = new RequestHandler();
45+
private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
46+
47+
/**
48+
* Empty constructor to create the PostAction instance.
49+
*/
50+
public PostAction(){}
51+
52+
/**
53+
* Shuts down the scheduler, to stop the automatic posting.
54+
*/
55+
public void disableAutoPost(){
56+
scheduler.shutdown();
57+
}
58+
59+
/**
60+
* Starts posting of the guild count each n minutes.
61+
* <br>The delay in which this happens is set using <code>{@link com.andre601.javabotblockapi.BotBlockAPI.Builder#setUpdateInteval(int)} BotBlockAPI.Builder#setUpdateInterval(int)}</code>
62+
*
63+
* <p><b>The scheduler will stop (cancel) the task, when an Exception appears!</b>
64+
*
65+
* @param jda
66+
* The {@link net.dv8tion.jda.api.JDA JDA instance} to use.
67+
* @param botBlockAPI
68+
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} to use.
69+
*/
70+
public void enableAutoPost(@NotNull JDA jda, @NotNull BotBlockAPI botBlockAPI){
71+
scheduler.scheduleAtFixedRate(() -> {
72+
try{
73+
postGuilds(jda, botBlockAPI);
74+
}catch(IOException | RatelimitedException ex){
75+
ex.printStackTrace();
76+
}
77+
}, botBlockAPI.getUpdateInterval(), botBlockAPI.getUpdateInterval(), TimeUnit.MINUTES);
78+
}
3879

39-
public PostAction(){
40-
this.disableCache = false;
80+
/**
81+
* Starts posting of the guild count each n minutes.
82+
* <br>The delay in which this happens is set using <code>{@link com.andre601.javabotblockapi.BotBlockAPI.Builder#setUpdateInteval(int)} BotBlockAPI.Builder#setUpdateInterval(int)}</code>
83+
*
84+
* <p><b>The scheduler will stop (cancel) the task, when an Exception appears!</b>
85+
*
86+
* @param botId
87+
* The ID of the bot as Long.
88+
* @param guilds
89+
* The guild count.
90+
* @param botBlockAPI
91+
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} to use.
92+
*/
93+
public void enableAutoPost(Long botId, int guilds, @NotNull BotBlockAPI botBlockAPI){
94+
scheduler.scheduleAtFixedRate(() -> {
95+
try{
96+
postGuilds(botId, guilds, botBlockAPI);
97+
}catch(IOException | RatelimitedException ex){
98+
ex.printStackTrace();
99+
}
100+
}, botBlockAPI.getUpdateInterval(), botBlockAPI.getUpdateInterval(), TimeUnit.MINUTES);
101+
}
102+
103+
/**
104+
* Starts posting of the guild count each n minutes.
105+
* <br>The delay in which this happens is set using <code>{@link com.andre601.javabotblockapi.BotBlockAPI.Builder#setUpdateInteval(int)} BotBlockAPI.Builder#setUpdateInterval(int)}</code>
106+
*
107+
* <p><b>The scheduler will stop (cancel) the task, when an Exception appears!</b>
108+
*
109+
* @param shardManager
110+
* The {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager instance} to use.
111+
* @param botBlockAPI
112+
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} to use.
113+
*/
114+
public void enableAutoPost(@NotNull ShardManager shardManager, @NotNull BotBlockAPI botBlockAPI){
115+
scheduler.scheduleAtFixedRate(() -> {
116+
try{
117+
postGuilds(shardManager, botBlockAPI);
118+
}catch(IOException | RatelimitedException ex){
119+
ex.printStackTrace();
120+
}
121+
}, botBlockAPI.getUpdateInterval(), botBlockAPI.getUpdateInterval(), TimeUnit.MINUTES);
41122
}
42123

43-
public PostAction(boolean disableCache){
44-
this.disableCache = disableCache;
124+
/**
125+
* Starts posting of the guild count each n minutes.
126+
* <br>The delay in which this happens is set using <code>{@link com.andre601.javabotblockapi.BotBlockAPI.Builder#setUpdateInteval(int)} BotBlockAPI.Builder#setUpdateInterval(int)}</code>
127+
*
128+
* <p><b>The scheduler will stop (cancel) the task, when an Exception appears!</b>
129+
*
130+
* @param botId
131+
* The ID of the bot as String.
132+
* @param guilds
133+
* The guild count.
134+
* @param botBlockAPI
135+
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} to use.
136+
*/
137+
public void enableAutoPost(@NotNull String botId, int guilds, @NotNull BotBlockAPI botBlockAPI){
138+
scheduler.scheduleAtFixedRate(() -> {
139+
try{
140+
postGuilds(botId, guilds, botBlockAPI);
141+
}catch(IOException | RatelimitedException ex){
142+
ex.printStackTrace();
143+
}
144+
}, botBlockAPI.getUpdateInterval(), botBlockAPI.getUpdateInterval(), TimeUnit.MINUTES);
45145
}
46146

47147
/**

0 commit comments

Comments
 (0)