Search in sources :

Example 1 with UrbanDefinition

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();
}
Also used : Response(okhttp3.Response) OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Request(okhttp3.Request) Logger(org.slf4j.Logger) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) LoggerFactory(org.slf4j.LoggerFactory) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) Util(at.xirado.bean.misc.Util) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Instant(java.time.Instant) java.awt(java.awt) Commands(net.dv8tion.jda.api.interactions.commands.build.Commands) Matcher(java.util.regex.Matcher) SlashCommandContext(at.xirado.bean.command.SlashCommandContext) Response(okhttp3.Response) SlashCommand(at.xirado.bean.command.SlashCommand) UrbanDefinition(at.xirado.bean.misc.urbandictionary.UrbanDefinition) Pattern(java.util.regex.Pattern) Bean(at.xirado.bean.Bean) NotNull(org.jetbrains.annotations.NotNull) LinkedDataObject(at.xirado.bean.data.LinkedDataObject) EmbedUtil(at.xirado.bean.misc.EmbedUtil) LinkedDataObject(at.xirado.bean.data.LinkedDataObject) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) UrbanDefinition(at.xirado.bean.misc.urbandictionary.UrbanDefinition) Matcher(java.util.regex.Matcher) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder)

Aggregations

Bean (at.xirado.bean.Bean)1 SlashCommand (at.xirado.bean.command.SlashCommand)1 SlashCommandContext (at.xirado.bean.command.SlashCommandContext)1 LinkedDataObject (at.xirado.bean.data.LinkedDataObject)1 EmbedUtil (at.xirado.bean.misc.EmbedUtil)1 Util (at.xirado.bean.misc.Util)1 UrbanDefinition (at.xirado.bean.misc.urbandictionary.UrbanDefinition)1 java.awt (java.awt)1 Instant (java.time.Instant)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)1 OptionType (net.dv8tion.jda.api.interactions.commands.OptionType)1 Commands (net.dv8tion.jda.api.interactions.commands.build.Commands)1 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 NotNull (org.jetbrains.annotations.NotNull)1 Logger (org.slf4j.Logger)1