use of me.shadorc.shadbot.core.command.Context in project Shadbot by Shadorc.
the class DiabloCmd method execute.
@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
if (!context.hasArg()) {
throw new MissingArgumentException();
}
List<String> splitArgs = StringUtils.split(context.getArg(), 2);
if (splitArgs.size() != 2) {
throw new MissingArgumentException();
}
Region region = Utils.getValueOrNull(Region.class, splitArgs.get(0));
if (region == null) {
throw new IllegalCmdArgumentException(String.format("`%s` is not a valid Region. %s", splitArgs.get(0), FormatUtils.formatOptions(Region.class)));
}
String battletag = splitArgs.get(1);
if (!battletag.matches("(\\p{L}*)#[0-9]*")) {
throw new IllegalCmdArgumentException(String.format("`%s` is not a valid Battletag.", splitArgs.get(1)));
}
battletag = battletag.replaceAll("#", "-");
LoadingMessage loadingMsg = new LoadingMessage("Loading Diablo 3 stats...", context.getChannel());
loadingMsg.send();
try {
String url = String.format("https://%s.api.battle.net/d3/profile/%s/?locale=en_GB&apikey=%s", region, NetUtils.encode(battletag), APIKeys.get(APIKey.BLIZZARD_API_KEY));
JSONObject playerObj = new JSONObject(NetUtils.getBody(url));
if (playerObj.has("code") && playerObj.getString("code").equals("NOTFOUND")) {
loadingMsg.edit(Emoji.MAGNIFYING_GLASS + " This user doesn't play Diablo 3 or doesn't exist.");
return;
}
TreeMap<Double, String> heroesMap = new TreeMap<>(Collections.reverseOrder());
JSONArray heroesArray = playerObj.getJSONArray("heroes");
for (int i = 0; i < heroesArray.length(); i++) {
JSONObject heroObj = heroesArray.getJSONObject(i);
String name = heroObj.getString("name");
String heroClass = StringUtils.capitalize(heroObj.getString("class").replace("-", " "));
url = String.format("https://%s.api.battle.net/d3/profile/%s/hero/%d?locale=en_GB&apikey=%s", region, NetUtils.encode(battletag), heroObj.getLong("id"), APIKeys.get(APIKey.BLIZZARD_API_KEY));
JSONObject statsHeroObj = new JSONObject(NetUtils.getBody(url));
Double dps = statsHeroObj.has("code") ? Double.NaN : statsHeroObj.getJSONObject("stats").getDouble("damage");
heroesMap.put(dps, String.format("**%s** (*%s*)", name, heroClass));
}
EmbedBuilder embed = EmbedUtils.getDefaultEmbed().setLenient(true).withAuthorName("Diablo 3 Stats").withThumbnail("http://osx.wdfiles.com/local--files/icon:d3/D3.png").appendDescription(String.format("Stats for **%s** (Guild: **%s**)" + "%n%nParangon level: **%s** (*Normal*) / **%s** (*Hardcore*)" + "%nSeason Parangon level: **%s** (*Normal*) / **%s** (*Hardcore*)", playerObj.getString("battleTag"), playerObj.getString("guildName"), playerObj.getInt("paragonLevel"), playerObj.getInt("paragonLevelSeasonHardcore"), playerObj.getInt("paragonLevelSeason"), playerObj.getInt("paragonLevelSeasonHardcore"))).appendField("Heroes", FormatUtils.format(heroesMap.values().stream(), Object::toString, "\n"), true).appendField("Damage", FormatUtils.format(heroesMap.keySet().stream(), dps -> String.format("%s DPS", FormatUtils.formatNum(dps)), "\n"), true);
loadingMsg.edit(embed.build());
} catch (FileNotFoundException err) {
loadingMsg.delete();
BotUtils.sendMessage(Emoji.MAGNIFYING_GLASS + " This user doesn't play Diablo 3 or doesn't exist.", context.getChannel());
} catch (JSONException | IOException err) {
loadingMsg.delete();
Utils.handle("getting Diablo 3 stats", context, err);
}
}
use of me.shadorc.shadbot.core.command.Context in project Shadbot by Shadorc.
the class HelpCmd method execute.
@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
if (context.hasArg()) {
AbstractCommand cmd = CommandManager.getCommand(context.getArg());
if (cmd == null) {
return;
}
BotUtils.sendMessage(cmd.getHelp(context.getPrefix()), context.getChannel());
CommandStatsManager.log(CommandEnum.COMMAND_HELPED, cmd);
return;
}
EmbedBuilder embed = EmbedUtils.getDefaultEmbed().setLenient(true).withAuthorName("Shadbot Help").appendDescription(String.format("Any issues, questions or suggestions ? Join the [support server.](%s)" + "%nGet more information by using `%s%s <command>`.", Config.SUPPORT_SERVER, context.getPrefix(), this.getName()));
for (CommandCategory category : CommandCategory.values()) {
if (category.equals(CommandCategory.HIDDEN)) {
continue;
}
String commands = CommandManager.getCommands().values().stream().distinct().filter(cmd -> cmd.getCategory().equals(category) && !cmd.getPermission().isSuperior(context.getAuthorPermission()) && (context.getGuild() == null || BotUtils.isCommandAllowed(context.getGuild(), cmd))).map(cmd -> String.format("`%s%s`", context.getPrefix(), cmd.getName())).collect(Collectors.joining(" "));
embed.appendField(String.format("%s Commands", category.toString()), commands, false);
}
BotUtils.sendMessage(embed.build(), context.getChannel());
}
use of me.shadorc.shadbot.core.command.Context in project Shadbot by Shadorc.
the class Rule34Cmd method execute.
@Override
public void execute(Context context) throws MissingArgumentException {
if (!context.getChannel().isNSFW()) {
BotUtils.sendMessage(TextUtils.mustBeNSFW(context.getPrefix()), context.getChannel());
return;
}
if (!context.hasArg()) {
throw new MissingArgumentException();
}
LoadingMessage loadingMsg = new LoadingMessage("Loading image...", context.getChannel());
loadingMsg.send();
try {
String url = String.format("https://rule34.xxx/index.php?page=dapi&s=post&q=index&tags=%s", NetUtils.encode(context.getArg().replace(" ", "_")));
JSONObject mainObj = XML.toJSONObject(NetUtils.getBody(url));
JSONObject postsObj = mainObj.getJSONObject("posts");
if (postsObj.getInt("count") == 0) {
loadingMsg.edit(TextUtils.noResult(context.getArg()));
return;
}
JSONObject postObj;
if (postsObj.get("post") instanceof JSONArray) {
JSONArray postsArray = postsObj.getJSONArray("post");
postObj = postsArray.getJSONObject(ThreadLocalRandom.current().nextInt(postsArray.length()));
} else {
postObj = postsObj.getJSONObject("post");
}
List<String> tags = StringUtils.split(postObj.getString("tags"), " ");
if (postObj.getBoolean("has_children") || tags.stream().anyMatch(tag -> tag.contains("loli") || tag.contains("shota"))) {
loadingMsg.edit(Emoji.WARNING + " Sorry, I don't display images containing children or tagged with `loli` or `shota`.");
return;
}
String formattedtags = StringUtils.truncate(FormatUtils.format(tags, tag -> String.format("`%s`", tag.toString()), " "), MAX_TAGS_LENGTH);
EmbedBuilder embed = EmbedUtils.getDefaultEmbed().setLenient(true).withAuthorName("Rule34 (Search: " + context.getArg() + ")").withAuthorUrl(postObj.getString("file_url")).withThumbnail("http://rule34.paheal.net/themes/rule34v2/rule34_logo_top.png").appendField("Resolution", String.format("%dx%s", postObj.getInt("width"), postObj.getInt("height")), false).appendField("Tags", formattedtags, false).withImage(postObj.getString("file_url")).withFooterText("If there is no preview, click on the title to see the media (probably a video)");
String source = postObj.get("source").toString();
if (!source.isEmpty()) {
embed.withDescription(String.format("%n[**Source**](%s)", source));
}
loadingMsg.edit(embed.build());
} catch (JSONException | IOException err) {
loadingMsg.delete();
Utils.handle("getting an image from Rule34", context, err);
}
}
use of me.shadorc.shadbot.core.command.Context in project Shadbot by Shadorc.
the class JokeCmd method execute.
@Override
public void execute(Context context) throws MissingArgumentException {
LoadingMessage loadingMsg = new LoadingMessage("Loading joke...", context.getChannel());
loadingMsg.send();
try {
String url = String.format("http://www.une-blague.com/blagues-courtes.html?&p=%d", ThreadLocalRandom.current().nextInt(1, 6));
Document doc = NetUtils.getDoc(url);
List<String> jokes = doc.getElementsByClass("texte ").stream().map(elmt -> elmt.html()).filter(elmt -> elmt.length() < 1000).collect(Collectors.toList());
String jokeHtml = jokes.get(ThreadLocalRandom.current().nextInt(jokes.size()));
String joke = FormatUtils.format(jokeHtml.split("<br>"), line -> Jsoup.parse(line).text().trim(), "\n");
EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Blague").withAuthorUrl("http://www.une-blague.com/").appendDescription(joke);
loadingMsg.edit(embed.build());
} catch (IOException err) {
loadingMsg.delete();
Utils.handle("getting a joke", context, err);
}
}
use of me.shadorc.shadbot.core.command.Context in project Shadbot by Shadorc.
the class ChannelSetting method execute.
@Override
public void execute(Context context, String arg) throws MissingArgumentException, IllegalCmdArgumentException {
if (arg == null) {
throw new MissingArgumentException();
}
List<String> splitArgs = StringUtils.split(arg);
if (splitArgs.size() < 2) {
throw new MissingArgumentException();
}
List<IChannel> mentionedChannels = context.getMessage().getChannelMentions();
if (mentionedChannels.isEmpty()) {
throw new MissingArgumentException();
}
Action action = Utils.getValueOrNull(Action.class, splitArgs.get(0));
if (action == null) {
throw new IllegalCmdArgumentException(String.format("`%s` is not a valid action. %s", splitArgs.get(0), FormatUtils.formatOptions(Action.class)));
}
DBGuild dbGuild = Database.getDBGuild(context.getGuild());
List<Long> allowedChannelsList = dbGuild.getAllowedChannels();
if (Action.ADD.equals(action)) {
if (allowedChannelsList.isEmpty() && mentionedChannels.stream().noneMatch(channel -> channel.getLongID() == context.getChannel().getLongID())) {
BotUtils.sendMessage(Emoji.WARNING + " You did not mentioned this channel. " + "I will not reply here until this channel is added to the list of allowed channels.", context.getChannel());
}
allowedChannelsList.addAll(mentionedChannels.stream().map(IChannel::getLongID).filter(channelID -> !allowedChannelsList.contains(channelID)).collect(Collectors.toList()));
BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Channel %s added to allowed channels.", FormatUtils.format(mentionedChannels, IChannel::mention, ", ")), context.getChannel());
} else {
allowedChannelsList.removeAll(mentionedChannels.stream().map(IChannel::getLongID).collect(Collectors.toList()));
BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Channel %s removed from allowed channels.", FormatUtils.format(mentionedChannels, IChannel::mention, ", ")), context.getChannel());
}
dbGuild.setSetting(this.getSetting(), new JSONArray(allowedChannelsList));
}
Aggregations