Skip to content

Commit 2999925

Browse files
authored
First upload, committing all files.
1 parent 20d5acd commit 2999925

File tree

6 files changed

+641
-0
lines changed

6 files changed

+641
-0
lines changed

pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>BotsForDiscordAPI</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
</properties>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<version>3.5.1</version>
23+
<configuration>
24+
<source>1.8</source>
25+
<target>1.8</target>
26+
</configuration>
27+
</plugin>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-surefire-plugin</artifactId>
31+
<version>2.12.4</version>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.google.code.gson</groupId>
39+
<artifactId>gson</artifactId>
40+
<version>2.8.7</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.mashape.unirest</groupId>
44+
<artifactId>unirest-java</artifactId>
45+
<version>1.4.9</version>
46+
</dependency>
47+
</dependencies>
48+
</project>
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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

Comments
 (0)