use of com.jockie.bot.core.option.Option in project Sx4 by sx4-discord-bot.
the class TranslateCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "to") Locale to, @Argument(value = "query | message id", endless = true, acceptEmpty = true) Or<MessageArgument, String> option, @Option(value = "from", description = "Choose what language to translate from") Locale from) {
String toTag = to.getLanguage(), fromTag = from == null ? "auto" : from.getLanguage();
this.getQuery(option).whenComplete((query, exception) -> {
if (query.length() > 1000) {
event.replyFailure("Query length cannot be any more than **1000** characters").queue();
return;
}
FormBody requestBody = new FormBody.Builder().addEncoded("f.req", "%5B%5B%5B%22MkEWBc%22%2C%22%5B%5B%5C%22" + URLEncoder.encode(query, StandardCharsets.UTF_8).replace("%0A", "\n") + "%5C%22%2C%5C%22" + fromTag + "%5C%22%2C%5C%22" + toTag + "%5C%22%2Ctrue%5D%2C%5Bnull%5D%5D%22%2Cnull%2C%22generic%22%5D%5D%5D").build();
Request request = new Request.Builder().url("https://translate.google.com/_/TranslateWebserverUi/data/batchexecute").post(requestBody).build();
event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> event.reply(this.getEmbed(response.body().string(), query, from, to)).queue());
});
}
use of com.jockie.bot.core.option.Option in project Sx4 by sx4-discord-bot.
the class RedditCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "subreddit") String subreddit, @Option(value = "sort", description = "What to sort the subreddit by") @Options({ "new", "hot", "top" }) @DefaultString("hot") @Lowercase String sort, @Option(value = "time", description = "If sort is top will choose the top posts after the set amount of time") @Options({ "day", "week", "month", "year", "all" }) @DefaultString("day") @Lowercase String time, @Option(value = "limit", description = "How many posts to show") @DefaultNumber(25) @Limit(min = 1, max = 100) int limit) {
Request request = new Request.Builder().url("https://reddit.com/r/" + subreddit + "/" + sort + ".json?t=" + time + "&limit=" + limit).build();
event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
if (response.code() == 302 || response.code() == 404) {
event.replyFailure("I could not find that subreddit").queue();
return;
}
List<Document> posts = Document.parse(response.body().string()).getEmbedded(List.of("data", "children"), Collections.emptyList());
posts = posts.stream().filter(post -> {
if (!event.getTextChannel().isNSFW()) {
return !post.getEmbedded(List.of("data", "over_18"), false);
}
return true;
}).map(post -> post.get("data", Document.class)).collect(Collectors.toList());
if (posts.isEmpty()) {
event.replyFailure("I could not find any posts with those filters").queue();
return;
}
PagedResult<Document> paged = new PagedResult<>(event.getBot(), posts).setPerPage(1).setSelect().setCustomFunction(page -> {
MessageBuilder builder = new MessageBuilder();
EmbedBuilder embed = new EmbedBuilder().setTitle("Page " + page.getPage() + "/" + page.getMaxPage());
page.forEach((data, index) -> {
String url = "https://reddit.com" + data.getString("permalink");
embed.setFooter(data.getString("subreddit_name_prefixed") + " | Upvotes: " + data.getInteger("ups"));
embed.setAuthor(StringUtility.limit(data.getString("title"), 50, "..."), url, "http://i.imgur.com/sdO8tAw.png");
String postHint = data.getString("post_hint");
if (postHint != null && postHint.equals("image")) {
embed.setImage(data.getString("url"));
} else if (postHint != null && postHint.equals("hosted:video")) {
embed.setImage(data.getString("thumbnail"));
builder.setContent("<" + data.getEmbedded(List.of("media", "reddit_video", "fallback_url"), String.class) + ">");
} else if (postHint != null && postHint.equals("rich:video")) {
embed.setImage(data.getEmbedded(List.of("media", "oembed", "thumbnail"), String.class));
builder.setContent("<" + data.getEmbedded(List.of("secure_media_embed", "media_domain_url"), String.class) + ">");
}
if (data.containsKey("selftext")) {
embed.setDescription(StringUtility.limit(data.getString("selftext"), MessageEmbed.DESCRIPTION_MAX_LENGTH, "... [Read more](" + url + ")"));
}
});
return builder.setEmbeds(embed.build());
});
paged.execute(event);
});
}
use of com.jockie.bot.core.option.Option in project Sx4 by sx4-discord-bot.
the class SourceCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "command", endless = true) Sx4Command command, @Option(value = "display", aliases = { "raw" }, description = "Sends the full command in discord") boolean display) {
Method method = command.getCommandMethod();
String path = method.getDeclaringClass().getName();
String className = path.substring(path.lastIndexOf('.'));
String[] classes = className.split("\\$");
String lastClassName = classes[classes.length - 1];
String filePath = path.replace(".", "/").substring(0, classes.length == 1 ? path.length() : path.indexOf("$")) + ".java";
String fullPath = Sx4.GIT_HASH + "/Sx4/src/main/java/" + filePath;
Request request = new Request.Builder().url("https://raw.githubusercontent.com/sx4-discord-bot/Sx4/" + fullPath).build();
event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
String code = response.body().string();
String[] lines = code.split("\n");
int classIndex = code.indexOf("class " + lastClassName);
int startBracketCount = 0, endBracketCount = 0, startLine = -1, leading = 0;
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
if (code.indexOf(line) < classIndex && startLine == -1) {
continue;
}
if (!line.contains(method.getName() + "(Sx4CommandEvent") && startLine == -1) {
continue;
}
if (startLine == -1) {
leading = line.indexOf("public");
startLine = i;
}
if (!line.isBlank()) {
lines[i] = line.substring(leading);
}
for (char character : line.toCharArray()) {
if (character == '{') {
startBracketCount++;
}
if (character == '}') {
endBracketCount++;
}
}
if (endBracketCount == startBracketCount) {
if (display) {
String commandCode = String.join("\n", Arrays.copyOfRange(lines, startLine, i + 1));
event.replyFile(commandCode.getBytes(StandardCharsets.UTF_8), className + ".java").queue();
} else {
event.reply("https://sx4.dev/github/Sx4/blob/" + fullPath + "#L" + (startLine + 1) + "-L" + (i + 1)).queue();
}
return;
}
}
});
}
Aggregations