1
0
Fork 0
mirror of synced 2024-05-16 02:22:32 +12:00
Bonfire/cogs/overwatch.py

170 lines
6 KiB
Python
Raw Normal View History

import utils
from discord.ext import commands
2016-07-14 15:15:55 +12:00
import discord
import logging
2017-03-08 11:35:30 +13:00
BASE_URL = "https://api.owapi.net/api/v3/u/"
# 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
# For example if you have not won with a character, wins will not exist in the list
# This sets an easy way to use list comprehension later, to print all possible things we want, if it exists
check_g_stats = [
"eliminations",
"deaths",
"kpd",
"wins",
"losses",
"time_played",
"cards",
"damage_done",
"healing_done",
"multikills",
]
check_o_stats = ["wins"]
log = logging.getLogger()
2019-02-24 09:13:10 +13:00
class Overwatch(commands.Cog):
"""Class for viewing Overwatch stats"""
@commands.group()
2017-07-29 01:43:34 +12:00
async def ow(self, ctx):
"""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"""
pass
2017-03-08 11:35:30 +13:00
@ow.command(name="stats")
@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 = ""):
"""Prints out a basic overview of a member's stats
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"""
user = user or ctx.message.author
bt = ctx.bot.db.load("overwatch", key=str(user.id), pluck="battletag")
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-07-25 02:02:50 +12:00
if hero == "":
# 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
log.info(data)
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:
# If there was a hero provided, search for a user's data on that hero
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
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)
if stats is None:
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)
return
2016-08-31 10:33:46 +12:00
# Same list comprehension as before
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))
try:
2017-03-08 11:35:30 +13:00
banner = await utils.create_banner(user, "Overwatch", output_data)
await ctx.send(file=discord.File(banner, filename="banner.png"))
except (FileNotFoundError, discord.Forbidden):
fmt = "\n".join("{}: {}".format(k, r) for k, r in output_data)
await ctx.send(
"Overwatch stats for {}: ```py\n{}```".format(user.name, fmt)
)
2017-07-19 10:08:09 +12:00
@ow.command(name="add")
@utils.can_run(send_messages=True)
2016-07-14 15:15:55 +12:00
async def add(self, ctx, bt: str):
"""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
# 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("#", "-")
key = str(ctx.message.author.id)
2016-08-31 10:33:46 +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)
if data is None:
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
# Now just save the battletag
entry = {"member_id": key, "battletag": bt}
2017-06-07 20:30:19 +12:00
await ctx.bot.db.save("overwatch", entry)
await ctx.send(
"I have just saved your battletag {}".format(ctx.message.author.mention)
)
@ow.command(name="delete", aliases=["remove"])
@utils.can_run(send_messages=True)
async def delete(self, ctx):
"""Removes your battletag from the records
EXAMPLE: !ow delete
RESULT: Your battletag is no longer saved"""
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
def setup(bot):
bot.add_cog(Overwatch(bot))