async def rank_solo(self, context, *usernameTokens): username, urlFriendlyUsername = parse_username_tokens(usernameTokens) url = f'https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/{ urlFriendlyUsername }' print(f'Using {url} to get summoner ID') req = requests.get(url, headers=get_headers()) try: response = req.json() userId = response['id'] try: print(f'Sending {username} their rank results.') await context.send( embed=self.get_ranked_results(username, userId, 'solo')) return except Exception as e: print(f'Error retreiving {username} rank results. Err = { e }') await context.send( f'{username} does not have Solo/Duo Rank games.') return except KeyError as exception: if r.status_code == 403: print('Riot API has expired.') await context.send(f'Error connecting to riot servers.') return elif r.status_code == 404: print(f'{username} is not a real user.') await context.send(f'{username} is not registered with Riot.') return
def get_account_info_using_summoner_name(urlFriendlyUsername): try: r = requests.get( SUMMONER_INFO_URL.format(urlFriendlyUsername=urlFriendlyUsername), headers=get_headers()) r.raise_for_status() json = r.json() return json['accountId'] except HTTPError as exception: print( "Error connecting to riot API while trying to fetch account info")
def get_match_history(username, urlFriendlyUsername): try: accountId = get_account_info_using_summoner_name(urlFriendlyUsername) r = requests.get(MATCH_HISTORY_URL.format(accountId=accountId), headers=get_headers()) return r.json()['matches'] except HTTPError as e: if r.status_code == 403: print('Riot API has expired.') return elif r.status_code == 404: print(f'{username} is not a real user.') return
def get_ranked_results(self, username, userId, request): REQUEST_maps_QUEUE_TYPE = { 'solo': 'RANKED_SOLO_5x5', 'flex': 'RANKED_FLEX_SR' } r = requests.get( f'https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/{userId}', headers=get_headers()) foundRankRecord = None for rankedQueueInfo in r.json(): if rankedQueueInfo['queueType'] == REQUEST_maps_QUEUE_TYPE[ request]: foundRankRecord = rankedQueueInfo if foundRankRecord == None: raise Exception('No ranked data found for solo queue') hotstreak = '' if foundRankRecord['hotStreak'] == True: hotstreak = 'You are on fire right now! Keep the win streak up.' current_rank_info = (foundRankRecord["tier"].lower(), self.ROMAN_maps_DECIMAL[foundRankRecord["rank"]]) current_rank_icon_url = self.RANK_ICON_URL_TEMPLATE % current_rank_info print(current_rank_icon_url) soloRankRecordEmbed = Embed( color=self.EMBED_COLOR, title=f'Solo/Duo Rank Queue Stats For { username }') soloRankRecordEmbed.set_thumbnail(url=current_rank_icon_url) soloRankRecordEmbed.add_field( name='Current Rank', value= f'{ foundRankRecord["tier"] } { self.ROMAN_maps_DECIMAL[foundRankRecord["rank"]] } {foundRankRecord["leaguePoints"]}LP', inline=False) soloRankRecordEmbed.add_field(name='Total Wins', value=f'{ foundRankRecord["wins"] }', inline=True) soloRankRecordEmbed.add_field(name='Total Losses', value=f'{ foundRankRecord["losses"] }', inline=True) soloRankRecordEmbed.add_field( name='Win/Loss Ratio', value= f'{ float(int(foundRankRecord["wins"]) / (int(foundRankRecord["wins"]) + int(foundRankRecord["losses"]))) }', inline=True) return soloRankRecordEmbed
async def get_last_match_stats(self, context, *usernameTokens): username, urlFriendlyUsername = parse_username_tokens(usernameTokens) summonerId = '' url = f'https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/{ urlFriendlyUsername }' print(f'Using {url} to get summoner ID') r = requests.get(url, headers=get_headers()) try: response = r.json() summonerId = response['id'] except: print('Error fetching username') gameId = get_most_recent_game_id(username, urlFriendlyUsername) riotMatchData = get_match_data_by_game_id(gameId) participantId = None summonerName = None for participantData in riotMatchData['participantIdentities']: if participantData['player']['summonerId'] == summonerId: participantId = participantData['participantId'] summonerName = participantData['player']['summonerName'] participantData = riotMatchData['participants'][participantId - 1] mapSide = ('Blue', 'Red')[participantData['teamId'] == 100] participantStats = participantData['stats'] kills = int(participantStats["kills"]) deaths = int(participantStats["deaths"]) assists = int(participantStats["assists"]) matchMessage = Embed( color=(BLUE_COLOR, RED_COLOR)[participantData['teamId'] == 100], title=f'Last Match For { summonerName }') matchMessage.set_thumbnail( url= f'http://ddragon.leagueoflegends.com/cdn/10.3.1/img/champion/{ self.championData[participantData["championId"]] ["name"] }.png' ) matchMessage.add_field(name='Map Side', value=mapSide, inline=False) matchMessage.add_field(name='Score', value=f'{kills}/{deaths}/{assists}', inline=True) matchMessage.add_field(name='KDA', value=float(kills / deaths)) matchMessage.add_field( name='Items', value= '![](http://ddragon.leagueoflegends.com/cdn/10.11.1/img/item/1402.png)' ) matchMessage.set_image( url= 'http://ddragon.leagueoflegends.com/cdn/10.11.1/img/item/1402.png') matchMessage.set_image( url= 'http://ddragon.leagueoflegends.com/cdn/10.11.1/img/item/3020.png') matchMessage.set_image( url= 'http://ddragon.leagueoflegends.com/cdn/10.11.1/img/item/3916.png') matchMessage.set_image( url= 'http://ddragon.leagueoflegends.com/cdn/10.11.1/img/item/3101.png') matchMessage.set_image( url= 'http://ddragon.leagueoflegends.com/cdn/10.11.1/img/item/3340.png') # matchMessage.set_image(url = 'http://ddragon.leagueoflegends.com/cdn/10.11.1/img/item/1001.png') print(f'Sending {username} their stats for the last game they played.') await context.send(embed=matchMessage)
def get_match_data_by_game_id(gameId): url = f'https://na1.api.riotgames.com/lol/match/v4/matches/{ gameId }' r = requests.get(url, headers=get_headers()) return r.json()