Example #1
0
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandNotFound):
        return
    log.debug("Error caused by message: `{}`".format(ctx.message.content))
    log.debug('\n'.join(traceback.format_exception(type(error), error, error.__traceback__)))
    if isinstance(error, AvraeException):
        return await ctx.send(str(error))
    tb = ''.join(traceback.format_exception(type(error), error, error.__traceback__))
    if isinstance(error,
                  (commands.MissingRequiredArgument, commands.BadArgument, commands.NoPrivateMessage, ValueError)):
        return await ctx.send("Error: " + str(
            error) + f"\nUse `{ctx.prefix}help " + ctx.command.qualified_name + "` for help.")
    elif isinstance(error, commands.CheckFailure):
        return await ctx.send("Error: You are not allowed to run this command.")
    elif isinstance(error, commands.CommandOnCooldown):
        return await ctx.send("This command is on cooldown for {:.1f} seconds.".format(error.retry_after))
    elif isinstance(error, CommandInvokeError):
        original = error.original
        if isinstance(original, EvaluationError):  # PM an alias author tiny traceback
            e = original.original
            if not isinstance(e, AvraeException):
                tb = f"```py\n{''.join(traceback.format_exception(type(e), e, e.__traceback__, limit=0, chain=False))}\n```"
                try:
                    await ctx.author.send(tb)
                except Exception as e:
                    log.info(f"Error sending traceback: {e}")
        if isinstance(original, AvraeException):
            return await ctx.send(str(original))
        if isinstance(original, Forbidden):
            try:
                return await ctx.author.send(
                    f"Error: I am missing permissions to run this command. "
                    f"Please make sure I have permission to send messages to <#{ctx.channel.id}>."
                )
            except:
                try:
                    return await ctx.send(f"Error: I cannot send messages to this user.")
                except:
                    return
        if isinstance(original, NotFound):
            return await ctx.send("Error: I tried to edit or delete a message that no longer exists.")
        if isinstance(original, ValueError) and str(original) in ("No closing quotation", "No escaped character"):
            return await ctx.send("Error: No closing quotation.")
        if isinstance(original, AttributeError) and str(original) in ("'NoneType' object has no attribute 'name'",):
            return await ctx.send("Error in Discord API. Please try again.")
        if isinstance(original, (ClientResponseError, InvalidArgument, asyncio.TimeoutError)):
            return await ctx.send("Error in Discord API. Please try again.")
        if isinstance(original, HTTPException):
            if original.response.status == 400:
                return await ctx.send("Error: Message is too long, malformed, or empty.")
            if original.response.status == 500:
                return await ctx.send("Error: Internal server error on Discord's end. Please try again.")
        if isinstance(original, redis.ResponseError):
            await ctx.send(
                "Error: I am having an issue writing to my database. Please report this to the dev!")
            return await bot.owner.send(f"Database error!\n{repr(original)}")
        if isinstance(original, OverflowError):
            return await ctx.send(
                f"Error: A number is too large for me to store. "
                f"This generally means you've done something terribly wrong.")

    error_msg = gen_error_message()

    await ctx.send(
        f"Error: {str(error)}\nUh oh, that wasn't supposed to happen! "
        f"Please ignore this and tell the developer that {error_msg}!")
    try:
        await bot.owner.send(
            f"**{error_msg}**\n" \
            + "Error in channel {} ({}), server {} ({}): {}\nCaused by message: `{}`".format(
                ctx.channel, ctx.channel.id, ctx.guild,
                ctx.guild.id, repr(error),
                ctx.message.content))
        await ctx.send("Try again. The goblins should have been murdered. If they haven't harrass the developers. They'll love that")
        process = ["sudo", "systemctl", "restart", "mongodb"]
        subprocess.run(process)
    except AttributeError:
        await bot.owner.send(f"**{error_msg}**\n" \
                             + "Error in PM with {} ({}), shard 0: {}\nCaused by message: `{}`".format(
            ctx.author.mention, str(ctx.author), repr(error), ctx.message.content))
    for o in discord_trim(tb):
        await bot.owner.send(o)
    log.error("Error caused by message: `{}`".format(ctx.message.content))
    traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
