From aebc5dcadbde43fd9b13d689fdeee9cc6d35c25e Mon Sep 17 00:00:00 2001 From: Dan Hess Date: Thu, 25 Mar 2021 16:20:16 -0800 Subject: [PATCH] Correct logic for error sending --- utils/utilities.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/utils/utilities.py b/utils/utilities.py index 9c8b6f4..8de7af5 100644 --- a/utils/utilities.py +++ b/utils/utilities.py @@ -102,9 +102,6 @@ async def request( async def log_error(error, bot, ctx=None): - # First set the actual channel if it's not set yet - if isinstance(bot.error_channel, int): - bot.error_channel = bot.get_channel(bot.error_channel) # Format the error message fmt = f"""``` {''.join(traceback.format_tb(error.__traceback__)).strip()} @@ -112,14 +109,25 @@ async def log_error(error, bot, ctx=None): # Add the command if ctx is given if ctx is not None: fmt = f"Command = {discord.utils.escape_markdown(ctx.message.clean_content).strip()}\n{fmt}" - # If no channel is set, log to a file - if bot.error_channel is None: + # If the channel has been set, use it + if isinstance(bot.error_channel, discord.TextChannel): + await bot.error_channel.send(fmt) + # Otherwise if it hasn't been set yet, try to set it + if isinstance(bot.error_channel, int): + channel = bot.get_channel(bot.error_channel) + if channel is not None: + bot.error_channel = channel + await bot.error_channel.send(fmt) + # If we can't find the channel yet (before ready) just send to file + else: + fmt = fmt.strip("`") + with open("error_log", "a") as f: + print(fmt, file=f) + # Otherwise just send to file + else: fmt = fmt.strip("`") with open("error_log", "a") as f: print(fmt, file=f) - # Otherwise send to the error channel - else: - await bot.error_channel.send(fmt) async def convert(ctx, option):