Skip to content

Commit b300565

Browse files
committed
Implement full shard support.
1 parent b1ee388 commit b300565

File tree

1 file changed

+23
-15
lines changed
  • javacord/src/main/java/org/botblock/javabotblockapi/javacord

1 file changed

+23
-15
lines changed

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.botblock.javabotblockapi.core.exceptions.RatelimitedException;
2323
import org.botblock.javabotblockapi.requests.handler.RequestHandler;
2424
import org.javacord.api.DiscordApi;
25-
import org.javacord.api.Javacord;
2625
import org.json.JSONArray;
2726
import org.json.JSONObject;
2827

@@ -31,6 +30,7 @@
3130
import java.io.IOException;
3231
import java.util.ArrayList;
3332
import java.util.Arrays;
33+
import java.util.Collection;
3434
import java.util.List;
3535
import java.util.concurrent.ScheduledExecutorService;
3636
import java.util.concurrent.TimeUnit;
@@ -140,15 +140,15 @@ public void disableAutoPost(long time, @Nonnull TimeUnit timeUnit){
140140
* time set in {@link org.botblock.javabotblockapi.core.BotBlockAPI.Builder#setUpdateDelay(Integer) BotBlockAPI.Builder.setUpdateDelay(Integer)}
141141
* (default is 30 minutes).
142142
*
143-
* @param api
144-
* The {@link org.javacord.api.DiscordApi DiscordApi instance} to post stats from.
143+
* @param apis
144+
* The {@link org.javacord.api.DiscordApi DiscordApi instances} to post stats from.
145145
* @param botBlockAPI
146146
* The {@link org.botblock.javabotblockapi.core.BotBlockAPI BotBlockAPI instance} to use.
147147
*/
148-
public void enableAutoPost(@Nonnull DiscordApi api, @Nonnull BotBlockAPI botBlockAPI){
148+
public void enableAutoPost(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi... apis){
149149
scheduler.scheduleAtFixedRate(() -> {
150150
try{
151-
postGuilds(api, botBlockAPI);
151+
postGuilds(botBlockAPI, apis);
152152
}catch(IOException | RatelimitedException ex){
153153
ex.printStackTrace();
154154
}
@@ -162,8 +162,8 @@ public void enableAutoPost(@Nonnull DiscordApi api, @Nonnull BotBlockAPI botBloc
162162
* <p>If the provided DiscordApi instance is a sharded Bot (Amount of shards is larger than 1) will the request
163163
* contain the {@code shards} array alongside a {@code shard_count} field.
164164
*
165-
* @param api
166-
* The {@link org.javacord.api.DiscordApi DiscordApi instance} to post stats from.
165+
* @param apis
166+
* The {@link org.javacord.api.DiscordApi DiscordApi instances} to post stats from.
167167
* @param botBlockAPI
168168
* The {@link org.botblock.javabotblockapi.core.BotBlockAPI BotBlockAPI instance} to use.
169169
*
@@ -172,16 +172,24 @@ public void enableAutoPost(@Nonnull DiscordApi api, @Nonnull BotBlockAPI botBloc
172172
* @throws org.botblock.javabotblockapi.core.exceptions.RatelimitedException
173173
* When we get rate limited by the BotBlock API (returns error code 429).
174174
*/
175-
public void postGuilds(@Nonnull DiscordApi api, @Nonnull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{
175+
public void postGuilds(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi... apis) throws IOException, RatelimitedException{
176176
JSONObject json = new JSONObject()
177-
.put("server_count", api.getServers().size())
178-
.put("bot_id", api.getYourself().getId());
179-
180-
List<Long> shards = new ArrayList<>();
181-
for(int i = 0; i < (api.getTotalShards() -1); i++)
182-
shards.add(1L);
177+
.put("bot_id", apis[0].getYourself().getId());
178+
179+
if(apis.length > 1){
180+
int guilds = Arrays.stream(apis).map(DiscordApi::getServers).mapToInt(Collection::size).sum();
181+
json.put("server_count", guilds)
182+
.put("shard_count", apis.length);
183+
184+
List<Integer> shards = new ArrayList<>();
185+
for(DiscordApi api : apis)
186+
shards.add(api.getServers().size());
187+
188+
json.put("shards", new JSONArray(Arrays.deepToString(shards.toArray())));
189+
}else{
190+
json.put("server_count", apis[0].getServers().size());
191+
}
183192

184-
json.put("shards", new JSONArray(Arrays.deepToString(shards.toArray())));
185193
botBlockAPI.getTokens().forEach(json::put);
186194

187195
requestHandler.performPOST(json, botBlockAPI.getTokens().size());

0 commit comments

Comments
 (0)