Skip to content

Commit a9acf77

Browse files
committed
Added check for length, and also cleaned up async flow.
1 parent 4ec0691 commit a9acf77

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/main/java/net/javadiscord/javabot/systems/commands/ChangeMyMindCommand.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,26 @@
2424
*/
2525
@Deprecated
2626
public class ChangeMyMindCommand implements SlashCommand {
27+
/**
28+
* The maximum acceptable length for texts to send to the API. Technically,
29+
* the API supports up to but not including 2000, but we'll make that much
30+
* lower to avoid issues, since the API fails with a 501 error. People
31+
* shouldn't really be putting paragraphs into this anyway.
32+
*/
33+
private static final int MAX_SEARCH_TERM_LENGTH = 1500;
34+
2735
@Override
2836
public ReplyCallbackAction handleSlashCommandInteraction(SlashCommandInteractionEvent event) {
2937
var hook = event.getHook();
30-
String encodedSearchTerm = null;
38+
String encodedSearchTerm;
3139
try {
3240
encodedSearchTerm = URLEncoder.encode(Objects.requireNonNull(event.getOption("text")).getAsString(), StandardCharsets.UTF_8.toString());
3341
} catch (UnsupportedEncodingException e) {
3442
e.printStackTrace();
43+
return event.reply("The text you provided is using an unsupported encoding.");
44+
}
45+
if (encodedSearchTerm.toCharArray().length > MAX_SEARCH_TERM_LENGTH) {
46+
return event.reply("The text you provided is too long. It may not be more than " + MAX_SEARCH_TERM_LENGTH + " characters.");
3547
}
3648

3749
Unirest.get("https://nekobot.xyz/api/imagegen?type=changemymind&text=" + encodedSearchTerm).asJsonAsync(new Callback<>() {
@@ -49,12 +61,15 @@ public void completed(HttpResponse<JsonNode> hr) {
4961
hook.sendMessageEmbeds(e).queue();
5062
} catch (JSONException jsonException) {
5163
jsonException.printStackTrace();
64+
hook.sendMessage("The response from the ChangeMyMind API was not properly formatted.").queue();
5265
}
5366
}
5467

5568
@Override
5669
public void failed(UnirestException ue) {
5770
// Shouldn't happen
71+
ue.printStackTrace();
72+
hook.sendMessage("The request to the ChangeMyMind API failed.").queue();
5873
}
5974

6075
@Override

0 commit comments

Comments
 (0)