|
2 | 2 |
|
3 | 3 | import de.btobastian.sdcf4j.Command;
|
4 | 4 | import de.btobastian.sdcf4j.CommandExecutor;
|
| 5 | +import org.javacord.api.DiscordApi; |
5 | 6 | import org.javacord.api.entity.channel.TextChannel;
|
6 | 7 | import org.javacord.api.entity.message.embed.EmbedBuilder;
|
| 8 | +import org.javacord.api.util.logging.ExceptionLogger; |
7 | 9 | import org.javacord.bot.Constants;
|
| 10 | +import org.javacord.bot.util.wiki.parser.WikiPage; |
| 11 | +import org.javacord.bot.util.wiki.parser.WikiParser; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.List; |
| 16 | +import java.util.function.Predicate; |
| 17 | +import java.util.regex.Pattern; |
| 18 | +import java.util.stream.Collectors; |
8 | 19 |
|
9 | 20 | /**
|
10 | 21 | * The !wiki command which is used to link to Javacord's wiki.
|
11 | 22 | */
|
12 | 23 | public class WikiCommand implements CommandExecutor {
|
13 | 24 |
|
| 25 | + private static final Pattern HTML_TAG = Pattern.compile("<[^>]++>"); |
| 26 | + |
14 | 27 | /**
|
15 | 28 | * Executes the {@code !wiki} command.
|
16 | 29 | *
|
| 30 | + * @param api The Discord api. |
17 | 31 | * @param channel The channel where the command was issued.
|
| 32 | + * @param args The command's arguments. |
| 33 | + * @throws IOException If the connection to the wiki failed. |
18 | 34 | */
|
19 | 35 | @Command(aliases = {"!wiki"}, async = true)
|
20 |
| - public void onCommand(TextChannel channel) { |
21 |
| - EmbedBuilder embed = new EmbedBuilder() |
22 |
| - .setTitle("Javacord Wiki") |
23 |
| - .setDescription("https://javacord.org/wiki") |
24 |
| - .setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png") |
25 |
| - .setColor(Constants.JAVACORD_ORANGE); |
26 |
| - channel.sendMessage(embed).join(); |
| 36 | + public void onCommand(DiscordApi api, TextChannel channel, String[] args) throws IOException { |
| 37 | + try { |
| 38 | + if (args.length == 0) { // Just an overview |
| 39 | + EmbedBuilder embed = new EmbedBuilder() |
| 40 | + .setTitle("Javacord Wiki") |
| 41 | + .setDescription("The [Javacord Wiki](" + WikiParser.BASE_URL + "/wiki) is an excellent " |
| 42 | + + "resource to get you started with Javacord.\n") |
| 43 | + .addInlineField("Hint", "You can search the wiki using `!wiki [title|full] <search>") |
| 44 | + .setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png") |
| 45 | + .setColor(Constants.JAVACORD_ORANGE); |
| 46 | + channel.sendMessage(embed).join(); |
| 47 | + } else { |
| 48 | + EmbedBuilder embed = new EmbedBuilder() |
| 49 | + .setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png") |
| 50 | + .setColor(Constants.JAVACORD_ORANGE); |
| 51 | + String searchString = String.join(" ", Arrays.copyOfRange(args, 1, args.length)).toLowerCase(); |
| 52 | + switch (args[0]) { |
| 53 | + case "page": |
| 54 | + case "p": |
| 55 | + case "title": |
| 56 | + case "t": |
| 57 | + populatePages(api, embed, titleOnly(searchString)); |
| 58 | + break; |
| 59 | + case "full": |
| 60 | + case "f": |
| 61 | + case "content": |
| 62 | + case "c": |
| 63 | + populatePages(api, embed, fullSearch(searchString)); |
| 64 | + break; |
| 65 | + default: |
| 66 | + searchString = String.join(" ", Arrays.copyOfRange(args, 0, args.length)).toLowerCase(); |
| 67 | + populatePages(api, embed, defaultSearch(searchString)); |
| 68 | + } |
| 69 | + channel.sendMessage(embed).join(); |
| 70 | + } |
| 71 | + } catch (Throwable t) { |
| 72 | + channel.sendMessage("Something went wrong: ```" + ExceptionLogger.unwrapThrowable(t).getMessage() + "```") |
| 73 | + .join(); |
| 74 | + // Throw the caught exception again. The sdcf4j will log it. |
| 75 | + throw t; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private Predicate<WikiPage> defaultSearch(String searchString) { |
| 80 | + return titleOnly(searchString).or(keywordsOnly(searchString)); |
| 81 | + } |
| 82 | + |
| 83 | + private Predicate<WikiPage> fullSearch(String searchString) { |
| 84 | + return titleOnly(searchString).or(keywordsOnly(searchString)).or(contentOnly(searchString)); |
| 85 | + } |
| 86 | + |
| 87 | + private Predicate<WikiPage> titleOnly(String searchString) { |
| 88 | + return p -> p.getTitle().toLowerCase().contains(searchString); |
| 89 | + } |
| 90 | + |
| 91 | + private Predicate<WikiPage> keywordsOnly(String searchString) { |
| 92 | + return p -> Arrays.stream(p.getKeywords()) |
| 93 | + .map(String::toLowerCase) |
| 94 | + .anyMatch(k -> k.contains(searchString)); |
| 95 | + } |
| 96 | + |
| 97 | + private Predicate<WikiPage> contentOnly(String searchString) { |
| 98 | + return p -> p.getContent().toLowerCase().contains(searchString); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + private void populatePages(DiscordApi api, EmbedBuilder embed, Predicate<WikiPage> criteria) throws IOException { |
| 103 | + List<WikiPage> pages; |
| 104 | + |
| 105 | + pages = new WikiParser(api) |
| 106 | + .getPagesBlocking().stream() |
| 107 | + .filter(criteria) |
| 108 | + .sorted() |
| 109 | + .collect(Collectors.toList()); |
| 110 | + |
| 111 | + if (pages.isEmpty()) { |
| 112 | + embed.setTitle("Javacord Wiki"); |
| 113 | + embed.setUrl(WikiParser.BASE_URL + "/wiki/"); |
| 114 | + embed.setDescription("No pages found. Maybe try another search."); |
| 115 | + embed.addField("Standard Search", "Use `!wiki <search>` to search page titles and keywords."); |
| 116 | + embed.addField("Title Search", "Use `!wiki [page|p|title|t] <search>` to exclusively search page titles."); |
| 117 | + embed.addField("Full Search", "Use `!wiki [full|f|content|c] <search>` to perform a full search."); |
| 118 | + } else if (pages.size() == 1) { |
| 119 | + WikiPage page = pages.get(0); |
| 120 | + displayPagePreview(embed, page); |
| 121 | + } else { |
| 122 | + displayPageList(embed, pages); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private void displayPagePreview(EmbedBuilder embed, WikiPage page) { |
| 127 | + embed.setTitle("Javacord Wiki"); |
| 128 | + String cleanedDescription = HTML_TAG.matcher(page.getContent()).replaceAll("").trim(); |
| 129 | + int length = 0; |
| 130 | + int sentences = 0; |
| 131 | + while (length < 600 && sentences < 3) { |
| 132 | + int tmpLength = cleanedDescription.indexOf(". ", length); |
| 133 | + length = (tmpLength > length) ? tmpLength : cleanedDescription.indexOf(".\n"); |
| 134 | + |
| 135 | + sentences++; |
| 136 | + } |
| 137 | + StringBuilder description = new StringBuilder() |
| 138 | + .append(String.format("**[%s](%s)**\n\n", page.getTitle(), WikiParser.BASE_URL + page.getUrl())) |
| 139 | + .append(cleanedDescription, 0, length + 1); |
| 140 | + if (length < cleanedDescription.length()) { |
| 141 | + description.append("\n\n[*view full page*](").append(WikiParser.BASE_URL).append(page.getUrl()).append(")"); |
| 142 | + } |
| 143 | + embed.setDescription(description.toString()); |
| 144 | + } |
| 145 | + |
| 146 | + private void displayPageList(EmbedBuilder embed, List<WikiPage> pages) { |
| 147 | + embed.setTitle("Javacord Wiki"); |
| 148 | + embed.setUrl(WikiParser.BASE_URL + "/wiki/"); |
| 149 | + |
| 150 | + StringBuilder builder = new StringBuilder(); |
| 151 | + int counter = 0; |
| 152 | + for (WikiPage page : pages) { |
| 153 | + String pageLink = "• " + page.asMarkdownLink(); |
| 154 | + if (builder.length() + pageLink.length() > 1900) { // Prevent hitting the description size limit |
| 155 | + break; |
| 156 | + } |
| 157 | + builder.append(pageLink).append("\n"); |
| 158 | + counter++; |
| 159 | + } |
| 160 | + if (pages.size() > counter) { |
| 161 | + builder.append("and ").append(pages.size() - counter).append(" more ..."); |
| 162 | + } |
| 163 | + embed.setDescription(builder.toString()); |
27 | 164 | }
|
28 | 165 |
|
29 | 166 | }
|
0 commit comments