🏗 - Moved Riot's API interaction from main.py to riot_api.py

This commit is contained in:
Florian SYLVAIN
2021-03-06 18:08:18 +01:00
parent dce69e12cd
commit ed9f389bce
2 changed files with 18 additions and 12 deletions
+10 -7
View File
@@ -2,15 +2,18 @@ import asyncio
import socket import socket
import re import re
import discord import discord
from private import *
from osu_api import *
from discord.ext import tasks, commands from discord.ext import tasks, commands
from discord.ext.commands.cooldowns import BucketType from discord.ext.commands.cooldowns import BucketType
from riot_api import rank_track, WATCHER, REGION
from riotwatcher import ApiError from private import *
from osu_api import *
from riot_api import rank_track, what_player
from emoji import demojize from emoji import demojize
from twitch import TwitchClient from twitch import TwitchClient
bot = commands.Bot(command_prefix='$') bot = commands.Bot(command_prefix='$')
twitch_client = TwitchClient(client_id=ID_TWITCH, oauth_token=TOKEN_TWITCH) twitch_client = TwitchClient(client_id=ID_TWITCH, oauth_token=TOKEN_TWITCH)
chats = dict() chats = dict()
@@ -124,8 +127,8 @@ async def chat_stop(ctx):
@commands.cooldown(1, 2, commands.BucketType.user) @commands.cooldown(1, 2, commands.BucketType.user)
async def lol_rank(ctx, arg, argf=None): async def lol_rank(ctx, arg, argf=None):
if argf is None: if argf is None:
try: player = what_player(arg)
player = WATCHER.summoner.by_name(REGION, arg) if player != 0:
ranks = rank_track(player) ranks = rank_track(player)
embed = discord.Embed(title=arg, url='https://bit.ly/3biTekM') embed = discord.Embed(title=arg, url='https://bit.ly/3biTekM')
embed.set_thumbnail(url='http://ddragon.leagueoflegends.com/cdn/11.2.1/img/profileicon/' + str( embed.set_thumbnail(url='http://ddragon.leagueoflegends.com/cdn/11.2.1/img/profileicon/' + str(
@@ -140,7 +143,7 @@ async def lol_rank(ctx, arg, argf=None):
else: else:
embed.description = ranks embed.description = ranks
await ctx.send(embed=embed) await ctx.send(embed=embed)
except ApiError: else:
await ctx.send('The username you entered is unknown.') await ctx.send('The username you entered is unknown.')
else: else:
await ctx.send('If you are trying to use a username with spaces, please surround it with quotes.') await ctx.send('If you are trying to use a username with spaces, please surround it with quotes.')
+8 -5
View File
@@ -1,16 +1,19 @@
from riotwatcher import LolWatcher from riotwatcher import LolWatcher, ApiError
from private import TOKEN_RIOT from private import TOKEN_RIOT
WATCHER = LolWatcher(TOKEN_RIOT) WATCHER = LolWatcher(TOKEN_RIOT)
REGION = 'EUW1' REGION = 'EUW1'
def rank_track(player): def what_player(name):
'''function that communicate with Riot API''' try:
ranked_stats = WATCHER.league.by_summoner(REGION, player['id']) return WATCHER.summoner.by_name(REGION, name)
except ApiError:
return 0
def rank_track(player):
ranked_stats = WATCHER.league.by_summoner(REGION, player['id'])
if not ranked_stats: if not ranked_stats:
return 'Unranked cette saison.' return 'Unranked cette saison.'
liste = [] liste = []
for infos in ranked_stats: for infos in ranked_stats:
if infos['queueType'] == 'RANKED_SOLO_5x5': if infos['queueType'] == 'RANKED_SOLO_5x5':