1
0
Fork 0
mirror of synced 2024-05-19 20:12:30 +12:00

Added overwatch lookup on users

This commit is contained in:
phxntxm 2016-07-13 22:15:55 -05:00
parent 946b61b926
commit aaa1b8e6ae

View file

@ -1,5 +1,6 @@
from .utils import config
from discord.ext import commands
import discord
import urllib.parse
import urllib.request
@ -15,14 +16,38 @@ class Overwatch:
self.bot = bot
@commands.group(no_pm=True, invote_without_command=True)
async def ow(self):
pass
async def ow(self, ctx, user: discord.Member=None):
if user is None:
user = ctx.message.author
cursor = config.getCursor()
cursor.execute('use {}'.format(config.db_default))
cursor.execute('select battletag from overwatch where id=%s', (user.id,))
result = cursor.fetchone()
config.closeConnection()
if result is None:
await self.bot.say("I do not have this user's battletag saved!")
return
bt = result['battletag']
await self.bot.say("Searching profile information....")
url = base_url + "{}/stats/general".format(bt)
result = urllib.request.urlopen(url)
data = json.loads(result.read().decode('utf-8'))
o_stats = data['overall_stats']
g_stats = data['game_stats']
fmt = "Wins: {}".format(o_stats['wins'])
fmt += "Losses: {}".format(o_stats['losses'])
fmt += "Kills: {}".format(g_stats['eliminations'])
fmt += "Deaths: {}".format(g_stats['deaths'])
fmt += "Kill/Death Ratio: {}".format(g_stats['kpd'])
d = divmod(g_stats['time_played'], 24)
fmt += "Time Played: {} days {} hours".format(int(d[0]), int(d[1]))
await self.bot.say("Overwatch stats for {}: ```py\n{}```".format(user.name, fmt))
@ow.command(pass_context=True, name="add")
async def add(self, ctx, username: str):
username = username.replace("#", "-")
async def add(self, ctx, bt: str):
bt = bt.replace("#", "-")
await self.bot.say("Looking up your profile information....")
url = base_url + "{}/stats/general".format(username)
url = base_url + "{}/stats/general".format(bt)
try:
urllib.request.urlopen(url)
except urllib.error.HTTPError:
@ -34,10 +59,10 @@ class Overwatch:
cursor.execute('select * from overwatch where id=%s', (ctx.message.author.id,))
result = cursor.fetchone()
if result:
cursor.execute('update overwatch set battletag=%s where id=%s', (username, ctx.message.author.id))
cursor.execute('update overwatch set battletag=%s where id=%s', (bt, ctx.message.author.id))
await self.bot.say("I have updated your saved battletag {}".format(ctx.message.author.mention))
else:
cursor.execute('insert into overwatch (id, battletag) values (%s, %s)', (ctx.message.author.id, username))
cursor.execute('insert into overwatch (id, battletag) values (%s, %s)', (ctx.message.author.id, bt))
await self.bot.say("I have just saved your battletag {}".format(ctx.message.author.mention))
config.closeConnection()