The class can currently do the following things: + *
If you use this on a sharded bot, better use {@link #postGuilds(ShardManager, BotBlockAPI)}.
*
* @param jda
- * The {@link net.dv8tion.jda.core.JDA JDA instance} that should be used.
+ * The {@link net.dv8tion.jda.api.JDA JDA instance} that should be used.
* @param botBlockAPI
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
*
@@ -100,8 +111,6 @@ public void postGuilds(@NotNull ShardManager shardManager, @NotNull BotBlockAPI
* When the post request couldn't be performed properly.
* @throws RatelimitedException
* When the Bot (IP or ID) got ratelimited.
- *
- * @see #postGuilds(ShardManager, BotBlockAPI) postGuilds(ShardManager, BotBlockAPI) for posting with ShardManager.
*/
public void postGuilds(@NotNull JDA jda, @NotNull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{
this.id = jda.getSelfUser().getId();
@@ -109,18 +118,17 @@ public void postGuilds(@NotNull JDA jda, @NotNull BotBlockAPI botBlockAPI) throw
json.put("server_count", jda.getGuildCache().size())
.put("bot_id", id);
- if(jda.getShardInfo() != null)
+ if(jda.getShardInfo().getShardTotal() > 1)
json.put("shard_id", jda.getShardInfo().getShardId())
.put("shard_count", jda.getShardInfo().getShardTotal());
botBlockAPI.getAuthTokens().forEach(json::put);
- performRequest();
+ postRequest();
}
/**
* Posts the provided guilds from the provided Bot id.
- * The JSONObject will look something like this:
+ * The JSONObject will look something like this:
+ * The JSONObject will look something like this:
+ * The JSONObject will look something like this:
+ * The JSONObject will look something like this:
+ * The JSONObject will look something like this:
+ * The JSONObject will look something like this:
+ * The JSONObject will look something like this:
+ * A response could look like this:
+ * A response could look like this:
+ * A response could look like this:
+ * A response could look like this:
+ * A response could look like this:
+ * A response could look like this:
+ *
This is a shortcut to {@link #postGuilds(String, int, BotBlockAPI)}.
*
* @param botId
* The ID (as long) of the bot.
@@ -133,10 +141,6 @@ public void postGuilds(@NotNull JDA jda, @NotNull BotBlockAPI botBlockAPI) throw
* When the post request couldn't be performed properly.
* @throws RatelimitedException
* When the Bot (IP or ID) got ratelimited.
- *
- * @see #postGuilds(String, int, BotBlockAPI) postGuilds(String, int, BotBlockAPI) for the full method.
- * @see #postGuilds(ShardManager, BotBlockAPI) postGuilds(ShardManager, BotBlockAPI) for posting with ShardManager.
- * @see #postGuilds(JDA, BotBlockAPI) postGuilds(JDA, BotBlockAPI) for posting with JDA.
*/
public void postGuilds(Long botId, int guilds, @NotNull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{
postGuilds(Long.toString(botId), guilds, botBlockAPI);
@@ -156,29 +160,28 @@ public void postGuilds(Long botId, int guilds, @NotNull BotBlockAPI botBlockAPI)
* When the post request couldn't be performed properly.
* @throws RatelimitedException
* When the Bot (IP or ID) got ratelimited.
- *
- * @see #postGuilds(ShardManager, BotBlockAPI) postGuilds(ShardManager, BotBlockAPI) for posting with ShardManager.
- * @see #postGuilds(JDA, BotBlockAPI) postGuilds(JDA, BotBlockAPI) for posting with JDA.
*/
public void postGuilds(@NotNull String botId, int guilds, @NotNull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{
+ Check.notEmpty(botId, "ID may not be empty.");
+
+ this.id = botId;
+
json.put("server_count", guilds)
.put("bot_id", botId);
botBlockAPI.getAuthTokens().forEach(json::put);
- performRequest();
+ postRequest();
}
/**
- * Starts a scheduler that posts the guilds from the provided {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager}
+ * Starts a scheduler that posts the guilds from the provided {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager}
* every X minutes.
*
* @param shardManager
- * The {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager instance} that should be used.
+ * The {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager instance} that should be used.
* @param botBlockAPI
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
- *
- * @see #startAutoPosting(JDA, BotBlockAPI) startAutoPosting(JDA, BotBlockAPI) for posting with JDA.
*/
public void startAutoPosting(@NotNull ShardManager shardManager, @NotNull BotBlockAPI botBlockAPI){
scheduler.scheduleAtFixedRate(() -> {
@@ -191,15 +194,12 @@ public void startAutoPosting(@NotNull ShardManager shardManager, @NotNull BotBlo
}
/**
- * Starts a scheduler that posts the guilds from the provided {@link net.dv8tion.jda.core.JDA JDA}
- * every X minutes.
+ * Starts a scheduler that posts the guilds from the provided {@link net.dv8tion.jda.api.JDA JDA} every X minutes.
*
* @param jda
- * The {@link net.dv8tion.jda.core.JDA JDA instance} that should be used.
+ * The {@link net.dv8tion.jda.api.JDA JDA instance} that should be used.
* @param botBlockAPI
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
- *
- * @see #startAutoPosting(ShardManager, BotBlockAPI) startAutoPosting(ShardManager, BotBlockAPI) for posting with ShardManager.
*/
public void startAutoPosting(@NotNull JDA jda, @NotNull BotBlockAPI botBlockAPI){
scheduler.scheduleAtFixedRate(() -> {
@@ -220,9 +220,6 @@ public void startAutoPosting(@NotNull JDA jda, @NotNull BotBlockAPI botBlockAPI)
* The guilds the bot is in.
* @param botBlockAPI
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
- *
- * @see #startAutoPosting(JDA, BotBlockAPI) startAutoPosting(JDA, BotBlockAPI) for posting with JDA.
- * @see #startAutoPosting(ShardManager, BotBlockAPI) startAutoPosting(ShardManager, BotBlockAPI) for posting with ShardManager.
*/
public void startAutoPosting(Long botId, int guilds, @NotNull BotBlockAPI botBlockAPI){
scheduler.scheduleAtFixedRate(() -> {
@@ -243,9 +240,6 @@ public void startAutoPosting(Long botId, int guilds, @NotNull BotBlockAPI botBlo
* The guilds the bot is in.
* @param botBlockAPI
* The {@link com.andre601.javabotblockapi.BotBlockAPI BotBlockAPI instance} that should be used.
- *
- * @see #startAutoPosting(JDA, BotBlockAPI) startAutoPosting(JDA, BotBlockAPI) for posting with JDA.
- * @see #startAutoPosting(ShardManager, BotBlockAPI) startAutoPosting(ShardManager, BotBlockAPI) for posting with ShardManager.
*/
public void startAutoPosting(@NotNull String botId, int guilds, @NotNull BotBlockAPI botBlockAPI){
scheduler.scheduleAtFixedRate(() -> {
@@ -258,48 +252,740 @@ public void startAutoPosting(@NotNull String botId, int guilds, @NotNull BotBloc
}
/**
- * Shuts down the scheduler.
+ * Stops the auto-posting by shutting down the scheduler.
*/
public void stopAutoPosting(){
scheduler.shutdown();
}
- private void performRequest() throws IOException, RatelimitedException{
- Objects.requireNonNull(json, "JSON may not be null.");
- Objects.requireNonNull(id, "Id may not be null.");
+ /**
+ * Gets the owners of a bot as a list.
+ *
+ * @param shardManager
+ * The {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager instance} that should be used.
+ *
+ * @return The owners as a list.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public List
The data of each Botlist depends on the site.
+ *
+ *
+ *
+ * @param shardManager
+ * The {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager instance} that should be used.
+ *
+ * @return The Botlists as JSONObject.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONObject getBotInfos(@NotNull ShardManager shardManager) throws IOException, RatelimitedException{
+ return getBotInfos(Objects.requireNonNull(shardManager.getShardById(0), "Received invalid shard.")
+ .getSelfUser().getId());
+ }
+
+ /**
+ * Gets all the available Botlists as JSONObject.
+ *
+ * {
+ * "somebotlist.com": [
+ * {@literal
The data of each Botlist depends on the site.
+ *
+ *
+ *
+ * @param jda
+ * The {@link net.dv8tion.jda.api.JDA jda instance} that should be used.
+ *
+ * @return The Botlists as JSONObject.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONObject getBotInfos(@NotNull JDA jda) throws IOException, RatelimitedException{
+ return getBotInfos(jda.getSelfUser().getId());
+ }
+
+ /**
+ * Gets all the available Botlists as JSONObject.
+ *
+ * {
+ * "somebotlist.com": [
+ * {@literal
The data of each Botlist depends on the site.
+ *
+ *
+ *
+ * @param id
+ * The id of the bot.
+ *
+ * @return The Botlists as JSONObject.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONObject getBotInfos(Long id) throws IOException, RatelimitedException{
+ return getBotInfos(Long.toString(id));
+ }
+
+ /**
+ * Gets all the available Botlists as JSONObject.
+ *
+ * {
+ * "somebotlist.com": [
+ * {@literal
The data of each Botlist depends on the site.
+ *
+ *
+ *
+ * @param id
+ * The id of the bot
+ *
+ * @return The Botlists as JSONObject.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONObject getBotInfos(@NotNull String id) throws IOException, RatelimitedException{
+ return getAll(id).getJSONObject("list_data");
+ }
+
+ /**
+ * Gets the specific information from a single Botlist.
+ *
+ * {
+ * "somebotlist.com": [
+ * {@literal
The returned data depends on the Botlist.
+ *
+ *
+ *
+ * @param shardManager
+ * The {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager instance} that should be used.
+ * @param site
+ * The sites name to get information from.
+ *
+ * {[
+ * {@literal
A list of supported sites can be found here.
+ *
+ * @return The sites information as JSONArray.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONArray getBotInfo(@NotNull ShardManager shardManager, @NotNull String site) throws IOException, RatelimitedException{
+ return getBotInfo(Objects.requireNonNull(shardManager.getShardById(0), "Received invalid shard.")
+ .getSelfUser().getId(), site);
+ }
+
+ /**
+ * Gets the specific information from a single Botlist.
+ *
The returned data depends on the Botlist.
+ *
+ *
+ *
+ * @param id
+ * The id of the bot.
+ * @param site
+ * The sites name to get information from.
+ *
+ * {[
+ * {@literal
A list of supported sites can be found here.
+ *
+ * @return The sites information as JSONArray.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONArray getBotInfo(Long id, @NotNull String site) throws IOException, RatelimitedException{
+ return getBotInfo(Long.toString(id), site);
+ }
+
+ /**
+ * Gets the specific information from a single Botlist.
+ *
The returned data depends on the Botlist.
+ *
+ *
+ *
+ * @param jda
+ * The {@link net.dv8tion.jda.api.JDA JDA instance} that should be used.
+ * @param site
+ * The sites name to get information from.
+ *
+ * {[
+ * {@literal
A list of supported sites can be found here.
+ *
+ * @return The sites information as JSONArray.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONArray getBotInfo(@NotNull JDA jda, String site) throws IOException, RatelimitedException{
+ return getBotInfo(jda.getSelfUser().getId(), site);
+ }
+
+ /**
+ * Gets the specific information from a single Botlist.
+ *
The returned data depends on the Botlist.
+ *
+ *
+ *
+ * @param id
+ * The id of the bot.
+ * @param site
+ * The sites name to get information from.
+ *
+ * {[
+ * {@literal
A list of supported sites can be found here.
+ *
+ * @return The sites information as JSONArray.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONArray getBotInfo(@NotNull String id, @NotNull String site) throws IOException, RatelimitedException{
+ return getAll(id).getJSONObject("list_data").getJSONArray(site);
+ }
+
+ /**
+ * Gets information from BotBlock about the provided Bot.
+ *
The information can contain:
+ *
+ *
+ * *Based on most appearances on the botlists.
+ *
**The provided data depends on the Botlist and can be different.
+ *
+ *
+ *
+ * @param shardManager
+ * The instance of {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager} to use.
+ *
+ * @return The Bot information as JSONObject.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONObject getAll(@NotNull ShardManager shardManager) throws IOException, RatelimitedException{
+ return getAll(Objects.requireNonNull(shardManager.getShardById(0), "Received invalid shard.")
+ .getSelfUser().getId());
+ }
+
+ /**
+ * Gets information from BotBlock about the provided Bot.
+ *
+ * {
+ * "id": "123456789012345678",
+ * "usernam": "MyBot",
+ * "discriminator": "1234",
+ * "owners": [
+ * "234567890123456789"
+ * ],
+ * "server_count": 100,
+ * "invite":{@literal "https://discordapp.com/oauth2/authorize?client_id=123456789012345678&scope=bot"},
+ * "list_data": {
+ * "somebotlist.com": [
+ * {@literal },
+ * 200
+ * ],
+ * "otherlist.org": [
+ * {@literal
},
+ * 404
+ * ]
+ * }
+ * }
+ *
The information can contain:
+ *
+ *
+ * *Based on most appearances on the botlists.
+ *
**The provided data depends on the Botlist and can be different.
+ *
+ *
+ *
+ * @param jda
+ * The instance of {@link net.dv8tion.jda.api.JDA JDA} to use.
+ *
+ * @return The Bot information as JSONObject.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONObject getAll(@NotNull JDA jda) throws IOException, RatelimitedException{
+ return getAll(jda.getSelfUser().getId());
+ }
+
+ /**
+ * Gets information from BotBlock about the provided Bot.
+ *
+ * {
+ * "id": "123456789012345678",
+ * "usernam": "MyBot",
+ * "discriminator": "1234",
+ * "owners": [
+ * "234567890123456789"
+ * ],
+ * "server_count": 100,
+ * "invite":{@literal "https://discordapp.com/oauth2/authorize?client_id=123456789012345678&scope=bot"},
+ * "list_data": {
+ * "somebotlist.com": [
+ * {@literal },
+ * 200
+ * ],
+ * "otherlist.org": [
+ * {@literal
},
+ * 404
+ * ]
+ * }
+ * }
+ *
The information can contain:
+ *
+ *
+ * *Based on most appearances on the botlists.
+ *
**The provided data depends on the Botlist and can be different.
+ *
+ *
+ *
+ * @param id
+ * The id of the bot.
+ *
+ * @return The Bot information as JSONObject.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONObject getAll(Long id) throws IOException, RatelimitedException{
+ return getAll(Long.toString(id));
+ }
+
+ /**
+ * Gets information from BotBlock about the provided Bot.
+ *
+ * {
+ * "id": "123456789012345678",
+ * "usernam": "MyBot",
+ * "discriminator": "1234",
+ * "owners": [
+ * "234567890123456789"
+ * ],
+ * "server_count": 100,
+ * "invite":{@literal "https://discordapp.com/oauth2/authorize?client_id=123456789012345678&scope=bot"},
+ * "list_data": {
+ * "somebotlist.com": [
+ * {@literal },
+ * 200
+ * ],
+ * "otherlist.org": [
+ * {@literal
},
+ * 404
+ * ]
+ * }
+ * }
+ *
The information can contain:
+ *
+ *
+ * *Based on most appearances on the botlists.
+ *
**The provided data depends on the Botlist and can be different.
+ *
+ *
+ *
+ * @param id
+ * The id of the bot.
+ *
+ * @return The Bot information as JSONObject.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONObject getAll(@NotNull String id) throws IOException, RatelimitedException{
+ String url = BASE_URL + "bots/" + id;
+
+ Request request = new Request.Builder()
+ .url(url)
+ .addHeader("User-Agent", id)
+ .build();
+
+ try(Response response = CLIENT.newCall(request).execute()){
+ Check.notNull(response.body(), "Received empty response body from BotBlcok API.");
+ ResponseBody responseBody = response.body();
+
+ Check.notEmpty(responseBody.string(), "Received empty response body from BotBlock API.");
+
+ if(!response.isSuccessful()){
+ if(response.code() == 429)
+ throw new RatelimitedException(responseBody.string());
+
+ throw new IOException(String.format(
+ "Couldn't get Bot information. Site responded with error code %d (%s)",
+ response.code(),
+ response.message()
+ ));
+ }
+
+ return new JSONObject(responseBody.string());
+ }
+ }
+
+ /**
+ * Returns the provided botlist info that is saved in BotBlock.
+ *
+ *
+ * {
+ * "id": "123456789012345678",
+ * "usernam": "MyBot",
+ * "discriminator": "1234",
+ * "owners": [
+ * "234567890123456789"
+ * ],
+ * "server_count": 100,
+ * "invite":{@literal "https://discordapp.com/oauth2/authorize?client_id=123456789012345678&scope=bot"},
+ * "list_data": {
+ * "somebotlist.com": [
+ * {@literal },
+ * 200
+ * ],
+ * "otherlist.org": [
+ * {@literal
},
+ * 404
+ * ]
+ * }
+ * }
+ *
+ *
+ * @param name
+ * The name of the botlist.
+ *
+ * @return The botlist as JSONObject.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONObject getBotlist(@NotNull String name) throws IOException, RatelimitedException{
+ return getBotlists().getJSONObject(name);
+ }
+
+ /**
+ * Returns the current botlists that BotBlock supports.
+ *
+ *
+ * {
+ * "api_docs": "https://somebotlist.com/docs",
+ * "api_post": "https://somebotlist.com/api/v1/bots/:id/post",
+ * "api_field": "server_count",
+ * "api_shard_id": "shard_id",
+ * "api_shard_count": "shard_count",
+ * "api_shards": "shards",
+ * "api_get": "https://somebotlist.com/api/v1/bots/:id"
+ * }
+ *
+ *
+ * @return The botlists as JSONObject.
+ *
+ * @throws IOException
+ * When the request couldn't be performed properly.
+ * @throws RatelimitedException
+ * When the API gets ratelimited.
+ *
+ * @since v2.0.0
+ */
+ public JSONObject getBotlists() throws IOException, RatelimitedException{
+ String url = BASE_URL + "lists";
+
+ Request request = new Request.Builder()
+ .url(url)
+ .build();
+
+ try(Response response = CLIENT.newCall(request).execute()){
+ Check.notNull(response.body(), "Received empty response body from BotBlcok API.");
+ ResponseBody responseBody = response.body();
+
+ Check.notEmpty(responseBody.string(), "Received empty response body from BotBlock API.");
+
+ if(!response.isSuccessful()){
+ if(response.code() == 429)
+ throw new RatelimitedException(responseBody.string());
+
+ throw new IOException(String.format(
+ "Couldn't get Botlists. Site responded with error code %d (%s)",
+ response.code(),
+ response.message()
+ ));
+ }
+
+ return new JSONObject(responseBody.string());
+ }
+ }
+
+ private void postRequest() throws IOException, RatelimitedException{
+ Check.notNull(json, "JSON may not be null.");
+ Check.notEmpty(id, "ID may not be empty.");
+
+ String url = BASE_URL + "count";
RequestBody body = RequestBody.create(null, json.toString());
Request request = new Request.Builder()
- .url("https://botblock.org/api/count")
+ .url(url)
.addHeader("User-Agent", id)
.addHeader("Content-Type", "application/json") // Some sites require this in the header.
.post(body)
.build();
try(Response response = CLIENT.newCall(request).execute()){
- Objects.requireNonNull(response.body(), "Received empty body from BotBlocks API.");
+ Check.notNull(response.body(), "Received empty response body from BotBlcok API.");
+ ResponseBody responseBody = response.body();
- if(response.body().string().isEmpty())
- throw new NullPointerException("Received empty body from BotBlocks API.");
+ Check.notEmpty(responseBody.string(), "Received empty response body from BotBlock API.");
if(!response.isSuccessful()){
if(response.code() == 429)
- throw new RatelimitedException(response.body().string());
+ throw new RatelimitedException(responseBody.string());
throw new IOException(String.format(
- "Couldn't post guild counts to BotBlockAPI! Site responded with %d (%s)",
+ "Couldn't post guild counts to BotBlockAPI! Site responded with error code %d (%s)",
response.code(),
response.message()
));
}
- JSONObject json = new JSONObject(response.body());
+ JSONObject json = new JSONObject(responseBody);
if(json.has("failure")){
JSONObject failure = json.getJSONObject("failure");
List
+ * {
+ * "somebotlist.com": {
+ * "api_docs": "https://somebotlist.com/docs",
+ * "api_post": "https://somebotlist.com/api/v1/bots/:id/post",
+ * "api_field": "server_count",
+ * "api_shard_id": "shard_id",
+ * "api_shard_count": "shard_count",
+ * "api_shards": "shards",
+ * "api_get": "https://somebotlist.com/api/v1/bots/:id"
+ * },
+ * "otherlist.org": {
+ * "api_docs": "https://docs.otherlist.org",
+ * "api_post": null,
+ * "api_field": null,
+ * "api_shard_id": null,
+ * "api_shard_count": null,
+ * "api_shards": null,
+ * "api_get": "https://api.otherlist.org/v2/bot/:id"
+ * }
+ * }
+ *