Example #2
0
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandNotFound):
        return
    if isinstance(error, AvraeException):
        return await ctx.send(str(error))
    tb = ''.join(
        traceback.format_exception(type(error), error, error.__traceback__))
    if isinstance(
            error,
        (commands.UserInputError, commands.NoPrivateMessage, ValueError)):
        return await ctx.send(f"Error: {str(error)}\nUse `{ctx.prefix}help " +
                              ctx.command.qualified_name + "` for help.")
    elif isinstance(error, commands.CheckFailure):
        msg = str(error) or "You are not allowed to run this command."
        return await ctx.send(f"Error: {msg}")
    elif isinstance(error, commands.CommandOnCooldown):
        return await ctx.send(
            "This command is on cooldown for {:.1f} seconds.".format(
                error.retry_after))
    elif isinstance(error, CommandInvokeError):
        original = error.original
        if isinstance(original,
                      EvaluationError):  # PM an alias author tiny traceback
            e = original.original
            if not isinstance(e, AvraeException):
                tb = f"```py\nError when parsing expression {original.expression}:\n" \
                    f"{''.join(traceback.format_exception(type(e), e, e.__traceback__, limit=0, chain=False))}\n```"
                try:
                    await ctx.author.send(tb)
                except Exception as e:
                    log.info(f"Error sending traceback: {e}")
        if isinstance(original, AvraeException):
            return await ctx.send(str(original))
        if isinstance(original, Forbidden):
            try:
                return await ctx.author.send(
                    f"Error: I am missing permissions to run this command. "
                    f"Please make sure I have permission to send messages to <#{ctx.channel.id}>."
                )
            except:
                try:
                    return await ctx.send(
                        f"Error: I cannot send messages to this user.")
                except:
                    return
        if isinstance(original, NotFound):
            return await ctx.send(
                "Error: I tried to edit or delete a message that no longer exists."
            )
        if isinstance(original, ValueError) and str(original) in (
                "No closing quotation", "No escaped character"):
            return await ctx.send("Error: No closing quotation.")
        if isinstance(original, (ClientResponseError, InvalidArgument,
                                 asyncio.TimeoutError, ClientOSError)):
            return await ctx.send("Error in Discord API. Please try again.")
        if isinstance(original, HTTPException):
            if original.response.status == 400:
                return await ctx.send(
                    "Error: Message is too long, malformed, or empty.")
            if original.response.status == 500:
                return await ctx.send(
                    "Error: Internal server error on Discord's end. Please try again."
                )
        if isinstance(original, OverflowError):
            return await ctx.send(
                f"Error: A number is too large for me to store.")

    error_msg = gen_error_message()

    await ctx.send(
        f"Error: {str(error)}\nUh oh, that wasn't supposed to happen! "
        f"Please join <http://support.avrae.io> and let us know about the error!\n"
        f"Error code: {error_msg}")

    # send error to error channel
    error_channel = bot.get_channel(ERROR_CHANNEL_ID)
    if error_channel is not None:
        try:
            await error_channel.send(
                f"**{error_msg}**\n" \
                + "Error in channel {} ({}), server {} ({}): {}\nCaused by message: `{}`".format(
                    ctx.channel, ctx.channel.id, ctx.guild,
                    ctx.guild.id, repr(error),
                    ctx.message.content))
        except AttributeError:
            await error_channel.send(f"**{error_msg}**\n" \
                                     + "Error in PM with {} ({}), shard 0: {}\nCaused by message: `{}`".format(
                ctx.author.mention, str(ctx.author), repr(error), ctx.message.content))
        for o in discord_trim(tb):
            await error_channel.send(o)
    log.error("Error caused by message: `{}`".format(ctx.message.content))
    traceback.print_exception(type(error),
                              error,
                              error.__traceback__,
                              file=sys.stderr)
