/
DocsCommand.java
288 lines (273 loc) · 12.1 KB
/
DocsCommand.java
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package com.almightyalpaca.discord.jdabutler.commands.commands;
import com.almightyalpaca.discord.jdabutler.Bot;
import com.almightyalpaca.discord.jdabutler.commands.ButtonListener;
import com.almightyalpaca.discord.jdabutler.commands.ReactionCommand;
import com.almightyalpaca.discord.jdabutler.commands.ReactionListenerRegistry;
import com.almightyalpaca.discord.jdabutler.util.EmbedUtil;
import com.almightyalpaca.discord.jdabutler.util.Paginator;
import com.kantenkugel.discordbot.jdocparser.Documentation;
import com.kantenkugel.discordbot.jdocparser.JDoc;
import com.kantenkugel.discordbot.jdocparser.JDocUtil;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import java.awt.*;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class DocsCommand extends ReactionCommand
{
private static final int RESULTS_PER_PAGE = 5;
private static final String[] ALIASES = new String[]{"documentation", "doc", "jdoc", "jdocs"};
private final ButtonListener buttons;
public DocsCommand(ReactionListenerRegistry registry, ButtonListener buttons)
{
super(registry);
this.buttons = buttons;
}
private static Message getDocMessage(String jDocBase, Documentation documentation)
{
EmbedBuilder embed = getDefaultEmbed()
.setTitle(documentation.getTitle(), documentation.getUrl(jDocBase));
Map<String, List<String>> fields = documentation.getFields();
if(fields != null && fields.get("Deprecated:") != null)
{
//element deprecated
embed.setColor(Color.RED);
}
else if(fields != null && fields.get("Incubating") != null)
{
//incubating
embed.setColor(Color.orange);
}
if (documentation.getContent().length() > MessageEmbed.TEXT_MAX_LENGTH)
{
embed.appendDescription("Description too long. Please refer to [the online docs](" + documentation.getUrl(jDocBase) + ')');
return new MessageBuilder().setEmbed(embed.build()).build();
}
if (documentation.getContent().length() == 0)
{
embed.appendDescription("No Description available.");
}
else
{
embed.appendDescription(documentation.getContent());
}
if (fields != null && fields.size() > 0)
{
for (Map.Entry<String, List<String>> field : fields.entrySet())
{
String fieldValue = String.join("\n", field.getValue());
if (fieldValue.length() > MessageEmbed.VALUE_MAX_LENGTH)
{
embed.addField(field.getKey(), "This section is too long. Please look at [the online docs](" + documentation.getUrl(jDocBase) + ')', false);
}
else
{
embed.addField(field.getKey(), String.join("\n", field.getValue()), false);
}
}
}
return new MessageBuilder().setEmbed(embed.build()).build();
}
private void showPaginatorEmbed(GuildMessageReceivedEvent event, String jDocBase, Set<Documentation> items)
{
Paginator paginator = new Paginator(event.getMessage());
buttons.addListener(paginator.getId(), paginator::onButtonClick);
List<Documentation> sorted = items.stream().sorted(Comparator.comparing(Documentation::getShortTitle)).collect(Collectors.toList());
for (int i = 0; i <= (sorted.size() - 1) / RESULTS_PER_PAGE; i++)
paginator.addPage(getMultiResult(jDocBase, sorted, i));
event.getMessage()
.reply(paginator.getCurrent())
.setActionRows(paginator.getButtons())
.mentionRepliedUser(false)
.queue(linkReply(event, null));
}
private void showRefinementEmbed(GuildMessageReceivedEvent event, User sender, String jDocBase, List<Documentation> docs)
{
EmbedBuilder embedB = getDefaultEmbed().setTitle("Refine your Search");
for (int i = 0; i < docs.size(); i++)
{
Documentation doc = docs.get(i);
embedB.appendDescription(
ReactionCommand.NUMBERS[i] + " [" + doc.getShortTitle() + "](" + doc.getUrl(jDocBase) + ")\n"
);
}
embedB.getDescriptionBuilder().setLength(embedB.getDescriptionBuilder().length() - 1);
List<String> options = Arrays.stream(ReactionCommand.NUMBERS).limit(docs.size()).collect(Collectors.toList());
options.add(ReactionCommand.CANCEL);
reply(event, embedB.build(),
m -> this.addReactions(m, options, Collections.singleton(sender), 30, TimeUnit.SECONDS, index -> {
if (index >= docs.size())
{ //cancel button or other error
stopReactions(m, false);
m.delete().queue();
return;
}
stopReactions(m);
m.editMessage(getDocMessage(jDocBase, docs.get(index))).queue();
})
);
}
private static Message getMultiResult(String jDocBase, List<Documentation> search, int page)
{
EmbedBuilder embed = getDefaultEmbed()
.setTitle("Found " + search.size() + " Results. Page " + (page + 1) + '/' + ((search.size() - 1) / RESULTS_PER_PAGE + 1));
for (int index = page * RESULTS_PER_PAGE; index < search.size() && index < (page + 1) * RESULTS_PER_PAGE; index++)
{
Documentation documentation = search.get(index);
embed.appendDescription('[' + documentation.getShortTitle() + "](" + documentation.getUrl(jDocBase) + ")\n");
}
return new MessageBuilder().setEmbed(embed.build()).build();
}
private static EmbedBuilder getDefaultEmbed()
{
return new EmbedBuilder().setAuthor("JDA Javadocs", null, EmbedUtil.getJDAIconUrl()).setColor(EmbedUtil.COLOR_JDA_PURPLE);
}
@Override
public void dispatch(final User sender, final TextChannel channel, final Message message, final String content, final GuildMessageReceivedEvent event)
{
if (content.trim().isEmpty())
{
reply(event, "See the docs here: " + JDocUtil.JDOCBASE);
return;
}
if (content.trim().equalsIgnoreCase("help"))
{
String cmd = Bot.config.getString("prefix") + "docs";
reply(event, "Searches and prints out documentation.\n"
+ "Syntax: `" + cmd + " [term | search:[params:]term | java:term | help]`.\n"
+ "\n"
+ "**Standard Mode**\n"
+ "When in `standard` mode, `term` is a class name or a class-prefixed variable or method name. "
+ "Both `.` and `#` can be used to specify inner classes, methods and variables. "
+ "Omitting the method parentheses will print all available methods with given name. "
+ "When specified (with parameter types), only the specific one is returned.\n"
+ "\n"
+ "__Examples__:\n"
+ "`" + cmd + " JDA#getUserById`\n"
+ "`" + cmd + " TextChannel.sendMessage`\n"
+ "`" + cmd + " TextChannel.sendMessage(Message)`\n"
+ "\n"
+ "**Search Mode**\n"
+ "When in `search` mode, `.` and `#` won't work and all documentations that **contain** `term` in their name/signature are returned.\n"
+ "Search parameters can be used to restrict the search:\n"
+ " `f ` - to only search for methods\n"
+ " `var` - to only search for variables\n"
+ " `c ` - to only search for classes\n"
+ " `cs ` - to make matching case-sensitive\n"
+ "\n"
+ "__Examples__:\n"
+ "`" + cmd + " search:onGuildMember`\n"
+ "`" + cmd + " search:c:join`\n"
+ "`" + cmd + " search:f:getTextChannel`\n"
+ "\n"
+ "**Java JDK Mode**\n"
+ "When in `java` mode, java 8 docs are searched instead. Syntax for `term` is the same as `standard` mode.\n"
+ "\n"
+ "__Examples__:\n"
+ "`" + cmd + " java:BufferedInputStream`\n"
+ "`" + cmd + " java:List.get`\n"
);
return;
}
if (content.contains(":"))
{
String[] split = content.split(":", 4);
if (split.length > 3)
{
reply(event, "Invalid syntax!");
return;
}
switch (split[0].toLowerCase())
{
case "search":
{
String[] options = split.length == 3 ? split[1].toLowerCase().split("\\s*,\\s*") : new String[0];
Set<Documentation> search = JDoc.search(split[split.length - 1], options);
if (search.size() == 0)
{
reply(event, "Did not find anything matching query!");
return;
}
if (search.size() > RESULTS_PER_PAGE)
{
showPaginatorEmbed(event, JDocUtil.JDOCBASE, search);
}
else
{
//show all items, but without emoji selector (search only)
EmbedBuilder embedB = getDefaultEmbed().setTitle("Found following:");
for (Documentation documentation : search)
{
embedB.appendDescription('[' + documentation.getShortTitle() + "](" + documentation.getUrl(JDocUtil.JDOCBASE) + ")\n");
}
embedB.getDescriptionBuilder().setLength(embedB.getDescriptionBuilder().length() - 1);
reply(event, embedB.build());
}
break;
}
case "java":
{
if (split.length != 2)
{
reply(event, "Invalid syntax!");
return;
}
List<Documentation> javadocs = JDoc.getJava(split[1]);
switch (javadocs.size())
{
case 0:
reply(event, "No Result found!");
break;
case 1:
reply(event, getDocMessage(JDocUtil.JAVA_JDOCS_PREFIX, javadocs.get(0)));
break;
default:
showRefinementEmbed(event, sender, JDocUtil.JAVA_JDOCS_PREFIX, javadocs);
break;
}
break;
}
default:
reply(event, "Unsupported operation " + split[0]);
break;
}
return;
}
//no : found -> normal lookup
final List<Documentation> docs = JDoc.get(content);
switch (docs.size())
{
case 0:
reply(event, "No Result found!");
break;
case 1:
reply(event, getDocMessage(JDocUtil.JDOCBASE, docs.get(0)));
break;
default:
showRefinementEmbed(event, sender, JDocUtil.JDOCBASE, docs);
break;
}
}
@Override
public String getHelp()
{
return "Displays documentation. Use `" + Bot.config.getString("prefix") + "docs help` for more help";
}
@Override
public String getName()
{
return "docs";
}
@Override
public String[] getAliases()
{
return DocsCommand.ALIASES;
}
}