Skip to content

Commit

Permalink
Escaped newlines in strings to prevent faliure of &embedraw
Browse files Browse the repository at this point in the history
  • Loading branch information
raynichc committed Apr 5, 2020
1 parent 33aa807 commit fb6dabc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
27 changes: 10 additions & 17 deletions src/main/java/com/ibdiscord/command/registrar/RegistrarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.ibdiscord.reminder.Reminder;
import com.ibdiscord.reminder.ReminderHandler;
import com.ibdiscord.utils.UDatabase;
import com.ibdiscord.utils.UInput;
import com.ibdiscord.utils.UString;
import de.arraying.gravity.Gravity;
import de.arraying.kotys.JSON;
Expand Down Expand Up @@ -86,15 +87,11 @@ public void register(CommandRegistry registry) {
return;
}
context.assertArguments(2, "error.missing_data");
StringBuilder jsonBuilder = new StringBuilder();
for (int i = 1; i < context.getArguments().length; i++) {
jsonBuilder.append(context.getArguments()[i]).append(" ");
}

String json = UInput.escapeLinebreakInQuotations(UString.concat(context.getArguments(), " ", 1));
JSON embed;

try {
embed = new JSON(jsonBuilder.toString());
embed = new JSON(json.toString());
} catch (IllegalStateException e) {
context.replyI18n("error.missing_data");
return;
Expand Down Expand Up @@ -174,18 +171,14 @@ public void register(CommandRegistry registry) {
.noneMatch(it -> it.getName().equalsIgnoreCase("update"))) {
target.sendMessage(builder.build()).queue();
} else {
try {
for (Option option : context.getOptions()) {
if (option.getName().equalsIgnoreCase("update")) {
long id = Long.valueOf(option.getValue());
target.retrieveMessageById(id)
.queue(success -> target.editMessageById(id, builder.build()).queue(),
failure -> context.replyI18n("error.pin_channel"));
break;
}
for (Option option : context.getOptions()) {
if (option.getName().equalsIgnoreCase("update")) {
long id = UString.toLong(option.getValue());
target.retrieveMessageById(id)
.queue(success -> target.editMessageById(id, builder.build()).queue(),
failure -> context.replyI18n("error.pin_channel"));
break;
}
} catch(NumberFormatException exception) {
exception.printStackTrace();
}
}
});
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/ibdiscord/utils/UInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public final class UInput {
*/
private static final Pattern QUOTATION_MARK = Pattern.compile("[“”„«»]");

/**
* The regular expression for a (universal) quotation.
*/
private static final Pattern QUOTATION = Pattern.compile("[\"“”„«»][^“”„«»\"]*[“”„«»\"]");

/**
* Gets the member corresponding to the given user input.
* @param guild The guild.
Expand Down Expand Up @@ -204,4 +209,17 @@ public static boolean isValidRegex(String regex) {
}
}

/**
* Escapes newline characters found within quotations only.
* @param input The string to escape.
* @return Escaped string.
*/
public static String escapeLinebreakInQuotations(String input) {
StringBuilder string = new StringBuilder(input);
QUOTATION.matcher(string.toString()).results().forEach(matchResult ->
string.replace(matchResult.start(), matchResult.end(),
matchResult.group().replaceAll("\n", "\\\\n")));
return string.toString();
}

}

0 comments on commit fb6dabc

Please sign in to comment.