From 33ec7bbb3087ed1c5094142894ecb8098675e11d Mon Sep 17 00:00:00 2001 From: phxntxm Date: Wed, 13 Jul 2016 21:24:41 -0500 Subject: [PATCH] Added saving of battletags; prepping for overwatch support --- cogs/overwatch.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 cogs/overwatch.py diff --git a/cogs/overwatch.py b/cogs/overwatch.py new file mode 100644 index 0000000..ce6be9d --- /dev/null +++ b/cogs/overwatch.py @@ -0,0 +1,57 @@ +from .utils import config +from discord.ext import commands + +import urllib.parse +import urllib.request +import urllib.error +import json + +base_url = "https://owapi.net/api/v2/u/" + + +class Overwatch: + def __init__(self, bot): + self.bot = bot + + @commands.group(no_pm=True, invote_without_command=True) + async def ow(self): + pass + + @ow.command(pass_context=True, name="add") + async def add(self, ctx, username: str): + username = username.replace("#", "-") + url = base_url + "{}/stats/general".format(username) + try: + urllib.request.urlopen(url) + except urllib.error.HTTPError: + await self.bot.say("Profile does not exist! Battletags are picky, " + "format needs to be `user#xxxx`. Capitalization matters") + return + cursor = config.getCursor() + cursor.execute('use {}'.format(config.db_default)) + 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)) + 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)) + await self.bot.say("I have just saved your battletag {}".format(ctx.message.author.id)) + config.closeConnection() + + @ow.command(pass_context=True, name="delete", aliases=['remove']) + async def add(self, ctx): + cursor = config.getCursor() + cursor.execute('use {}'.format(config.db_default)) + cursor.execute('select * from overwatch where id=%s', (ctx.message.author.id,)) + result = cursor.fetchone() + if result: + cursor.execute('delete from overwatch where id=%s', (ctx.message.author.id,)) + await self.bot.say("I no longer have your battletag saved {}".format(ctx.message.author.mention)) + else: + await self.bot.say("I don't even have your battletag saved {}".format(ctx.message.author.mention)) + config.closeConnection() + + +def setup(bot): + bot.add_cog(Overwatch(bot))