Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Command conversion #6: Define Command
- Loading branch information
Showing
2 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/pw/chew/Chewbotcca/commands/english/DefineCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
); | ||
} | ||
} |