From 9f5f6f8feaf60f5bf1a2acad3df24be75c628352 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 28 Apr 2017 18:54:20 +0200 Subject: [PATCH] EVERYTHING works --- database.py | 31 ++++++++++++++++++++++++------- lol.py | 19 +++---------------- royalbot.py | 23 +++++++++++++---------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/database.py b/database.py index b539e667..a9864f15 100644 --- a/database.py +++ b/database.py @@ -1,5 +1,6 @@ import datetime import sqlalchemy.exc +import discord from sqlalchemy import create_engine, Column, Integer, String, Boolean, DateTime, ForeignKey from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.ext.declarative import declarative_base @@ -44,7 +45,7 @@ class LoL(Base): __tablename__ = "lol" id = Column(Integer, primary_key=True) - parentid = Column(Integer, ForeignKey("account.id")) + parent_id = Column(Integer, ForeignKey("account.id")) last_updated = Column(DateTime) summoner_name = Column(String, nullable=False) @@ -60,6 +61,22 @@ class LoL(Base): return f"" + def generate_discord_embed(self): + embed = discord.Embed(type="rich") + # TODO: change the icon + embed.set_author(name="League of Legends", url="http://euw.leagueoflegends.com/", icon_url="https://cdn.discordapp.com/attachments/152150752523976704/307558194824216578/icon.png") + embed.add_field(name="Summoner", value=str(self.summoner_name)) + embed.add_field(name="Level", value=str(self.level)) + if self.soloq_tier is not None: + embed.add_field(name="Solo/duo SR", value=f"{lol.tiers[self.soloq_tier].capitalize()} {lol.divisions[self.soloq_division]}", inline=False) + embed.set_thumbnail(url=f"https://royal.steffo.me/loltiers/{lol.tiers[self.soloq_tier].lower()}_{lol.divisions[self.soloq_division].lower()}.png") + if self.flexq_tier is not None: + embed.add_field(name="Flex SR", value=f"{lol.tiers[self.flexq_tier].capitalize()} {lol.divisions[self.flexq_division]}", inline=False) + if self.ttq_tier is not None: + embed.add_field(name="Twisted Treeline", value=f"{lol.tiers[self.ttq_tier].capitalize()} {lol.divisions[self.ttq_division]}", inline=False) + embed.colour = discord.Colour(0x09AEBB) + return embed + Base.metadata.create_all(engine) @@ -106,20 +123,20 @@ async def update_lol(discord_id): soloq, flexq, ttq = await lol.get_rank_data("euw", lid) # Update the user data if soloq is not None: - account.soloq_tier = lol.tiers[soloq["tier"]] - account.soloq_division = lol.divisions[soloq["entries"][0]["division"]] + account.soloq_tier = lol.tiers.index(soloq["tier"]) + account.soloq_division = lol.divisions.index(soloq["entries"][0]["division"]) else: account.soloq_tier = None account.soloq_division = None if flexq is not None: - account.flexq_tier = lol.tiers[flexq["tier"]] - account.flexq_division = lol.divisions[flexq["entries"][0]["division"]] + account.flexq_tier = lol.tiers.index(flexq["tier"]) + account.flexq_division = lol.divisions.index(flexq["entries"][0]["division"]) else: account.flexq_tier = None account.flexq_division = None if ttq is not None: - account.ttq_tier = lol.tiers[ttq["tier"]] - account.ttq_division = lol.divisions[ttq["entries"][0]["division"]] + account.ttq_tier = lol.tiers.index(ttq["tier"]) + account.ttq_division = lol.divisions.index(ttq["entries"][0]["division"]) else: account.ttq_tier = None account.ttq_division = None diff --git a/lol.py b/lol.py index 61a61230..a826ce31 100644 --- a/lol.py +++ b/lol.py @@ -8,23 +8,10 @@ class LoLAPIError(Exception): pass -tiers = { - "BRONZE": 0, - "SILVER": 1, - "GOLD": 2, - "PLATINUM": 3, - "DIAMOND": 4, - "MASTER": 5, - "CHALLENGER": 6 -} +tiers = ["BRONZE", "SILVER", "GOLD", "PLATINUM", "DIAMOND", "MASTER", "CHALLENGER"] -divisions = { - "I": 0, - "II": 1, - "III": 2, - "IV": 3, - "V": 4 -} + +divisions = ["I", "II", "III", "IV", "V"] async def get_json(url, **kwargs): diff --git a/royalbot.py b/royalbot.py index 9e560db7..07dc034f 100644 --- a/royalbot.py +++ b/royalbot.py @@ -254,7 +254,7 @@ Sintassi: `{symbol}syncdiscord`""" # DISCORD ONLY! -async def synclol(bot, thing, arguments): +async def synclol(bot, thing: extradiscord.discord.Message, arguments): """Connetti il tuo account di LoL all'account Royal Games! Sintassi: `{symbol}synclol `""" @@ -271,20 +271,23 @@ Sintassi: `{symbol}synclol `""" if user is None: await answer(bot, thing, "⚠ Fai il login prima di sincronizzare l'account di LoL.") return - # Create a new LoL account and link it to the user - # TODO: IMPROVE THIS + # Check if there are other LoL account registered with the user + user = session.query(database.Account).filter_by(id=thing.author.id).join(database.LoL).all() + if len(user) > 0: + await answer(bot, thing, "⚠ Hai già un account connesso.\n_Se stai cercando di registrare uno smurf, chiedi a Steffo._") + # Get data about the user summoner_name = " ".join(arguments) data = await lol.get_summoner_data("euw", summoner_name=summoner_name) + # Create a new database entry for the account lolaccount = database.LoL(id=data["id"], summoner_name=summoner_name) - lolaccount.parentid = user.id + lolaccount.parent_id = thing.author.id session.add(lolaccount) + # Commit the changes to the database session.commit() - await database.update_lol(user.id) - - -async def update_stats(timeout): - await asyncio.sleep(timeout) - loop.create_task() + # Update the newly added user + await database.update_lol(thing.author.id) + # Send some info to Discord + await d.client.send_message(thing.channel, embed=lolaccount.generate_discord_embed()) if __name__ == "__main__": # Init universal bot commands