1313import net .javadiscord .javabot .command .interfaces .SlashCommand ;
1414import org .json .JSONException ;
1515
16- import java .io .UnsupportedEncodingException ;
1716import java .net .URLEncoder ;
1817import java .nio .charset .StandardCharsets ;
1918import java .time .Instant ;
2423 */
2524@ Deprecated
2625public 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