Skip to content

Commit 8502a43

Browse files
Merge pull request #297 from andrewlalis/andrew/fix_changemymindcommand
Cleaned up ChangeMyMindCommand
2 parents 45dbe76 + d2152e7 commit 8502a43

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import net.javadiscord.javabot.command.interfaces.SlashCommand;
1414
import org.json.JSONException;
1515

16-
import java.io.UnsupportedEncodingException;
1716
import java.net.URLEncoder;
1817
import java.nio.charset.StandardCharsets;
1918
import java.time.Instant;
@@ -24,14 +23,21 @@
2423
*/
2524
@Deprecated
2625
public class ChangeMyMindCommand implements SlashCommand {
26+
/**
27+
* The maximum acceptable length for texts to send to the API. Technically,
28+
* the API supports up to but not including 2000, but we'll make that much
29+
* lower to avoid issues, since the API fails with a 501 error. People
30+
* shouldn't really be putting paragraphs into this anyway.
31+
*/
32+
private static final int MAX_SEARCH_TERM_LENGTH = 500;
33+
2734
@Override
2835
public ReplyCallbackAction handleSlashCommandInteraction(SlashCommandInteractionEvent event) {
2936
var hook = event.getHook();
30-
String encodedSearchTerm = null;
31-
try {
32-
encodedSearchTerm = URLEncoder.encode(Objects.requireNonNull(event.getOption("text")).getAsString(), StandardCharsets.UTF_8.toString());
33-
} catch (UnsupportedEncodingException e) {
34-
e.printStackTrace();
37+
String encodedSearchTerm;
38+
encodedSearchTerm = URLEncoder.encode(Objects.requireNonNull(event.getOption("text")).getAsString(), StandardCharsets.UTF_8);
39+
if (encodedSearchTerm.length() > MAX_SEARCH_TERM_LENGTH) {
40+
return event.reply("The text you provided is too long. It may not be more than " + MAX_SEARCH_TERM_LENGTH + " characters.");
3541
}
3642

3743
Unirest.get("https://nekobot.xyz/api/imagegen?type=changemymind&text=" + encodedSearchTerm).asJsonAsync(new Callback<>() {
@@ -49,12 +55,15 @@ public void completed(HttpResponse<JsonNode> hr) {
4955
hook.sendMessageEmbeds(e).queue();
5056
} catch (JSONException jsonException) {
5157
jsonException.printStackTrace();
58+
hook.sendMessage("The response from the ChangeMyMind API was not properly formatted.").queue();
5259
}
5360
}
5461

5562
@Override
5663
public void failed(UnirestException ue) {
5764
// Shouldn't happen
65+
ue.printStackTrace();
66+
hook.sendMessage("The request to the ChangeMyMind API failed.").queue();
5867
}
5968

6069
@Override

0 commit comments

Comments
 (0)