From 63f65b6745d8faac322438f4642e7cacf0a48b98 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Fri, 28 Aug 2020 09:40:38 +0100 Subject: [PATCH] Reformatted code. Put code into functions for increased modularity --- cogs/anime.py | 64 ++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/cogs/anime.py b/cogs/anime.py index 59783d50..264f39a7 100644 --- a/cogs/anime.py +++ b/cogs/anime.py @@ -43,6 +43,29 @@ class WaifuCommandNotFound(Exception): return f'{self.command} -> {self.message}' +async def get_waifu_api(self, ctx, url): + """Retreiving information about daily/random waifu's""" + + url = f"https://mywaifulist.moe/api/v1/meta/{url}" + + # Retrieve random or daily waifu from API + async with aiohttp.ClientSession() as session: + async with session.get(url, headers=self.headers) as resp: + + # Store waifu's in dict when request is successful, else send an error + if resp.status == 200: + waifu_dict = await resp.json() + waifu = waifu_dict["data"] + + # Send error if something went wrong internally/while grabbing data from API + else: + await self.bot.generate_embed(ctx, desc="**Something went wrong with MyWaifuList!**") + + await session.close() + + return waifu + + async def get_airing_api(self, ctx, url): """Retreiving information about the airing shows/waifus""" @@ -575,23 +598,7 @@ class Anime(Cog): async def daily_waifu(self, ctx): """Returns the Daily Waifu from MyWaifuList""" - url = "https://mywaifulist.moe/api/v1/meta/daily" - - # Retrieve a random waifu from the API - async with aiohttp.ClientSession() as session: - async with session.get(url, headers=self.headers) as resp: - # Store waifu's in dict when request is successful, else send an error - if resp.status == 200: - waifu_dict = await resp.json() - waifu = waifu_dict["data"] - - # Send error if something went wrong internally/while grabbing data from API - else: - await self.bot.generate_embed(ctx, desc="**Something went wrong with MyWaifuList!**") - - # Close session - await session.close() - + waifu = await get_waifu_api(self, ctx, "daily") await ctx.send(embed=waifu_embedder(self, waifu, "daily")) @waifu.command(name="random", aliases=["rnd"]) @@ -599,25 +606,14 @@ class Anime(Cog): async def random_waifu(self, ctx): """Returning a Random Waifu from MyWaifuList""" - # Set variables to retrieve data from the API - url = "https://mywaifulist.moe/api/v1/meta/random" - - # Retrieve a random waifu from the API - async with aiohttp.ClientSession() as session: - async with session.get(url, headers=self.headers) as resp: - # Store waifu's in dict when request is successful, else send an error - if resp.status == 200: - waifu_dict = await resp.json() - waifu = waifu_dict["data"] - - # Send error if something went wrong internally/while grabbing data from API - else: - await self.bot.generate_embed(ctx, desc="**Something went wrong with MyWaifuList!**") - - await session.close() - + waifu = await get_waifu_api(self, ctx, "random") await ctx.send(embed=waifu_embedder(self, waifu, "random")) + @group(name="profile", aliases=["user"], invoke_without_command=True, case_insensitive=True) + @bot_has_permissions(embed_links=True) + async def mwl_user_profile(self, ctx): + """Returning the MWL User Profile requested""" + def setup(bot): bot.add_cog(Anime(bot))