Repository navigation
Expand file tree
/
Copy pathDictionaryCommand.java
More file actions
147 lines (133 loc) · 5.25 KB
/
Copy pathDictionaryCommand.java
File metadata and controls
147 lines (133 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package Command.Commands;
import Command.Structure.*;
import Hangman.DictWord;
import Hangman.Dictionary;
import Network.NetworkRequest;
import Network.NetworkResponse;
import Network.Secret;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.MessageEmbed;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.*;
/**
* Get the definitions of words!
*/
public class DictionaryCommand extends DiscordCommand {
private final String footer, thumbnail;
public DictionaryCommand() {
super("define", "Get the dictionary definition of a word!", "define [word]");
this.footer = "Try: " + getHelpName();
this.thumbnail = "https://i.imgur.com/pRu1hDN.png";
}
@Override
public void execute(CommandContext context) {
MessageChannel channel = context.getMessageChannel();
Member member = context.getMember();
String word = context.getLowerCaseMessage().replaceFirst(getTrigger(), "").trim();
if(word.isEmpty()) {
channel.sendMessage(member.getAsMention() + " You forgot the word bro").queue();
return;
}
DictWord dictWord = Dictionary.getInstance().getWord(word);
if(dictWord == null) {
ArrayList<DictWord> similar = Dictionary.getInstance().searchWords(word);
showSimilarWords(context, similar, word);
return;
}
channel.sendTyping().queue();
channel.sendMessage(buildDictionaryWordEmbed(dictWord)).queue();
}
/**
* Display a list of words and truncated descriptions in a pageable embed.
* The list of words are the result of searching the dictionary for the given query.
*
* @param context Command context
* @param words List of words found from the given query
* @param query Query used to find words
*/
private void showSimilarWords(CommandContext context, ArrayList<DictWord> words, String query) {
new PageableTableEmbed<DictWord>(
context,
words,
thumbnail,
"Dictionary Search",
words.size() + " words found for: **" + query + "**",
footer,
new String[]{
"Word",
"Definition"
},
5,
EmbedHelper.ORANGE
) {
@Override
public String getNoItemsDescription() {
return "That isn't in the dictionary bro, might need to give Merriam or Webster a call.";
}
@Override
public String[] getRowValues(int index, DictWord dictWord, boolean defaultSort) {
return new String[]{
dictWord.getWord(),
dictWord.getTruncatedDescription(50)
};
}
@Override
public void sortItems(List<DictWord> items, boolean defaultSort) {
items.sort(new LevenshteinDistance<DictWord>(query, defaultSort) {
@Override
public String getString(DictWord o) {
return o.getWord();
}
});
}
}.showMessage();
}
/**
* Build a message embed detailing the given dictionary word.
* Attempt to retrieve an image based on the word to use in the embed,
* if unsuccessful the embed will not contain an image.
*
* @param dictWord Dictionary word to display in the embed
* @return Message embed detailing dictionary word
*/
private MessageEmbed buildDictionaryWordEmbed(DictWord dictWord) {
EmbedBuilder builder = new EmbedBuilder()
.setColor(EmbedHelper.ORANGE)
.setTitle("Dictionary | " + dictWord.getWord())
.setDescription(dictWord.getTruncatedDescription(200))
.setThumbnail(thumbnail)
.setFooter(footer);
String imageUrl = getImageForWord(dictWord.getWord());
if(imageUrl != null) {
builder.setImage(imageUrl);
}
return builder.build();
}
/**
* Get a random image for the given word
*
* @param word Word to search
* @return Random image or null
*/
private String getImageForWord(String word) {
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", "Client-ID " + Secret.UNSPLASH_CLIENT_ID);
String url = "https://api.unsplash.com/search/photos?page=1&query=" + EmbedHelper.urlEncode(word);
NetworkResponse response = new NetworkRequest(url, false).get(headers);
JSONObject data = new JSONObject(response.body);
if(response.code != 200 || data.getLong("total") == 0) {
return null;
}
JSONArray results = data.getJSONArray("results");
JSONObject randomResult = results.getJSONObject(new Random().nextInt(results.length()));
return randomResult.getJSONObject("urls").getString("full");
}
@Override
public boolean matches(String query, Message message) {
return query.startsWith(getTrigger());
}
}