From 46227e1fa61c0864a729396665a6fbcc0a42e446 Mon Sep 17 00:00:00 2001 From: AnInternetTroll Date: Wed, 24 Feb 2021 23:08:47 +0100 Subject: Make !verified asynchronous --- cogs/src.py | 70 +++++++++++++++++++++++++++++------------------------------ cogs/utils.py | 4 +--- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/cogs/src.py b/cogs/src.py index 72ec722..b96d36a 100755 --- a/cogs/src.py +++ b/cogs/src.py @@ -331,44 +331,44 @@ async def verifyNew(self, apiKey=None, userID=None): else: await member.remove_roles(RunneRole) + async def verifiedCount(self, ctx, modName): - head = { - "Accept": "application/json", - "User-Agent": "mcbeDiscordBot/1.0" - } - - r = requests.get( - f"https://www.speedrun.com/api/v1/users/{modName}", - headers=head - ) - if r.status_code != 200: - await ctx.send(f"Could not find user {modName}") - return - try: - modID = r.json()['data']['id'] - except: - await ctx.send("Something went wrong") - return - - hold = [] - temp = requests.get( - f"https://www.speedrun.com/api/v1/runs?examiner={modID}&max=200", - headers=head) - while True: - if temp.status_code != 200: + head = {"Accept": "application/json", "User-Agent": "mcbeDiscordBot/1.0"} + async with self.bot.session.get( + f"https://www.speedrun.com/api/v1/users/{modName}", headers=head + ) as r: + if r.status != 200: + await ctx.send(f"Could not find user {modName}") + return + try: + modID = await r.json() + modID = modID["data"]["id"] + except: await ctx.send("Something went wrong") return - temp_json = temp.json() - hold.extend(temp_json['data']) - if 'pagination' not in temp_json or temp_json['pagination']['size'] < 200: - break - temp = requests.get( - {item['rel']: item['uri'] - for item in temp_json['pagination']['links']}['next'], - headers=head) - + + hold = [] + async with self.bot.session.get( + f"https://www.speedrun.com/api/v1/runs?examiner={modID}&max=200", headers=head + ) as temp: + while True: + if temp.status != 200: + await ctx.send("Something went wrong") + return + temp_json = await temp.json() + hold.extend(temp_json["data"]) + if "pagination" not in temp_json or temp_json["pagination"]["size"] < 200: + break + temp = await self.bot.session.get( + {item["rel"]: item["uri"] for item in temp_json["pagination"]["links"]}[ + "next" + ], + headers=head, + ) + await ctx.send(f"{modName} has verified {len(hold)} runs") + class Src(commands.Cog): def __init__(self, bot): self.bot = bot @@ -444,8 +444,8 @@ class Src(commands.Cog): if modName is None: await ctx.send("Please supply a moderator to view stats of") return - await verifiedCount(self, ctx, modName) - + await verifiedCount(self, ctx, modName) + @tasks.loop(minutes=10.0) async def checker(self): data = json.loads(Path("./api_keys.json").read_text()) diff --git a/cogs/utils.py b/cogs/utils.py index b4f819f..1ff6820 100755 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -398,9 +398,7 @@ class Utils(commands.Cog): .decode("utf-8") ) if "women" in text.lower() or "woman" in text.lower(): - await message.channel.send( - "<:shut:808844274933694474>" - ) + await message.channel.send("<:shut:808844274933694474>") for word in badWords: if word in message.content.lower().replace(" ", ""): -- cgit v1.2.3