Skip to content

Commit

Permalink
error handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
EllieJaybee committed Jul 4, 2024
1 parent d2f0d0c commit d1b1fff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
27 changes: 22 additions & 5 deletions bot/plugins/context/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
plugin = Plugin()


class NoSauceError(IndexError):
pass


@plugin.include
@crescent.message_command(name="Find Sauce")
async def sauce(ctx: crescent.Context, message: hikari.Message):
Expand All @@ -16,16 +20,29 @@ async def sauce(ctx: crescent.Context, message: hikari.Message):
if message.attachments:
for att in message.attachments:
sourced = await sauceclient.from_url(att.url)
if not sourced:
raise NoSauceError
await ctx.respond(sourced[0].url)
elif message.content:
if "https://" not in message.content:
return await ctx.respond("Nothing to be sauced")
for word in message.content.split():
if word.startswith("https://"):
try:
sourced = await sauceclient.from_url(word)
await ctx.respond(sourced[0].url)
except errors.InvalidImageException:
await ctx.respond("Invalid url")
sourced = await sauceclient.from_url(word)
await ctx.respond(sourced[0].url)
else:
await ctx.respond("Nothing to be sauced")


@plugin.include
@crescent.catch_command(NoSauceError)
async def no_sauce_handler(err: NoSauceError, ctx: crescent.Context):
await ctx.respond("Can't sauce message :<")


@plugin.include
@crescent.catch_command(errors.InvalidImageException)
async def invalid_image_handler(
err: errors.InvalidImageException, ctx: crescent.Context
):
await ctx.respond("Invalid url")
8 changes: 8 additions & 0 deletions bot/plugins/main/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ async def startup(event: hikari.GuildAvailableEvent):
for channel_id in guild.get_channels():
channel = guild.get_channel(channel_id)
logger.debug(f"L Connected to #{channel.name}({channel.id})")


@plugin.include
@crescent.catch_command(Exception)
async def on_any_command_error(exc: Exception, ctx: crescent.Context):
await ctx.respond("Unknown error. Report to maintainer")
logger.error(f"{exc}")
raise

0 comments on commit d1b1fff

Please sign in to comment.