Skip to content

Commit adbe776

Browse files
committed
Fix Handler
1 parent 712b298 commit adbe776

2 files changed

Lines changed: 56 additions & 65 deletions

File tree

src/me/Logicism/NightbotCommandsWebServer/http/handlers/CommandsHandler.java

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import java.io.IOException;
1717
import java.net.URL;
1818
import java.net.URLDecoder;
19-
import java.nio.charset.StandardCharsets;
2019
import java.time.LocalDate;
20+
import java.time.LocalDateTime;
2121
import java.time.Period;
2222
import java.time.ZoneId;
2323
import java.time.format.DateTimeFormatter;
@@ -71,34 +71,41 @@ public void handle(HttpExchange exchange) throws IOException {
7171
.getJSONArray("data");
7272

7373
if (userArray.length() != 0) {
74-
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
75-
format.withZone(ZoneId.of("UTC"));
76-
77-
LocalDate date = LocalDate.parse(userArray.getJSONObject(0).getString("followed_at"));
78-
LocalDate nowDate = LocalDate.now();
79-
80-
Period period = Period.between(date, nowDate).normalized();
81-
82-
response = "<html><body>@" + userArray.getJSONObject(0)
83-
.getString("user_name") + " has been following for " + period.getYears() +
84-
(period.getYears() == 1 ? "year" : "years") + " and " + period.getMonths() +
85-
(period.getMonths() == 1 ? "month" : "months") + ", for a total of "
86-
+ period.getDays() + (period.getDays() == 1 ? " Day" : " Days") + "</body></html>";
74+
if (userArray.getJSONObject(0).getString("user_login").equalsIgnoreCase(queryMap.get("username"))) {
75+
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US).withZone(ZoneId.of("UTC"));
76+
77+
try {
78+
LocalDateTime date = LocalDateTime.parse(userArray.getJSONObject(0).getString("followed_at"), format);
79+
LocalDateTime nowDate = LocalDateTime.now();
80+
81+
Period period = Period.between(date.toLocalDate(), nowDate.toLocalDate()).normalized();
82+
83+
response = "@" + userArray.getJSONObject(0)
84+
.getString("user_name") + " has been following for " +
85+
(period.getYears() == 1 ? "1 year, " : (period.getYears() > 0 ? period.getYears() + " years, " : "")) +
86+
(period.getMonths() == 1 ? "1 month, " : (period.getMonths() > 0 ? period.getMonths() + " months, " : "")) +
87+
(period.getDays() == 1 ? " and 1 day" : period.getDays() > 0 ? " and " + period.getDays() + " days" : "") ;
88+
} catch (Exception e) {
89+
response = "Exception Occurred!";
90+
e.printStackTrace();
91+
}
92+
} else {
93+
response = "Unable to grab follower information, please try again!";
94+
}
8795
} else {
88-
response = "<html><body>@" + userArray.getJSONObject(0) + " is not following" +
89-
"the channel</body></html>";
96+
response = "@" + queryMap.get("username") + " is not following the channel";
9097
}
9198