async def on_command_error(error, ctx):
    if isinstance(error, commands.CommandNotFound):
        return
    log.debug("Error caused by message: `{}`".format(ctx.message.content))
    log.debug('\n'.join(
        traceback.format_exception(type(error), error, error.__traceback__)))
    if isinstance(error, AvraeException):
        return await bot.send_message(ctx.message.channel, str(error))
    tb = ''.join(
        traceback.format_exception(type(error), error, error.__traceback__))
    if isinstance(error, commands.CheckFailure):
        await bot.send_message(
            ctx.message.channel,
            "Error: Either you do not have the permissions to run this command, the command is disabled, or something went wrong internally."
        )
        return
    elif isinstance(error,
                    (commands.MissingRequiredArgument, commands.BadArgument,
                     commands.NoPrivateMessage, ValueError)):
        return await bot.send_message(
            ctx.message.channel, "Error: " + str(error) + "\nUse `!help " +
            ctx.command.qualified_name + "` for help.")
    elif isinstance(error, commands.CommandOnCooldown):
        return await bot.send_message(
            ctx.message.channel,
            "This command is on cooldown for {:.1f} seconds.".format(
                error.retry_after))
    elif isinstance(error, CommandInvokeError):
        original = error.original
        if isinstance(original,
                      EvaluationError):  # PM an alias author tiny traceback
            e = original.original
            if not isinstance(e, AvraeException):
                tb = f"```py\n{''.join(traceback.format_exception(type(e), e, e.__traceback__, limit=0, chain=False))}\n```"
                try:
                    await bot.send_message(ctx.message.author, tb)
                except Exception as e:
                    log.info(f"Error sending traceback: {e}")
        if isinstance(original, AvraeException):
            return await bot.send_message(ctx.message.channel, str(original))
        if isinstance(original, Forbidden):
            try:
                return await bot.send_message(
                    ctx.message.author,
                    "Error: I am missing permissions to run this command. Please make sure I have permission to send messages to <#{}>."
                    .format(ctx.message.channel.id))
            except:
                try:
                    return await bot.send_message(
                        ctx.message.channel,
                        f"Error: I cannot send messages to this user.")
                except:
                    return
        if isinstance(original, NotFound):
            return await bot.send_message(
                ctx.message.channel,
                "Error: I tried to edit or delete a message that no longer exists."
            )
        if isinstance(original, ValueError) and str(original) in (
                "No closing quotation", "No escaped character"):
            return await bot.send_message(ctx.message.channel,
                                          "Error: No closing quotation.")
        if isinstance(original, AttributeError) and str(original) in (
                "'NoneType' object has no attribute 'name'", ):
            return await bot.send_message(
                ctx.message.channel, "Error in Discord API. Please try again.")
        if isinstance(original, (ClientResponseError, InvalidArgument)):
            return await bot.send_message(
                ctx.message.channel, "Error in Discord API. Please try again.")
        if isinstance(original, HTTPException):
            if original.response.status == 400:
                return await bot.send_message(
                    ctx.message.channel,
                    "Error: Message is too long, malformed, or empty.")
            if original.response.status == 500:
                return await bot.send_message(
                    ctx.message.channel,
                    "Error: Internal server error on Discord's end. Please try again."
                )
        if isinstance(original, redis.ResponseError):
            await bot.send_message(
                ctx.message.channel,
                "Error: I am having an issue writing to my database. Please report this to the dev!"
            )
            return await bot.send_message(
                bot.owner, f"Database error!\n{repr(original)}")

    error_msg = gen_error_message()

    await bot.send_message(
        ctx.message.channel,
        f"Error: {str(error)}\nUh oh, that wasn't supposed to happen! "
        f"Please join <https://support.avrae.io> and tell the developer that {error_msg}!"
    )
    try:
        await bot.send_message(bot.owner,
                               f"**{error_msg}**\n" \
                               + "Error in channel {} ({}), server {} ({}), shard {}: {}\nCaused by message: `{}`".format(
                                   ctx.message.channel, ctx.message.channel.id, ctx.message.server,
                                   ctx.message.server.id, getattr(bot, 'shard_id', 0), repr(error),
                                   ctx.message.content))
    except AttributeError:
        await bot.send_message(bot.owner, f"**{error_msg}**\n" \
                               + "Error in PM with {} ({}), shard 0: {}\nCaused by message: `{}`".format(
            ctx.message.author.mention, str(ctx.message.author), repr(error), ctx.message.content))
    for o in discord_trim(tb):
        await bot.send_message(bot.owner, o)
    log.error("Error caused by message: `{}`".format(ctx.message.content))
    traceback.print_exception(type(error),
                              error,
                              error.__traceback__,
                              file=sys.stderr)
    async def on_command_error(self, ctx, error):
        owner = self.bot.get_user(GG.OWNER)
        if isinstance(error, commands.CommandNotFound):
            return
        log.debug("Error caused by message: `{}`".format(ctx.message.content))
        log.debug('\n'.join(
            traceback.format_exception(type(error), error,
                                       error.__traceback__)))
        if isinstance(error, FeCrawlerException):
            return await ctx.send(str(error))
        tb = ''.join(
            traceback.format_exception(type(error), error,
                                       error.__traceback__))
        if isinstance(error,
                      (commands.MissingRequiredArgument, commands.BadArgument,
                       commands.NoPrivateMessage, ValueError)):
            return await ctx.send("Error: " + str(error) +
                                  f"\nUse `{ctx.prefix}oldhelp " +
                                  ctx.command.qualified_name + "` for help.")
        elif isinstance(error, commands.CheckFailure):
            return await ctx.send(
                "Error: You are not allowed to run this command.")
        elif isinstance(error, commands.CommandOnCooldown):
            return await ctx.send(
                "This command is on cooldown for {:.1f} seconds.".format(
                    error.retry_after))
        elif isinstance(error, UnexpectedQuoteError):
            return await ctx.send(
                f"Error: You either gave me a command that requires quotes, and you forgot one.\n"
                f"Or you have used the characters that are used to define what's optional and required (<> and [])\n"
                f"Please check the ``!help`` command for proper usage of my commands."
            )
        elif isinstance(error, ExpectedClosingQuoteError):
            return await ctx.send(
                f"Error: You gave me a command that requires quotes, and you forgot one at the end."
            )
        elif isinstance(error, CommandInvokeError):
            original = error.original
            if isinstance(
                    original,
                    EvaluationError):  # PM an alias author tiny traceback
                e = original.original
                if not isinstance(e, FeCrawlerException):
                    tb = f"```py\nError when parsing expression {original.expression}:\n" \
                         f"{''.join(traceback.format_exception(type(e), e, e.__traceback__, limit=0, chain=False))}\n```"
                    try:
                        await ctx.author.send(tb)
                    except Exception as e:
                        log.info(f"Error sending traceback: {e}")
            if isinstance(original, FeCrawlerException):
                return await ctx.send(str(original))
            if isinstance(original, Forbidden):
                try:
                    return await ctx.author.send(
                        f"Error: I am missing permissions to run this command. "
                        f"Please make sure I have permission to send messages to <#{ctx.channel.id}>."
                    )
                except:
                    try:
                        return await ctx.send(
                            f"Error: I cannot send messages to this user.")
                    except:
                        return
            if isinstance(original, NotFound):
                return await ctx.send(
                    "Error: I tried to edit or delete a message that no longer exists."
                )
            if isinstance(original, ValueError) and str(original) in (
                    "No closing quotation", "No escaped character"):
                return await ctx.send("Error: No closing quotation.")
            if isinstance(original, (ClientResponseError, InvalidArgument,
                                     asyncio.TimeoutError, ClientOSError)):
                return await ctx.send("Error in Discord API. Please try again."
                                      )
            if isinstance(original, HTTPException):
                if original.response.status == 400:
                    return await ctx.send(
                        "Error: Message is too long, malformed, or empty.")
                if original.response.status == 500:
                    return await ctx.send(
                        "Error: Internal server error on Discord's end. Please try again."
                    )
                if original.response.status == 503:
                    return await ctx.send(
                        "Error: Connecting failure on Discord's end. (Service unavailable). Please check https://status.discordapp.com for the status of the Discord Service, and try again later."
                    )
            if isinstance(original, OverflowError):
                return await ctx.send(
                    f"Error: A number is too large for me to store.")
            if isinstance(original, SearchException):
                return await ctx.send(
                    f"Search Timed out, please try the command again.")
            if isinstance(original, KeyError):
                if str(original) == "'content-type'":
                    return await ctx.send(
                        f"The command errored on Discord's side. I can't do anything about this, Sorry.\nPlease check https://status.discordapp.com for the status on the Discord API, and try again later."
                    )

        error_msg = gen_error_message()

        await ctx.send(
            f"Error: {str(error)}\nUh oh, that wasn't supposed to happen! "
            f"Please join the 5eCrawler Support Discord (!support) and tell the developer that: **{error_msg}!**"
        )
        if not self.bot.testing:
            try:
                await owner.send(
                    f"**{error_msg}**\n" \
                    + "Error in channel {} ({}), server {} ({}): {}\nCaused by message: `{}`".format(
                        ctx.channel, ctx.channel.id, ctx.guild,
                        ctx.guild.id, repr(error),
                        ctx.message.content))
            except AttributeError:
                await owner.send(f"**{error_msg}**\n" \
                                 + "Error in PM with {} ({}), shard 0: {}\nCaused by message: `{}`".format(
                    ctx.author.mention, str(ctx.author), repr(error), ctx.message.content))
            for o in discord_trim(tb):
                await owner.send(o)
        log.error("Error caused by message: `{}`".format(ctx.message.content))