Search in sources :

Example 1 with Dictionary

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

the class Define method noArgs.

@Override
public void noArgs(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
    if (args.length == 1) {
        sendTranslatedMessage("You need to include a word to define ^_^", channel, user);
    } else {
        Calendar myCal = Calendar.getInstance();
        myCal.setTimeInMillis(System.currentTimeMillis());
        boolean isMidnight = myCal.get(Calendar.HOUR_OF_DAY) == 0 && myCal.get(Calendar.MINUTE) == 0 && myCal.get(Calendar.SECOND) == 0 && myCal.get(Calendar.MILLISECOND) == 0;
        if (isMidnight)
            requests = 0;
        else {
            if (requests < 2000) {
                Shard shard = getShard();
                String query = message.getContent().replace(GuildUtils.getPrefix(guild) + args[0] + " ", "");
                try {
                    Dictionary dictionary = shard.gson.fromJson(Unirest.get("https://wordsapiv1.p.mashape" + ".com/words/" + query + "/definitions").header("X-Mashape-Key", Ardent.mashapeKey).header("Accept", "application/json").asString().getBody(), Dictionary.class);
                    EmbedBuilder builder = MessageUtils.getDefaultEmbed(user);
                    builder.setAuthor("English Dictionary", "https://ardentbot.tk", shard.bot.getAvatarUrl());
                    List<Definition> definitions = dictionary.getDefinitions();
                    StringBuilder description = new StringBuilder();
                    description.append("Word: **" + query + "**\n");
                    if (definitions.size() == 0)
                        description.append("There were no definitions for this word");
                    else {
                        Definition first = definitions.get(0);
                        description.append("Part of Speech: " + first.getPartOfSpeech() + "\n");
                        description.append("Definition: " + first.getDefinition());
                    }
                    builder.setDescription(description.toString());
                    sendEmbed(builder, channel, user);
                } catch (Exception ex) {
                    sendTranslatedMessage("There were no definitions for this word!", channel, user);
                }
            } else
                sendTranslatedMessage("We have hit the daily API limit for the dictionary, sorry!", channel, user);
        }
    }
}
Also used : Dictionary(tk.ardentbot.core.models.Dictionary) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Calendar(java.util.Calendar) Definition(tk.ardentbot.core.models.Definition) Shard(tk.ardentbot.main.Shard)

Aggregations

Calendar (java.util.Calendar)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 Definition (tk.ardentbot.core.models.Definition)1 Dictionary (tk.ardentbot.core.models.Dictionary)1 Shard (tk.ardentbot.main.Shard)1