9299
HTTPUtils.throwSuccessHTML(exchange, response);
93100
} else {
94-
String response = "<html><body>Cannot find that channel or username!</body></html>";
101+
String response = "Cannot find that channel or username! Please retry your query or check your spelling!";
95102

96-
HTTPUtils.throwError(exchange, response);
103+
HTTPUtils.throwSuccessHTML(exchange, response);
97104
}
98105
} else {
99-
String response = "<html><body>Moderator is unauthenticated! Head to https://nightbot.logicism.tv/ to Authenticate!</body></html>";
106+
String response = "Moderator is unauthenticated! Head to https://nightbot.logicism.tv/ to Authenticate!";
100107

101-
HTTPUtils.throwError(exchange, response);
108+
HTTPUtils.throwSuccessHTML(exchange, response);
102109
}
103110
} else if (exchange.getRequestURI().toString().startsWith("/commands/quote")) {
104111
TokenHandle tokenHandle = NightbotCommandsWebServer.INSTANCE
@@ -138,28 +145,33 @@ public void handle(HttpExchange exchange) throws IOException {
138145
List<String> quotes = NightbotCommandsWebServer.INSTANCE.getQuotesMap()
139146
.get(userArray.getJSONObject(0).getString("id"));
140147

141-
if (quotes.size() > Integer.parseInt(queryMap.get("index")) - 1 &&
142-
Integer.parseInt(queryMap.get("index")) - 1 > -1) {
143-
response = "<html><body>" + quotes.get(Integer.parseInt(queryMap.get("index")))
144-
+ "</body></html>";
148+
if (queryMap.containsKey("index")) {
149+
if (quotes.size() > Integer.parseInt(queryMap.get("index")) - 1 &&
150+
Integer.parseInt(queryMap.get("index")) - 1 > -1) {
151+
response = quotes.get(Integer.parseInt(queryMap.get("index")))
152+
;
153+
} else {
154+
response = "No quote found under that number!";
155+
}
145156
} else {
146-
response = "<html><body>No quote found under that number!</body></html>";
157+
response = quotes.get(new Random().nextInt(quotes.size()))
158+
;
147159
}
148160
} catch (NumberFormatException e) {
149-
response = "<html><body>That is an invalid number!</body></html>";
161+
response = "That is an invalid number!";
150162
}
151163
} else {
152-
response = "<html><body>Channel does not have any quotes!</body></html>";
164+
response = "Channel does not have any quotes!";
153165
}
154166

155167
HTTPUtils.throwSuccessHTML(exchange, response);
156168
} else {
157-
String response = "<html><body>Cannot find that channel or username!</body></html>";
169+
String response = "Cannot find that channel or username!";
158170

159171
HTTPUtils.throwError(exchange, response);
160172
}
161173
} else {
162-
String response = "<html><body>Moderator is unauthenticated! Head to https://nightbot.logicism.tv/ to Authenticate!</body></html>";
174+
String response = "Moderator is unauthenticated! Head to https://nightbot.logicism.tv/ to Authenticate!";
163175

164176
HTTPUtils.throwError(exchange, response);
165177
}
@@ -208,17 +220,17 @@ public void handle(HttpExchange exchange) throws IOException {
208220
File quotesMapDatabase = new File("quotesMap.dat");
209221
NightbotCommandsWebServer.INSTANCE.writeQuotesMap(quotesMapDatabase);
210222

211-
response = "<html><body>Added Quote: " +
212-
URLDecoder.decode(queryMap.get("text"), "UTF-8") + "</body></html>";
223+
response = "Added Quote: " +
224+
URLDecoder.decode(queryMap.get("text"), "UTF-8") ;
213225

214226
HTTPUtils.throwSuccessHTML(exchange, response);
215227
} else {
216-
String response = "<html><body>Cannot find that channel or username!</body></html>";
228+
String response = "Cannot find that channel or username!";
217229

218230
HTTPUtils.throwError(exchange, response);
219231
}
220232
} else {
221-
String response = "<html><body>Moderator is unauthenticated! Head to https://nightbot.logicism.tv/ to Authenticate!</body></html>";
233+
String response = "Moderator is unauthenticated! Head to https://nightbot.logicism.tv/ to Authenticate!";
222234

223235
HTTPUtils.throwError(exchange, response);
224236
}
@@ -262,7 +274,7 @@ public void handle(HttpExchange exchange) throws IOException {
262274

263275
if (NightbotCommandsWebServer.INSTANCE.getQuotesMap()
264276
.get(userArray.getJSONObject(0).getString("id")).isEmpty()) {
265-
response = "<html><body>Channel does not have any quotes!</body></html>";
277+
response = "Channel does not have any quotes!";
266278
} else {
267279
if (NightbotCommandsWebServer.INSTANCE.getQuotesMap()
268280
.get(userArray.getJSONObject(0).getString("id")).size()
@@ -278,20 +290,20 @@ public void handle(HttpExchange exchange) throws IOException {
278290
File quotesMapDatabase = new File("quotesMap.dat");
279291
NightbotCommandsWebServer.INSTANCE.writeQuotesMap(quotesMapDatabase);
280292

281-
response = "<html><body>Removed Quote: " + quote + "</body></html>";
293+
response = "Removed Quote: " + quote ;
282294
} else {
283-
response = "<html><body>Quote number does not exist</body></html>";
295+
response = "Quote number does not exist";
284296
}
285297
}
286298

287299
HTTPUtils.throwSuccessHTML(exchange, response);
288300
} else {
289-
String response = "<html><body>Cannot find that channel or username!</body></html>";
301+
String response = "Cannot find that channel or username!";
290302

291303
HTTPUtils.throwError(exchange, response);
292304
}
293305
} else {
294-
String response = "<html><body>Moderator is unauthenticated! Head to https://nightbot.logicism.tv/ to Authenticate!</body></html>";
306+
String response = "Moderator is unauthenticated! Head to https://nightbot.logicism.tv/ to Authenticate!";
295307

296308
HTTPUtils.throwError(exchange, response);
297309
}
@@ -337,29 +349,29 @@ public void handle(HttpExchange exchange) throws IOException {
337349
formattedQuotes.add("#" + i + " - " + s);
338350
}
339351

