Search in sources :

Example 1 with APIKey

use of me.shadorc.shadbot.data.APIKeys.APIKey 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);
    }
}
Also used : HelpBuilder(me.shadorc.shadbot.utils.embed.HelpBuilder) CommandCategory(me.shadorc.shadbot.core.command.CommandCategory) BotUtils(me.shadorc.shadbot.utils.BotUtils) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) StringUtils(me.shadorc.shadbot.utils.StringUtils) Command(me.shadorc.shadbot.core.command.annotation.Command) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Utils(me.shadorc.shadbot.utils.Utils) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) RateLimited(me.shadorc.shadbot.core.command.annotation.RateLimited) APIKeys(me.shadorc.shadbot.data.APIKeys) IOException(java.io.IOException) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) FileNotFoundException(java.io.FileNotFoundException) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) List(java.util.List) NetUtils(me.shadorc.shadbot.utils.NetUtils) TreeMap(java.util.TreeMap) Context(me.shadorc.shadbot.core.command.Context) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) Emoji(me.shadorc.shadbot.utils.object.Emoji) APIKey(me.shadorc.shadbot.data.APIKeys.APIKey) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) Collections(java.util.Collections) JSONArray(org.json.JSONArray) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Collections (java.util.Collections)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 AbstractCommand (me.shadorc.shadbot.core.command.AbstractCommand)1 CommandCategory (me.shadorc.shadbot.core.command.CommandCategory)1 Context (me.shadorc.shadbot.core.command.Context)1 Command (me.shadorc.shadbot.core.command.annotation.Command)1 RateLimited (me.shadorc.shadbot.core.command.annotation.RateLimited)1 APIKeys (me.shadorc.shadbot.data.APIKeys)1 APIKey (me.shadorc.shadbot.data.APIKeys.APIKey)1 IllegalCmdArgumentException (me.shadorc.shadbot.exception.IllegalCmdArgumentException)1 MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)1 BotUtils (me.shadorc.shadbot.utils.BotUtils)1 FormatUtils (me.shadorc.shadbot.utils.FormatUtils)1 NetUtils (me.shadorc.shadbot.utils.NetUtils)1 StringUtils (me.shadorc.shadbot.utils.StringUtils)1 Utils (me.shadorc.shadbot.utils.Utils)1 EmbedUtils (me.shadorc.shadbot.utils.embed.EmbedUtils)1