2018-10-30 14:00:37 +13:00
|
|
|
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
|
2018-10-30 14:00:37 +13:00
|
|
|
import logging
|
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
|
2020-10-11 16:36:11 +13:00
|
|
|
check_g_stats = [
|
|
|
|
"eliminations",
|
|
|
|
"deaths",
|
|
|
|
"kpd",
|
|
|
|
"wins",
|
|
|
|
"losses",
|
|
|
|
"time_played",
|
|
|
|
"cards",
|
|
|
|
"damage_done",
|
|
|
|
"healing_done",
|
|
|
|
"multikills",
|
|
|
|
]
|
|
|
|
check_o_stats = ["wins"]
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2018-10-30 14:00:37 +13:00
|
|
|
log = logging.getLogger()
|
|
|
|
|
2016-07-14 14:24:41 +12:00
|
|
|
|
2019-02-24 09:13:10 +13:00
|
|
|
class Overwatch(commands.Cog):
|
2016-07-14 15:20:45 +12:00
|
|
|
"""Class for viewing Overwatch stats"""
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.group()
|
2017-07-29 01:43:34 +12:00
|
|
|
async def ow(self, ctx):
|
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")
|
2018-09-24 10:34:14 +12:00
|
|
|
@utils.can_run(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"""
|
2016-09-29 12:39:34 +13:00
|
|
|
user = user or ctx.message.author
|
2020-10-11 16:36:11 +13:00
|
|
|
bt = ctx.bot.db.load("overwatch", key=str(user.id), pluck="battletag")
|
2016-08-10 14:13:53 +12:00
|
|
|
|
2017-06-07 20:30:19 +12:00
|
|
|
if bt 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-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
|
|
|
|
|
2018-10-30 14:00:37 +13:00
|
|
|
log.info(data)
|
|
|
|
|
2020-10-11 16:36:11 +13:00
|
|
|
region = [
|
|
|
|
x
|
|
|
|
for x in data.keys()
|
|
|
|
if data[x] is not None and x in ["us", "any", "kr", "eu"]
|
|
|
|
][0]
|
|
|
|
stats = data[region]["stats"]["quickplay"]
|
|
|
|
|
|
|
|
output_data = [
|
|
|
|
(k.title().replace("_", " "), r)
|
|
|
|
for k, r in stats["game_stats"].items()
|
|
|
|
if 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
|
2020-10-11 16:36:11 +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
|
|
|
|
2020-10-11 16:36:11 +13:00
|
|
|
region = [
|
|
|
|
x
|
|
|
|
for x in data.keys()
|
|
|
|
if data[x] is not None and x in ["us", "any", "kr", "eu"]
|
|
|
|
][0]
|
|
|
|
stats = data[region]["heroes"]["stats"]["quickplay"].get(hero)
|
2017-01-18 04:31:09 +13:00
|
|
|
|
|
|
|
if stats is None:
|
2020-10-11 16:36:11 +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
|
2020-10-11 16:36:11 +13:00
|
|
|
output_data = [
|
|
|
|
(k.title().replace("_", " "), r)
|
|
|
|
for k, r in stats["general_stats"].items()
|
|
|
|
if k in check_g_stats
|
|
|
|
]
|
|
|
|
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)
|
2020-10-11 16:36:11 +13:00
|
|
|
await ctx.send(file=discord.File(banner, filename="banner.png"))
|
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)
|
2020-10-11 16:36:11 +13:00
|
|
|
await ctx.send(
|
|
|
|
"Overwatch stats for {}: ```py\n{}```".format(user.name, fmt)
|
|
|
|
)
|
2016-07-14 14:24:41 +12:00
|
|
|
|
2017-07-19 10:08:09 +12:00
|
|
|
@ow.command(name="add")
|
2018-09-24 10:34:14 +12:00
|
|
|
@utils.can_run(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"""
|
2019-01-28 15:58:39 +13:00
|
|
|
|
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:
|
2020-10-11 16:36:11 +13:00
|
|
|
await ctx.send(
|
|
|
|
"Profile does not exist! Battletags are picky, "
|
|
|
|
"format needs to be `user#xxxx`. Capitalization matters"
|
|
|
|
)
|
2017-03-08 11:35:30 +13:00
|
|
|
return
|
2016-08-31 10:33:46 +12:00
|
|
|
|
2016-08-16 15:30:52 +12:00
|
|
|
# Now just save the battletag
|
2020-10-11 16:36:11 +13:00
|
|
|
entry = {"member_id": key, "battletag": bt}
|
2017-06-07 20:30:19 +12:00
|
|
|
|
2020-10-11 16:36:11 +13:00
|
|
|
await ctx.bot.db.save("overwatch", entry)
|
|
|
|
await ctx.send(
|
|
|
|
"I have just saved your battletag {}".format(ctx.message.author.mention)
|
|
|
|
)
|
2016-07-14 14:24:41 +12:00
|
|
|
|
2020-10-11 16:36:11 +13:00
|
|
|
@ow.command(name="delete", aliases=["remove"])
|
2018-09-24 10:34:14 +12:00
|
|
|
@utils.can_run(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"""
|
2020-10-11 16:36:11 +13:00
|
|
|
entry = {"member_id": str(ctx.message.author.id), "battletag": None}
|
|
|
|
await ctx.bot.db.save("overwatch", entry)
|
|
|
|
await ctx.send(
|
|
|
|
"I no longer 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))
|