340-
response = "<html><body>" + StringUtils.join(formattedQuotes, ", ") + "</body></html>";
352+
response = StringUtils.join(formattedQuotes, ", ") ;
341353
} else {
342-
response = "<html><body>Channel does not have any quotes!</body></html>";
354+
response = "Channel does not have any quotes!";
343355
}
344356

345357
HTTPUtils.throwSuccessHTML(exchange, response);
346358
} else {
347-
String response = "<html><body>Cannot find that channel or username!</body></html>";
359+
String response = "Cannot find that channel or username!";
348360

349361
HTTPUtils.throwError(exchange, response);
350362
}
351363
} else {
352-
String response = "<html><body>Moderator is unauthenticated! Head to https://nightbot.logicism.tv/ to Authenticate!</body></html>";
364+
String response = "Moderator is unauthenticated! Head to https://nightbot.logicism.tv/ to Authenticate!";
353365

354366
HTTPUtils.throwError(exchange, response);
355367
}
356368
} else {
357-
String response = "<html><body>Command is unidentified!</body></html>";
369+
String response = "Command is unidentified!";
358370

359371
HTTPUtils.throwError(exchange, response);
360372
}
361373
} else {
362-
String response = "<html><body>Query requires 'channel' and 'moderator'</body></html>";
374+
String response = "Query requires 'channel' and 'moderator'";
363375

364376
HTTPUtils.throwError(exchange, response);
365377
}

src/me/Logicism/NightbotCommandsWebServer/util/HTTPUtils.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,6 @@ public static File getFile(String query) {
3333
return new File(query);
3434
}
3535

36-
public static void throwSuccessChallenge(HttpExchange exchange, String challenge) throws IOException {
37-
if (challenge != null) {
38-
OutputStream os = exchange.getResponseBody();
39-
40-
exchange.sendResponseHeaders(200, challenge.length());
41-
os.write(challenge.getBytes());
42-
os.flush();
43-
os.close();
44-
} else {
45-
throwError(exchange, null);
46-
}
47-
}
48-
4936
public static void throwSuccessFile(HttpExchange exchange, File file) throws IOException {
5037
if (file != null && file.exists()) {
5138
OutputStream os = exchange.getResponseBody();
@@ -83,14 +70,6 @@ public static void throwSuccessHTML(HttpExchange exchange, String html) throws I
8370
os.close();
8471
}
8572

86-
public static void throwSuccess(HttpExchange exchange) throws IOException {
87-
OutputStream os = exchange.getResponseBody();
88-
89-
exchange.sendResponseHeaders(204, -1);
90-
os.flush();
91-
os.close();
92-
}
93-
9473
public static void throwError(HttpExchange exchange, String error) throws IOException {
9574
OutputStream os = exchange.getResponseBody();
9675
String response = FileUtils.fileToString(getFile("/callbackError.html"))

0 commit comments

Comments
 (0)