Skip to content

Commit

Permalink
Add error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
No767 committed Nov 22, 2023
1 parent 9940d31 commit fdacfc8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bot/libs/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
is_manager as is_manager,
is_mod as is_mod,
)
from .context import RoboContext as RoboContext
from .embeds import Embed as Embed, ErrorEmbed as ErrorEmbed
from .errors import send_error_embed as send_error_embed
from .logger import RodhajLogger as RodhajLogger
from .modals import RoboModal as RoboModal
from .time import human_timedelta as human_timedelta
Expand Down
43 changes: 43 additions & 0 deletions bot/libs/utils/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import traceback

import discord
from discord.ext import commands

from .embeds import ErrorEmbed

Expand All @@ -18,3 +19,45 @@ def produce_error_embed(error: Exception) -> ErrorEmbed:
embed.set_footer(text="Happened At")
embed.timestamp = discord.utils.utcnow()
return embed


def create_premade_embed(title: str, description: str) -> ErrorEmbed:
embed = ErrorEmbed()
embed.timestamp = discord.utils.utcnow()
embed.title = title
embed.description = description
return embed


async def send_error_embed(ctx: commands.Context, error: commands.CommandError) -> None:
if isinstance(error, commands.CommandInvokeError) or isinstance(
error, commands.HybridCommandError
):
await ctx.send(embed=produce_error_embed(error))
elif isinstance(error, commands.CommandNotFound):
await ctx.send(
embed=create_premade_embed(
"Command not found",
"The command you were looking for could not be found",
)
)
elif isinstance(error, commands.NotOwner):
# Basically completely silence it making people not know what happened
return
elif isinstance(error, commands.MissingPermissions):
missing_perms = ", ".join(error.missing_permissions).rstrip(",")
await ctx.send(
embed=create_premade_embed(
"Missing Permissions",
f"You are missing the following permissions: {missing_perms}",
)
)
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(
embed=create_premade_embed(
"Missing Required Argument",
f"You are missing the following argument(s): {error.param.name}",
)
)
else:
await ctx.send(embed=produce_error_embed(error))

0 comments on commit fdacfc8

Please sign in to comment.