From e36d60ef2452fef167d79f678c81cb37e2e6a2ab Mon Sep 17 00:00:00 2001 From: Adriene Hutchins Date: Tue, 7 Apr 2020 19:56:01 -0400 Subject: [PATCH] Made logging not block. --- extensions/betterlogging.py | 6 ++-- extensions/botlist.py | 2 +- extensions/search.py | 7 ++-- extensions/utils/logging.py | 71 +++++++++++++++++++++---------------- old_search.py | 4 +-- 5 files changed, 48 insertions(+), 42 deletions(-) diff --git a/extensions/betterlogging.py b/extensions/betterlogging.py index e6a4eaa..062f6b6 100644 --- a/extensions/betterlogging.py +++ b/extensions/betterlogging.py @@ -34,7 +34,7 @@ class BetterLogging(commands.Cog): msg += f"_Member Count:_ {guild.member_count}\n\n" msg += f"_Guild Count now {len(self.bot.guilds)}._" - await self.info(content=msg, name="Guild Join") + self.info(content=msg, name="Guild Join") @commands.Cog.listener() async def on_guild_remove(self, guild): @@ -45,7 +45,7 @@ class BetterLogging(commands.Cog): msg += f"_Member Count:_ {guild.member_count}\n\n" msg += f"_Guild Count now {len(self.bot.guilds)}._" - await self.info(content=msg, name="Guild Leave") + self.info(content=msg, name="Guild Leave") @commands.Cog.listener() async def on_command(self, ctx): @@ -56,7 +56,7 @@ class BetterLogging(commands.Cog): f"**{ctx.author}** in _\"{ctx.guild if ctx.guild else 'DMs'}\"_." ) - await self.info(content=msg, name="Command Call") + self.info(content=msg, name="Command Call") async def cog_check(self, ctx): return commands.is_owner()(ctx.command) diff --git a/extensions/botlist.py b/extensions/botlist.py index 84a5894..09d9660 100644 --- a/extensions/botlist.py +++ b/extensions/botlist.py @@ -116,7 +116,7 @@ class BotList(commands.Cog, name='Bot List'): responses['dad'] = resp.status log_msg = f"**Botlists updated!**\n\n```{responses}```" - await self.debug(content=log_msg, name='List Update') + self.debug(content=log_msg, name='List Update') # Finalization return responses diff --git a/extensions/search.py b/extensions/search.py index 5384413..79d6349 100644 --- a/extensions/search.py +++ b/extensions/search.py @@ -102,7 +102,7 @@ class Search(commands.Cog, name="Basic"): f"&url={quote_plus(search_url)}" ) - await self.debug(search_url, name="_search_logic") + self.debug(search_url, name="_search_logic") # Searching headers = { @@ -113,7 +113,6 @@ class Search(commands.Cog, name="Basic"): } async with self.request.get(search_url, headers=headers) as resp: to_parse = await resp.json() - print(to_parse) # Sends results return to_parse['data']['result']['items'] @@ -169,8 +168,6 @@ class Search(commands.Cog, name="Basic"): f"{other_msg}\n\n_Powered by Qwant._" ) - print(msg) - msg = re.sub( r'(https?://(?:www\.)?[-a-zA-Z0-9@:%._+~#=]+\.' r'[a-zA-Z0-9()]+\b[-a-zA-Z0-9()@:%_+.~#?&/=]*)', @@ -180,7 +177,7 @@ class Search(commands.Cog, name="Basic"): # Sends message - await self.info( + self.info( f"**New Search** - `{ctx.author}` in `{ctx.guild}`\n\n{msg}", name="New Search" ) diff --git a/extensions/utils/logging.py b/extensions/utils/logging.py index ad06bc1..daea94f 100644 --- a/extensions/utils/logging.py +++ b/extensions/utils/logging.py @@ -8,6 +8,7 @@ import traceback from typing import Optional +import asyncio import discord from discord.ext.commands import Context @@ -96,37 +97,42 @@ class Logging(): # Provides completed embed return error_embed - async def info(self, content: str, + def info(self, content: str, embed: Optional[discord.Embed] = None, name: Optional[str] = None): """Logs info and sends it to the appropriate places.""" if self.info_hook: - return await self.info_hook.send( - content=content, - username=( - f"{self.bot.user.name} - {name if name else 'unknown'}" - ), - avatar_url=str(self.bot.user.avatar_url), - embed=embed - ) + async def task(): + await self.info_hook.send( + content=content, + username=( + f"{self.bot.user.name} - {name if name else 'unknown'}" + ), + avatar_url=str(self.bot.user.avatar_url), + embed=embed + ) + + asyncio.create_task(task()) else: return - async def warn(self, content: str, - embed: Optional[discord.Embed] = None, - name: Optional[str] = None): + def warn(self, content: str, embed: Optional[discord.Embed] = None, + name: Optional[str] = None): """Logs warnings and sends them to the appropriate places.""" if self.warn_hook: - return await self.warn_hook.send( - content=content, - username=( - f"{self.bot.user.name} - {name if name else 'unknown'}" - ), - avatar_url=str(self.bot.user.avatar_url), - embed=embed - ) + async def task(): + await self.warn_hook.send( + content=content, + username=( + f"{self.bot.user.name} - {name if name else 'unknown'}" + ), + avatar_url=str(self.bot.user.avatar_url), + embed=embed + ) + + asyncio.create_task(task()) else: return @@ -168,20 +174,23 @@ class Logging(): ) return error_embed - async def debug(self, content: str, - embed: Optional[discord.Embed] = None, - name: Optional[str] = None): + def debug(self, content: str, + embed: Optional[discord.Embed] = None, + name: Optional[str] = None): """Logs warnings and sends them to the appropriate places.""" if self.debug_hook and (self.maintenance or self.debug_toggle): - return await self.debug_hook.send( - content=f"```{content}```", - username=( - f"{self.bot.user.name} - {name if name else 'unknown'}" - ), - avatar_url=str(self.bot.user.avatar_url), - embed=embed - ) + async def task(): + await self.debug_hook.send( + content=f"```{content}```", + username=( + f"{self.bot.user.name} - {name if name else 'unknown'}" + ), + avatar_url=str(self.bot.user.avatar_url), + embed=embed + ) + + asyncio.create_task(task()) else: return diff --git a/old_search.py b/old_search.py index 4f3ee95..a156b98 100644 --- a/old_search.py +++ b/old_search.py @@ -113,7 +113,7 @@ # Reached if error with returned results except (KeyError, IndexError) as e: # Logging - await self.warn( + self.warn( f"A user encountered a(n) `{e}` with <{instance}> when searching for `{query}`. " "Consider removing it or looking into it.", name="Failed Instance" @@ -201,7 +201,7 @@ msg = await self._old_search_logic(query, is_nsfw, category) await ctx.send(msg) - await self.info( + self.info( content=( f"**{ctx.author}** searched for `{query}` " f"in \"{ctx.guild}\" and got this:"