From 169599dff551634164d6c73b291b3d5458aed1de Mon Sep 17 00:00:00 2001 From: s-prechtl Date: Thu, 17 Mar 2022 14:34:30 +0100 Subject: [PATCH] minor code refactoring --- APICommands/ChampionMasteryCommand.py | 32 ++++++++++++++++-- APICommands/Command.py | 20 +++++++++++ APICommands/HighestMasteryCommand.py | 4 +-- APICommands/SummonerLevelCommand.py | 3 +- APICommands/SummonerRankCommand.py | 3 +- iLeague.py | 48 --------------------------- 6 files changed, 56 insertions(+), 54 deletions(-) diff --git a/APICommands/ChampionMasteryCommand.py b/APICommands/ChampionMasteryCommand.py index f0b3b28..b596037 100644 --- a/APICommands/ChampionMasteryCommand.py +++ b/APICommands/ChampionMasteryCommand.py @@ -4,6 +4,7 @@ import discord import riotwatcher import APICommands.Command +from APICommands.Command import getSummonerNameFromMessage, getChampionsJSON, championIdToName class ChampionMastery(APICommands.Command.Command, ABC): @@ -15,10 +16,37 @@ class ChampionMastery(APICommands.Command.Command, ABC): super().__init__(pref, api, region, additionalKeywords) async def execute(self, message: discord.Message): - pass + sumname = "" + try: + sumname = getSummonerNameFromMessage(message, 2) + except: + await self.usage(message) + + if sumname != "": + if not await self.checkSumname(sumname, message): + return + + response = self.api.champion_mastery.by_summoner(self.region, + self.api.summoner.by_name(self.region, + sumname)["id"]) + + championsJSON = getChampionsJSON() + for i in response: + champname = championIdToName(i["championId"], championsJSON) + if champname == message.content.split(" ")[1]: + mpoints = i["championPoints"] + mastery = i["championLevel"] + out = "**" + sumname + "** --> **" + champname + "**" + " Points: " + str( + mpoints) + " Level: " + str( + mastery) + "\n" + + await message.channel.send(out) + return + await message.channel.send("No matching champion was found.") async def info(self, message: discord.Message): pass async def usage(self, message: discord.Message): - pass + await message.channel.send("Wrong usage of " + self.commandName + "!\nUsage: " + self.pref + "cm [champion] [" + "Summoner]") diff --git a/APICommands/Command.py b/APICommands/Command.py index 69f97b0..3d6a2fd 100644 --- a/APICommands/Command.py +++ b/APICommands/Command.py @@ -1,3 +1,4 @@ +import json from abc import abstractmethod, ABCMeta from datetime import datetime @@ -6,6 +7,25 @@ import requests import riotwatcher +def championIdToName(id, championsText): + champions = json.loads(championsText)['data'] + + for j in dict(champions): + if id == int(champions[j]["key"]): + return j + + +def getChampionsJSON(): + return requests.get("http://ddragon.leagueoflegends.com/cdn/11.19.1/data/en_US/champion.json").text + + +def intTryParse(value): + try: + return int(value), True + except ValueError: + return value, False + + def getSummonerNameFromMessage(message: discord.Message, argumentstart=1): ret = "" inp = message.content.split(" ") diff --git a/APICommands/HighestMasteryCommand.py b/APICommands/HighestMasteryCommand.py index f4fa538..b488c1a 100644 --- a/APICommands/HighestMasteryCommand.py +++ b/APICommands/HighestMasteryCommand.py @@ -4,7 +4,7 @@ import discord import riotwatcher import APICommands.Command -from iLeague import intTryParse, championIdToName, getChampionsJSON +from APICommands.Command import intTryParse, championIdToName, getChampionsJSON, getSummonerNameFromMessage class HighestMastery(APICommands.Command.Command, ABC): @@ -20,7 +20,7 @@ class HighestMastery(APICommands.Command.Command, ABC): firstIsInt = intTryParse(message.content.split(" ")[1])[1] try: - sumname = APICommands.Command.getSummonerNameFromMessage(message, 2) + sumname = getSummonerNameFromMessage(message, 2) except: await self.usage(message) if sumname != "": diff --git a/APICommands/SummonerLevelCommand.py b/APICommands/SummonerLevelCommand.py index 2c26cd4..46d10c6 100644 --- a/APICommands/SummonerLevelCommand.py +++ b/APICommands/SummonerLevelCommand.py @@ -4,6 +4,7 @@ import discord import riotwatcher import APICommands.Command +from APICommands.Command import getSummonerNameFromMessage class SummonerLevel(APICommands.Command.Command, ABC): @@ -17,7 +18,7 @@ class SummonerLevel(APICommands.Command.Command, ABC): async def execute(self, message: discord.Message): sumname = "" try: - sumname = APICommands.Command.getSummonerNameFromMessage(message) + sumname = getSummonerNameFromMessage(message) except: await self.usage(message) if sumname != "": diff --git a/APICommands/SummonerRankCommand.py b/APICommands/SummonerRankCommand.py index 7d12697..412f67e 100644 --- a/APICommands/SummonerRankCommand.py +++ b/APICommands/SummonerRankCommand.py @@ -4,6 +4,7 @@ import discord import riotwatcher import APICommands.Command +from APICommands.Command import getSummonerNameFromMessage def truncate(f, n): @@ -26,7 +27,7 @@ class SummonerRank(APICommands.Command.Command, ABC): async def execute(self, message: discord.Message): sumname = "" try: - sumname = APICommands.Command.getSummonerNameFromMessage(message) + sumname = getSummonerNameFromMessage(message) except: await self.usage(message) diff --git a/iLeague.py b/iLeague.py index d2b8c37..5e96881 100644 --- a/iLeague.py +++ b/iLeague.py @@ -9,12 +9,7 @@ from riotwatcher import LolWatcher from APICommands import ChampionMasteryCommand, HighestMasteryCommand, SummonerLevelCommand, PrefixCommand, SummonerRankCommand -def championIdToName(id, championsText): - champions = json.loads(championsText)['data'] - for j in dict(champions): - if id == int(champions[j]["key"]): - return j class MyClient(discord.Client): @@ -75,47 +70,11 @@ class MyClient(discord.Client): if True: return - elif self.getContentFromMessageWithPrefixCommand(message, ["cm", "CM", "Championmastery", - "championmastery"]): # get Mastery from Champion - self.log("Summoner champion mastery", message) - await self.requestChampionMastery(message) - # FREE CHAMPS elif self.getContentFromMessageWithPrefixCommand(message, ["f2p", "rotation", "F2P", "ROTATION"]): self.log("F2P rotation", message) await self.requestFreeChampRot(message) - - async def requestChampionMastery(self, message: discord.Message): - err = "Something went wrong.\nUsage: " + self.pref + "cm [Championname] [Summonername]" - sumname = "" - try: - sumname = self.getSummonerNameFromMessage(message, 2) - except Exception as e: - await message.channel.send(err) - - if sumname != "": - if not await self.checkSumname(sumname, message): - return - - response = self.api.champion_mastery.by_summoner(self.region, - self.api.summoner.by_name(self.region, - sumname)["id"]) - - championsJSON = getChampionsJSON() - for i in response: - champname = championIdToName(i["championId"], championsJSON) - if champname == message.content.split(" ")[1]: - mpoints = i["championPoints"] - mastery = i["championLevel"] - out = "**" + sumname + "** --> **" + champname + "**" + " Points: " + str( - mpoints) + " Level: " + str( - mastery) + "\n" - - await message.channel.send(out) - return - await message.channel.send("No matching champion was found.") - async def requestFreeChampRot(self, message: discord.Message): err = "Something went wrong.\nUsage: " + self.pref + "f2p [Summonername]" sumname = "" @@ -150,14 +109,7 @@ class MyClient(discord.Client): if len(output.split("\n")) <= 2: await message.channel.send("ㅤ\t- **Keine**") -def getChampionsJSON(): - return requests.get("http://ddragon.leagueoflegends.com/cdn/11.19.1/data/en_US/champion.json").text -def intTryParse(value): - try: - return int(value), True - except ValueError: - return value, False if __name__ == '__main__':