use of at.xirado.bean.misc.urbandictionary.UrbanDefinition in project Bean by Xirado.
the class UrbanDictionaryCommand method executeCommand.
@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
String phrase = event.getOption("phrase").getAsString();
int index = event.getOption("definition") != null ? (int) event.getOption("definition").getAsLong() : 1;
if (index < 1)
index = 1;
event.deferReply().queue();
LinkedDataObject dataObject;
try {
String url = "http://api.urbandictionary.com/v0/define?term=" + phrase.replaceAll("\\s+", "+");
Response response = Bean.getInstance().getOkHttpClient().newCall(new Request.Builder().url(url).build()).execute();
dataObject = LinkedDataObject.parse(response.body().byteStream());
response.close();
} catch (Exception ex) {
LOGGER.error("Could not get data from API!", ex);
event.getHook().sendMessageEmbeds(EmbedUtil.errorEmbed("An error occurred, please try again later.")).queue();
return;
}
UrbanDefinition[] results = dataObject.convertValueAt("list", UrbanDefinition[].class);
if (results.length == 0) {
EmbedBuilder builder = new EmbedBuilder().setColor(Color.decode("#1D2439")).setTitle(ctx.getLocalized("commands.urban.not_found")).setTimestamp(Instant.now());
event.getHook().sendMessageEmbeds(builder.build()).queue();
return;
}
if (results.length < index)
index = results.length;
UrbanDefinition result = results[index - 1];
String description = result.getDefinition();
Matcher matcher = PATTERN.matcher(description);
description = matcher.replaceAll(match -> "[" + match.group().replaceAll("\\[|\\]", "") + "]" + "(https://urbandictionary.com/define.php?term=" + match.group().replaceAll("\\s+", "+").replaceAll("\\[|\\]", "") + ")");
if (description.length() > 4096) {
String replaceString = "... [**" + ctx.getLocalized("general.read_more") + "**](" + result.getPermalink() + ")";
String split = description.substring(0, 4096 - replaceString.length());
description = split + replaceString;
}
EmbedBuilder builder = new EmbedBuilder().setColor(Color.decode("#1D2439")).setTitle(result.getWord()).setTitle(result.getWord(), result.getPermalink()).setFooter(Util.ordinal(index) + " definition").setFooter(Util.ordinal(index) + " definition", "https://bean.bz/assets/udlogo.png").setDescription(description);
String example = result.getExample();
if (example != null) {
boolean alreadyRecursive = example.startsWith("*") && example.endsWith("*");
Matcher matcher2 = PATTERN.matcher(example);
example = matcher2.replaceAll(match -> "[" + match.group().replaceAll("\\[|\\]", "") + "]" + "(https://www.urbandictionary.com/define.php?term=" + match.group().replaceAll(" ", "+").replaceAll("\\[|\\]", "") + ")");
if (example.length() <= 1022)
builder.addField(ctx.getLocalized("general.example"), ((alreadyRecursive) ? (example) : ("*" + example + "*")), false);
}
String authorUrl = "https://www.urbandictionary.com/author.php?author=";
authorUrl += result.getAuthor().replaceAll("\\s+", "%20");
builder.setTimestamp(Instant.parse(result.getWrittenOn()));
builder.addField(ctx.getLocalized("general.author"), "[" + result.getAuthor() + "](" + authorUrl + ")\n\uD83D\uDC4D " + result.getUpvotes() + " \uD83D\uDC4E " + result.getDownvotes(), false);
event.getHook().sendMessageEmbeds(builder.build()).queue();
}
Aggregations