|
| 1 | +package me.dorian349.bfdapi; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import com.mashape.unirest.http.HttpResponse; |
| 6 | +import com.mashape.unirest.http.Unirest; |
| 7 | +import com.mashape.unirest.http.exceptions.UnirestException; |
| 8 | +import me.dorian349.bfdapi.entities.bot.Bot; |
| 9 | +import me.dorian349.bfdapi.entities.bot.BotVotes; |
| 10 | +import me.dorian349.bfdapi.entities.user.User; |
| 11 | +import me.dorian349.bfdapi.entities.user.UserBots; |
| 12 | +import org.json.JSONObject; |
| 13 | + |
| 14 | +public class BotsForDiscordAPI { |
| 15 | + |
| 16 | + private final Gson gson; |
| 17 | + |
| 18 | + private final String botId; |
| 19 | + private final String bfdToken; |
| 20 | + |
| 21 | + public BotsForDiscordAPI(String botId, String bfdToken){ |
| 22 | + |
| 23 | + gson = new GsonBuilder().setPrettyPrinting().create(); |
| 24 | + |
| 25 | + this.botId = botId; |
| 26 | + this.bfdToken = bfdToken; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Returns the {@link User} instance of this user id |
| 31 | + * |
| 32 | + * @param userId |
| 33 | + * The id of the corresponding user. |
| 34 | + * |
| 35 | + * @throws java.lang.IllegalArgumentException |
| 36 | + * If the provided user id cannot be found. |
| 37 | + * |
| 38 | + * @return the corresponding User instance. |
| 39 | + */ |
| 40 | + public User getUser(String userId){ |
| 41 | + HttpResponse<String> response; |
| 42 | + |
| 43 | + if(userId == null) throw new IllegalArgumentException("The user id cannot be null."); |
| 44 | + |
| 45 | + try { |
| 46 | + response = Unirest.get("https://discords.com/bots/api/user/" + userId).asString(); |
| 47 | + if(response.getStatus() == 200){ |
| 48 | + return gson.fromJson(response.getBody(), User.class); |
| 49 | + } |
| 50 | + else { |
| 51 | + throw new IllegalArgumentException(new JSONObject(response.getBody()).getString("message")); |
| 52 | + } |
| 53 | + } catch (UnirestException e) { |
| 54 | + throw new IllegalArgumentException(e); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns the {@link UserBots} instance of this user id |
| 60 | + * |
| 61 | + * @param userId |
| 62 | + * The id of the corresponding user. |
| 63 | + * |
| 64 | + * @throws java.lang.IllegalArgumentException |
| 65 | + * If the provided user id cannot be found. |
| 66 | + * |
| 67 | + * @return the corresponding UserBots instance. |
| 68 | + */ |
| 69 | + public UserBots getUserBots(String userId){ |
| 70 | + HttpResponse<String> response; |
| 71 | + |
| 72 | + if(userId == null) throw new IllegalArgumentException("The user id cannot be null."); |
| 73 | + |
| 74 | + try { |
| 75 | + response = Unirest.get("https://discords.com/bots/api/user/" + userId + "/bots").asString(); |
| 76 | + if(response.getStatus() == 200){ |
| 77 | + return gson.fromJson(response.getBody(), UserBots.class); |
| 78 | + } |
| 79 | + else { |
| 80 | + throw new IllegalArgumentException(new JSONObject(response.getBody()).getString("message")); |
| 81 | + } |
| 82 | + } catch (UnirestException e) { |
| 83 | + throw new IllegalArgumentException(e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns the {@link Bot} instance of this bot id |
| 89 | + * |
| 90 | + * @param botId |
| 91 | + * The id of the corresponding user. |
| 92 | + * |
| 93 | + * @throws java.lang.IllegalArgumentException |
| 94 | + * If the provided bot id cannot be found. |
| 95 | + * |
| 96 | + * @return the corresponding Bot instance. |
| 97 | + */ |
| 98 | + public Bot getBot(String botId){ |
| 99 | + HttpResponse<String> response; |
| 100 | + |
| 101 | + if(botId == null) throw new IllegalArgumentException("The bot id cannot be null."); |
| 102 | + |
| 103 | + try { |
| 104 | + response = Unirest.get("https://discords.com/bots/api/bot/" + botId).asString(); |
| 105 | + if(response.getStatus() == 200){ |
| 106 | + return gson.fromJson(response.getBody(), Bot.class); |
| 107 | + } |
| 108 | + else { |
| 109 | + throw new IllegalArgumentException(new JSONObject(response.getBody()).getString("message")); |
| 110 | + } |
| 111 | + } catch (UnirestException e) { |
| 112 | + throw new IllegalArgumentException(e); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Returns the {@link BotVotes} instance of this bot |
| 118 | + * |
| 119 | + * @throws java.lang.IllegalArgumentException |
| 120 | + * If the provided bot id cannot be found. |
| 121 | + * |
| 122 | + * @return the corresponding BotVotes instance. |
| 123 | + */ |
| 124 | + public BotVotes getVotes(){ |
| 125 | + HttpResponse<String> response; |
| 126 | + |
| 127 | + if(this.botId == null) throw new IllegalArgumentException("The bot id cannot be null."); |
| 128 | + |
| 129 | + try { |
| 130 | + response = Unirest.get("https://discords.com/bots/api/bot/" + this.botId + "/votes").header("Authorization", this.bfdToken).header("Content-Type", "application/json").asString(); |
| 131 | + if(response.getStatus() == 200){ |
| 132 | + return gson.fromJson(response.getBody(), BotVotes.class); |
| 133 | + } |
| 134 | + else { |
| 135 | + throw new IllegalArgumentException(new JSONObject(response.getBody()).getString("message")); |
| 136 | + } |
| 137 | + } catch (UnirestException e) { |
| 138 | + throw new IllegalArgumentException(e); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Returns the bot widget as a {@link String} |
| 144 | + * |
| 145 | + * @param botId |
| 146 | + * The id of the corresponding user. |
| 147 | + * |
| 148 | + * @throws java.lang.IllegalArgumentException |
| 149 | + * If the provided bot id cannot be found. |
| 150 | + * |
| 151 | + * @return the corresponding bot widget. |
| 152 | + */ |
| 153 | + public String getWidget(String botId){ |
| 154 | + HttpResponse<String> response; |
| 155 | + |
| 156 | + if(botId == null) throw new IllegalArgumentException("The bot id cannot be null."); |
| 157 | + |
| 158 | + try { |
| 159 | + response = Unirest.get("https://discords.com/bots/api/bot/" + botId + "/widget").asString(); |
| 160 | + if(response.getStatus() == 200){ |
| 161 | + return response.getBody(); |
| 162 | + } |
| 163 | + else { |
| 164 | + throw new IllegalArgumentException(new JSONObject(response.getBody()).getString("message")); |
| 165 | + } |
| 166 | + } catch (UnirestException e) { |
| 167 | + throw new IllegalArgumentException(e); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Post the bot server count to the API. |
| 173 | + * |
| 174 | + * @param serverCount |
| 175 | + * The total number of servers using the bot. |
| 176 | + * |
| 177 | + * @throws java.lang.IllegalArgumentException |
| 178 | + * If the provided bot id cannot be found. |
| 179 | + * |
| 180 | + * @return the corresponding server response. |
| 181 | + */ |
| 182 | + public JSONObject postStats(int serverCount){ |
| 183 | + HttpResponse<String> response; |
| 184 | + |
| 185 | + try { |
| 186 | + response = Unirest.post("https://discords.com/bots/api/bot/" + this.botId).header("Authorization", this.bfdToken).header("Content-Type", "application/json").body(new JSONObject().put("server_count", serverCount).toString()).asString(); |
| 187 | + return new JSONObject(response.getBody()); |
| 188 | + } catch (UnirestException e) { |
| 189 | + throw new IllegalArgumentException(e); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments