Search in sources :

Example 1 with UrbanDictionary

use of tk.ardentbot.core.models.UrbanDictionary in project Ardent by adamint.

the class UD method noArgs.

@Override
public void noArgs(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
    Shard shard = GuildUtils.getShard(guild);
    if (args.length == 1) {
        sendTranslatedMessage("Search a word's definition from Urban Dictionary. Example: {0}ud test".replace("{0}", GuildUtils.getPrefix(guild)), channel, user);
    } else {
        GetRequest getRequest = Unirest.get("http://api.urbandictionary.com/v0/define?term=" + message.getRawContent().replace(GuildUtils.getPrefix(guild) + args[0] + " ", "").replaceAll(" ", "%20"));
        String json = "";
        try {
            json = getRequest.asJson().getBody().toString();
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        UrbanDictionary definition = shard.gson.fromJson(json, UrbanDictionary.class);
        if (definition.getList().size() == 0) {
            sendTranslatedMessage("There aren't any definitions for this word!", channel, user);
        } else {
            EmbedBuilder builder = MessageUtils.getDefaultEmbed(message.getAuthor());
            UDList list = definition.getList().get(0);
            String def = "Definition";
            String author = "Author";
            String example = "Example";
            String link = "Link";
            String thumbsUp = "Thumbs Up";
            String thumbsDown = "Thumbs Down";
            String urbanDictionary = "Urban Dictionary";
            builder.setAuthor(urbanDictionary, shard.bot.getAvatarUrl(), shard.bot.getAvatarUrl());
            builder.setThumbnail("https://i.gyazo.com/6a40e32928743e68e9006396ee7c2a14.jpg");
            builder.setColor(Color.decode("#00B7BE"));
            String definitionText = list.getDefinition();
            String exampleText = list.getExample();
            if (definitionText.length() > 1024)
                definitionText = definitionText.substring(0, 1023);
            if (exampleText.length() > 1024)
                exampleText = exampleText.substring(0, 1023);
            builder.addField(def, definitionText, true);
            builder.addField(example, exampleText, true);
            builder.addField(thumbsUp, String.valueOf(list.getThumbsUp() + ":thumbsup:"), true);
            builder.addField(thumbsDown, String.valueOf(list.getThumbsDown() + ":thumbsdown:"), true);
            builder.addField(author, list.getAuthor(), true);
            builder.addField(link, list.getPermalink(), true);
            sendEmbed(builder, channel, user);
        }
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) UDList(tk.ardentbot.core.models.UDList) GetRequest(com.mashape.unirest.request.GetRequest) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) UrbanDictionary(tk.ardentbot.core.models.UrbanDictionary) Shard(tk.ardentbot.main.Shard)

Aggregations

UnirestException (com.mashape.unirest.http.exceptions.UnirestException)1 GetRequest (com.mashape.unirest.request.GetRequest)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 UDList (tk.ardentbot.core.models.UDList)1 UrbanDictionary (tk.ardentbot.core.models.UrbanDictionary)1 Shard (tk.ardentbot.main.Shard)1