Skip to content

Commit

Permalink
Command conversion #6: Define Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Chew committed Jun 1, 2020
1 parent e434522 commit cef25e0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/java/pw/chew/Chewbotcca/Main.java
Expand Up @@ -13,6 +13,7 @@
import pw.chew.Chewbotcca.commands.about.PingCommand;
import pw.chew.Chewbotcca.commands.about.StatsCommand;
import pw.chew.Chewbotcca.commands.cat.CatCommand;
import pw.chew.Chewbotcca.commands.english.DefineCommand;

import javax.security.auth.login.LoginException;
import java.io.FileInputStream;
Expand Down Expand Up @@ -49,7 +50,10 @@ public static void main(String[] args) throws LoginException, IOException {
new StatsCommand(),

// Cat Module
new CatCommand()
new CatCommand(),

// English Module
new DefineCommand()
);

// Register listeners
Expand All @@ -65,4 +69,8 @@ public static void main(String[] args) throws LoginException, IOException {
public JDA getJDA() {
return jda;
}

public static Properties getProp() {
return prop;
}
}
@@ -0,0 +1,41 @@
package pw.chew.Chewbotcca.commands.english;

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import pw.chew.Chewbotcca.Main;
import pw.chew.Chewbotcca.util.RestClient;

public class DefineCommand extends Command {

public DefineCommand() {
this.name = "define";
this.botPermissions = new Permission[]{Permission.MESSAGE_EMBED_LINKS};
this.guildOnly = false;
}

@Override
protected void execute(CommandEvent commandEvent) {
String word = commandEvent.getArgs();
JSONArray grabbedword;
try {
grabbedword = new JSONArray(RestClient.get("http://api.wordnik.com/v4/word.json/" + word + "/definitions?limit=1&includeRelated=true&useCanonical=false&includeTags=false&api_key=" + Main.getProp().getProperty("wordnik")));
} catch(JSONException e) {
commandEvent.reply("Word not found! Check your local spell-checker!");
return;
}

commandEvent.reply(new EmbedBuilder()
.setTitle("Definition for " + word)
.setColor(0xd084)
.setDescription(grabbedword.getJSONObject(0).getString("text"))
.setAuthor("Dictionary", null, "https://icons.iconarchive.com/icons/johanchalibert/mac-osx-yosemite/1024/dictionary-icon.png")
.addField("Part of Speech", grabbedword.getJSONObject(0).getString("partOfSpeech"), true)
.build()
);
}
}

0 comments on commit cef25e0

Please sign in to comment.