2017-03-08 11:35:30 +13:00
|
|
|
from . import utils
|
2016-09-24 17:33:17 +12:00
|
|
|
|
2016-07-14 14:24:41 +12:00
|
|
|
from discord.ext import commands
|
2016-07-14 15:15:55 +12:00
|
|
|
import discord
|
2016-07-14 14:24:41 +12:00
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
BASE_URL = "https://api.owapi.net/api/v3/u/"
|
2016-08-16 15:30:52 +12:00
|
|
|
# This is a list of the possible things that we may want to retrieve from the stats
|
|
|
|
# The API returns something if it exists, and leaves it out of the data returned entirely if it does not
|
2016-09-24 18:31:20 +12:00
|
|
|
# For example if you have not won with a character, wins will not exist in the list
|
2016-08-16 15:30:52 +12:00
|
|
|
# This sets an easy way to use list comprehension later, to print all possible things we want, if it exists
|
2016-07-18 09:10:12 +12:00
|
|
|
check_g_stats = ["eliminations", "deaths", 'kpd', 'wins', 'losses', 'time_played',
|
|
|
|
'cards', 'damage_done', 'healing_done', 'multikills']
|
2016-10-08 19:20:43 +13:00
|
|
|
check_o_stats = ['wins']
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-14 14:24:41 +12:00
|
|
|
|
|
|
|
class Overwatch:
|
2016-07-14 15:20:45 +12:00
|
|
|
"""Class for viewing Overwatch stats"""
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-14 14:24:41 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2016-11-02 08:59:45 +13:00
|
|
|
|
2016-07-15 00:29:04 +12:00
|
|
|
@commands.group(no_pm=True)
|
2016-07-14 20:39:31 +12:00
|
|
|
async def ow(self):
|
2016-07-29 08:25:22 +12:00
|
|
|
"""Command used to lookup information on your own user, or on another's
|
|
|
|
When adding your battletag, it is quite picky, use the exact format user#xxxx
|
|
|
|
Multiple names with the same username can be used, this is why the numbers are needed
|
|
|
|
Capitalization also matters"""
|
2016-07-14 20:39:31 +12:00
|
|
|
pass
|
|
|
|
|
2017-03-08 11:35:30 +13:00
|
|
|
@ow.command(name="stats")
|
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-08-31 10:33:46 +12:00
|
|
|
async def ow_stats(self, ctx, user: discord.Member = None, hero: str = ""):
|
2016-07-29 08:25:22 +12:00
|
|
|
"""Prints out a basic overview of a member's stats
|
2016-11-29 17:55:55 +13:00
|
|
|
Provide a hero after the member to get stats for that specific hero
|
|
|
|
|
|
|
|
EXAMPLE: !ow stats @OtherPerson Junkrat
|
|
|
|
RESULT: Whether or not you should unfriend this person because they're a dirty rat"""
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.message.channel.trigger_typing()
|
|
|
|
|
2016-09-29 12:39:34 +13:00
|
|
|
user = user or ctx.message.author
|
2017-03-08 20:55:22 +13:00
|
|
|
ow_stats = await utils.get_content('overwatch', str(user.id))
|
2016-08-10 14:13:53 +12:00
|
|
|
|
2016-09-29 12:39:34 +13:00
|
|
|
if ow_stats is None:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I do not have this user's battletag saved!")
|
2016-07-14 15:15:55 +12:00
|
|
|
return
|
2016-08-16 15:30:52 +12:00
|
|
|
# This API sometimes takes a while to look up information, so send a message saying we're processing
|
2016-08-31 10:33:46 +12:00
|
|
|
|
2017-03-08 19:43:40 +13:00
|
|
|
bt = ow_stats['battletag']
|
2016-09-29 12:39:34 +13:00
|
|
|
|
2016-07-25 02:02:50 +12:00
|
|
|
if hero == "":
|
2016-08-16 15:30:52 +12:00
|
|
|
# If no hero was provided, we just want the base stats for a player
|
2017-03-08 11:35:30 +13:00
|
|
|
url = BASE_URL + "{}/stats".format(bt)
|
|
|
|
data = await utils.request(url)
|
2017-03-20 12:02:34 +13:00
|
|
|
if data is None:
|
|
|
|
await ctx.send("I couldn't connect to overwatch at the moment!")
|
|
|
|
return
|
|
|
|
|
2017-01-18 04:35:22 +13:00
|
|
|
region = [x for x in data.keys() if data[x] is not None][0]
|
|
|
|
stats = data[region]['stats']['quickplay']
|
2016-08-31 10:33:46 +12:00
|
|
|
|
2017-01-18 04:35:22 +13:00
|
|
|
output_data = [(k.title().replace("_", " "), r) for k, r in stats['game_stats'].items() if
|
2016-11-02 08:59:45 +13:00
|
|
|
k in check_g_stats]
|
2016-07-25 02:02:50 +12:00
|
|
|
else:
|
2016-08-16 15:30:52 +12:00
|
|
|
# If there was a hero provided, search for a user's data on that hero
|
2017-01-18 04:31:09 +13:00
|
|
|
hero = hero.lower().replace('-', '')
|
2017-03-08 11:35:30 +13:00
|
|
|
url = BASE_URL + "{}/heroes".format(bt)
|
|
|
|
data = await utils.request(url)
|
2017-03-20 12:02:34 +13:00
|
|
|
if data is None:
|
|
|
|
await ctx.send("I couldn't connect to overwatch at the moment!")
|
|
|
|
return
|
2017-01-18 04:31:09 +13:00
|
|
|
|
|
|
|
region = [x for x in data.keys() if data[x] is not None][0]
|
|
|
|
stats = data[region]['heroes']['stats']['quickplay'].get(hero)
|
|
|
|
|
|
|
|
if stats is None:
|
2016-11-02 08:59:45 +13:00
|
|
|
fmt = "I couldn't find data with that hero, make sure that is a valid hero, " \
|
|
|
|
"otherwise {} has never used the hero {} before!".format(user.display_name, hero)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send(fmt)
|
2016-11-02 08:59:45 +13:00
|
|
|
return
|
2016-08-31 10:33:46 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Same list comprehension as before
|
2017-01-18 04:31:09 +13:00
|
|
|
output_data = [(k.title().replace("_", " "), r) for k, r in stats['general_stats'].items() if
|
2016-10-31 10:54:58 +13:00
|
|
|
k in check_g_stats]
|
2017-01-18 04:31:09 +13:00
|
|
|
for k, r in stats['hero_stats'].items():
|
2016-11-01 08:36:15 +13:00
|
|
|
output_data.append((k.title().replace("_", " "), r))
|
2016-10-16 18:27:07 +13:00
|
|
|
try:
|
2017-03-08 11:35:30 +13:00
|
|
|
banner = await utils.create_banner(user, "Overwatch", output_data)
|
|
|
|
await ctx.send(file=banner)
|
2016-10-16 18:27:07 +13:00
|
|
|
except (FileNotFoundError, discord.Forbidden):
|
2016-10-31 10:54:58 +13:00
|
|
|
fmt = "\n".join("{}: {}".format(k, r) for k, r in output_data)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Overwatch stats for {}: ```py\n{}```".format(user.name, fmt))
|
2016-07-14 14:24:41 +12:00
|
|
|
|
2016-10-07 14:16:25 +13:00
|
|
|
@ow.command(pass_context=True, name="add")
|
2017-03-08 11:35:30 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-07-14 15:15:55 +12:00
|
|
|
async def add(self, ctx, bt: str):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Saves your battletag for looking up information
|
|
|
|
|
|
|
|
EXAMPLE: !ow add Username#1234
|
|
|
|
RESULT: Your battletag is now saved"""
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.message.channel.trigger_typing()
|
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Battletags are normally provided like name#id
|
|
|
|
# However the API needs this to be a -, so repliace # with - if it exists
|
2016-07-14 15:15:55 +12:00
|
|
|
bt = bt.replace("#", "-")
|
2017-03-08 20:55:22 +13:00
|
|
|
key = str(ctx.message.author.id)
|
2016-08-31 10:33:46 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# All we're doing here is ensuring that the status is 200 when looking up someone's general information
|
|
|
|
# If it's not, let them know exactly how to format their tag
|
2017-03-08 11:35:30 +13:00
|
|
|
url = BASE_URL + "{}/stats".format(bt)
|
|
|
|
data = await utils.request(url)
|
2016-11-02 08:59:45 +13:00
|
|
|
if data is None:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("Profile does not exist! Battletags are picky, "
|
|
|
|
"format needs to be `user#xxxx`. Capitalization matters")
|
|
|
|
return
|
2016-08-31 10:33:46 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Now just save the battletag
|
2017-03-08 20:55:22 +13:00
|
|
|
entry = {'member_id': key, 'battletag': bt}
|
2016-09-29 14:57:19 +13:00
|
|
|
update = {'battletag': bt}
|
2016-09-29 12:39:34 +13:00
|
|
|
# Try adding this first, if that fails, update the saved entry
|
2017-03-08 17:28:30 +13:00
|
|
|
if not await utils.add_content('overwatch', entry):
|
|
|
|
await utils.update_content('overwatch', update, key)
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I have just saved your battletag {}".format(ctx.message.author.mention))
|
2016-07-14 14:24:41 +12:00
|
|
|
|
2016-10-07 14:16:25 +13:00
|
|
|
@ow.command(pass_context=True, name="delete", aliases=['remove'])
|
2017-03-08 11:35:30 +13:00
|
|
|
@utils.custom_perms(send_messages=True)
|
2016-07-14 14:27:10 +12:00
|
|
|
async def delete(self, ctx):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Removes your battletag from the records
|
|
|
|
|
|
|
|
EXAMPLE: !ow delete
|
|
|
|
RESULT: Your battletag is no longer saved"""
|
2017-03-08 20:55:22 +13:00
|
|
|
if await utils.remove_content('overwatch', str(ctx.message.author.id)):
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I no longer have your battletag saved {}".format(ctx.message.author.mention))
|
2016-07-14 14:24:41 +12:00
|
|
|
else:
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.send("I don't even have your battletag saved {}".format(ctx.message.author.mention))
|
2016-08-31 10:33:46 +12:00
|
|
|
|
2016-07-14 14:24:41 +12:00
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Overwatch(bot))
|