diff --git a/APICommands/ChampionMasteryCommand.py b/APICommands/ChampionMasteryCommand.py index 4558a3a..f0b3b28 100644 --- a/APICommands/ChampionMasteryCommand.py +++ b/APICommands/ChampionMasteryCommand.py @@ -9,10 +9,10 @@ import APICommands.Command class ChampionMastery(APICommands.Command.Command, ABC): keywords = ["cm", "CM", "Championmastery", "championmastery"] - def __init__(self, pref, api: riotwatcher.LolWatcher, additionalKeywords: list): + def __init__(self, pref, api: riotwatcher.LolWatcher, region: str, additionalKeywords: list): if additionalKeywords is None: additionalKeywords = [] - super().__init__(pref, api, additionalKeywords) + super().__init__(pref, api, region, additionalKeywords) async def execute(self, message: discord.Message): pass diff --git a/APICommands/Command.py b/APICommands/Command.py index fe6e90d..3e20182 100644 --- a/APICommands/Command.py +++ b/APICommands/Command.py @@ -2,6 +2,7 @@ from abc import abstractmethod, ABCMeta from datetime import datetime import discord +import requests import riotwatcher @@ -11,12 +12,14 @@ class Command: pref = "" api: riotwatcher.LolWatcher commandName = "" + region = "" - def __init__(self, pref, api: riotwatcher.LolWatcher, additionalKeywords: list): + def __init__(self, pref, api: riotwatcher.LolWatcher, region: str, additionalKeywords: list): for i in additionalKeywords: self.keywords.append(i) self.pref = pref self.api = api + self.region = region @abstractmethod async def execute(self, message: discord.Message): @@ -43,3 +46,23 @@ class Command: print(logMSG) with open("requests.log", "a") as f: f.write(logMSG) + + def getSummonerNameFromMessage(self, message: discord.Message, argumentstart=1): + ret = "" + inp = message.content.split(" ") + if len(inp) > argumentstart + 1: + for i in inp[argumentstart:]: + ret += " " + i + + ret = ret[1:] + else: + ret = inp[argumentstart] + return ret + + async def checkSumname(self, sumname, message: discord.Message): + try: + var = self.api.summoner.by_name(self.region, sumname)["id"] + return True + except requests.exceptions.HTTPError: + await message.channel.send("No matching player found with name **" + sumname + "**") + return False diff --git a/APICommands/F2PCommand.py b/APICommands/F2PCommand.py index 5f08606..532f17c 100644 --- a/APICommands/F2PCommand.py +++ b/APICommands/F2PCommand.py @@ -9,10 +9,10 @@ import APICommands.Command class Free2Play(APICommands.Command.Command, ABC): keywords = ["f2p", "rotation", "F2P", "ROTATION"] - def __init__(self, pref, api: riotwatcher.LolWatcher, additionalKeywords=None): + def __init__(self, pref, api: riotwatcher.LolWatcher, region: str, additionalKeywords=None): if additionalKeywords is None: additionalKeywords = [] - super().__init__(pref, api, additionalKeywords) + super().__init__(pref, api, region, additionalKeywords) async def execute(self, message: discord.Message): pass diff --git a/APICommands/HighestMasteryCommand.py b/APICommands/HighestMasteryCommand.py index d30203b..9e12856 100644 --- a/APICommands/HighestMasteryCommand.py +++ b/APICommands/HighestMasteryCommand.py @@ -9,10 +9,10 @@ import APICommands.Command class HighestMastery(APICommands.Command.Command, ABC): keywords = ["highestmastery", "highestMastery", "HM", "hm", "Hm", "HighestMastery"] - def __init__(self, pref, api: riotwatcher.LolWatcher, additionalKeywords: list): + def __init__(self, pref, api: riotwatcher.LolWatcher, region: str, additionalKeywords: list): if additionalKeywords is None: additionalKeywords = [] - super().__init__(pref, api, additionalKeywords) + super().__init__(pref, api, region, additionalKeywords) async def execute(self, message: discord.Message): pass diff --git a/APICommands/PrefixCommand.py b/APICommands/PrefixCommand.py index c71c990..d0b3d9b 100644 --- a/APICommands/PrefixCommand.py +++ b/APICommands/PrefixCommand.py @@ -10,10 +10,10 @@ import APICommands.Command class Prefix(APICommands.Command.Command, ABC): keywords = ["prefix"] - def __init__(self, pref, api: riotwatcher.LolWatcher, additionalKeywords=None): + def __init__(self, pref, api: riotwatcher.LolWatcher, region: str, additionalKeywords=None): if additionalKeywords is None: additionalKeywords = [] - super().__init__(pref, api, additionalKeywords) + super().__init__(pref, api, region, additionalKeywords) self.commandName = "Prefix change" async def execute(self, message: discord.Message): @@ -30,7 +30,8 @@ class Prefix(APICommands.Command.Command, ABC): "Your current prefix is: " + self.pref + ". To change it use " + self.pref + "prefix [new Prefix]") async def usage(self, message: discord.Message): - await message.channel.send("Wrong usage of prefix command! Use " + self.pref + "prefix [new Prefix (optional)]") + await message.channel.send("Wrong usage of" + self.commandName + "! Use " + self.pref + "prefix [new Prefix (" + "optional)]") async def changePrefix(self, message: discord.Message): try: diff --git a/APICommands/SummonerLevelCommand.py b/APICommands/SummonerLevelCommand.py index be083d8..c68ece2 100644 --- a/APICommands/SummonerLevelCommand.py +++ b/APICommands/SummonerLevelCommand.py @@ -9,16 +9,31 @@ import APICommands.Command class SummonerLevel(APICommands.Command.Command, ABC): keywords = ["level", "Level", "lvl"] - def __init__(self, pref, api: riotwatcher.LolWatcher, additionalKeywords: list): + def __init__(self, pref, api: riotwatcher.LolWatcher, region: str, additionalKeywords: list): if additionalKeywords is None: additionalKeywords = [] - super().__init__(pref, api, additionalKeywords) + super().__init__(pref, api, region, additionalKeywords) async def execute(self, message: discord.Message): - pass + sumname = "" + try: + sumname = self.getSummonerNameFromMessage(message) + except: + await self.usage(message) + if sumname != "": + level = await self.requestLevel(sumname, message) + if level is not None: + await message.channel.send("Der Spieler " + sumname + " hat das Level " + str(level) + ".") 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 + "! Use " + self.pref + "level [Summoner]") + + async def requestLevel(self, sumname: str, message: discord.Message): + if not await self.checkSumname(sumname, message): + return + response = self.api.summoner.by_name(self.region, sumname) + return response["summonerLevel"] + diff --git a/APICommands/SummonerRankCommand.py b/APICommands/SummonerRankCommand.py index e05cd0e..21c86f6 100644 --- a/APICommands/SummonerRankCommand.py +++ b/APICommands/SummonerRankCommand.py @@ -9,10 +9,10 @@ import APICommands.Command class SummonerRank(APICommands.Command.Command, ABC): keywords = ["rank", "Rank", "RANK"] - def __init__(self, pref, api: riotwatcher.LolWatcher, additionalKeywords=None): + def __init__(self, pref, api: riotwatcher.LolWatcher, region: str, additionalKeywords=None): if additionalKeywords is None: additionalKeywords = [] - super().__init__(pref, api, additionalKeywords) + super().__init__(pref, api, region, additionalKeywords) async def execute(self, message: discord.Message): pass diff --git a/iLeague.py b/iLeague.py index b8e74a3..4f41032 100644 --- a/iLeague.py +++ b/iLeague.py @@ -26,13 +26,14 @@ class MyClient(discord.Client): def initAPI(self, APIKey, region="EUW1"): self.api = LolWatcher(APIKey) self.region = region + self.initCommands() def initCommands(self): self.commands = [] - self.commands.append(ChampionMasteryCommand.ChampionMastery(self.api, self.pref, [])) - self.commands.append(HighestMasteryCommand.HighestMastery(self.api, self.pref, [])) - self.commands.append(SummonerLevelCommand.SummonerLevel(self.api, self.pref, [])) - self.commands.append(PrefixCommand.Prefix(self.api, self.pref, [])) + self.commands.append(ChampionMasteryCommand.ChampionMastery(self.pref, self.api, self.region, [])) + self.commands.append(HighestMasteryCommand.HighestMastery(self.pref, self.api, self.region, [])) + self.commands.append(SummonerLevelCommand.SummonerLevel(self.pref, self.api, self.region, [])) + self.commands.append(PrefixCommand.Prefix(self.pref, self.api, self.region, [])) def load(self): # Loads the prefix file if accessable try: @@ -55,23 +56,23 @@ class MyClient(discord.Client): message.channel.id == 843900656108437504 or message.channel.id == 673681791932170240): # Only allows channels bot testing and leaguebot await message.channel.send("Bitte #league-bot verwenden.") return - if message.content == (self.pref + "prefix"): - await message.channel.send( - "Your current prefix is: " + self.pref + ". To change it use " + self.pref + "prefix [new Prefix]") - elif self.getContentFromMessageWithPrefixCommand(message, ["prefix"]): - self.log("Prefix change", message) - await self.changePrefix(message) - # HUBA + for command in self.commands: + if command.isCalled(message): + command.log(message) + await command.execute(message) + + + + # HUBA if self.getContentFromMessageWithPrefixCommand(message, ["hubaa"]): self.log("Huawa", message) await message.channel.send( "Julian Huber (16) ist ein Kinderschänder, welcher in Wahrheit schwul ist und seine sexuelle " "Orientierung hinter einer Beziehung mit einem weiblichen Kind versteckt.") - # LEVEL + # LEVEL elif self.getContentFromMessageWithPrefixCommand(message, ["level", "Level", "lvl"]): - self.log("Summoner level", message) await self.requestLevel(message) # RANK @@ -105,19 +106,6 @@ class MyClient(discord.Client): await message.channel.send( "Something went wrong while changing the prefix. To change it use " + self.pref + "prefix [new Prefix]") - async def requestLevel(self, message: discord.Message): - sumname = "" - try: - sumname = self.getSummonerNameFromMessage(message) - except: - pass - if sumname != "": - if not await self.checkSumname(sumname, message): - return - response = self.api.summoner.by_name(self.region, sumname) - level = response["summonerLevel"] - await message.channel.send("Der Spieler " + sumname + " hat das Level " + str(level) + ".") - async def requestRank(self, message: discord.Message): sumname = "" try: @@ -240,17 +228,7 @@ class MyClient(discord.Client): if len(output.split("\n")) <= 2: await message.channel.send("ㅤ\t- **Keine**") - def getSummonerNameFromMessage(self, message, argumentstart=1): - ret = "" - inp = message.content.split(" ") - if len(inp) > argumentstart + 1: - for i in inp[argumentstart:]: - ret += " " + i - ret = ret[1:] - else: - ret = inp[argumentstart] - return ret def getChampionMasteryList(self, sumname, listlen): output = ["Der Spieler " + sumname + " hat den höchsten Mastery auf diesen " + str( @@ -277,14 +255,6 @@ class MyClient(discord.Client): def getChampionsJSON(self): return requests.get("http://ddragon.leagueoflegends.com/cdn/11.19.1/data/en_US/champion.json").text - async def checkSumname(self, sumname, message: discord.Message): - try: - self.api.summoner.by_name(self.region, sumname)["id"] - return True - except requests.exceptions.HTTPError: - await message.channel.send("No matching player found with name **" + sumname + "**") - return False - def truncate(f, n): '''Truncates/pads a float f to n decimal places without rounding'''