Skip to content

Commit

Permalink
remove error handler, add more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzdom committed Oct 29, 2024
1 parent 4192be3 commit a329e04
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions src/extensions/fortune.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ async def fortune_command(
ctx: arc.GatewayContext,
user: arc.Option[hikari.User, arc.UserParams("A user")] = None,
) -> None:
"""Send a user a random Fortune!"""
"""Send a random Fortune!"""

# generate fortune
fortune_message = fortune()
assert len(fortune_message) > 0
if not fortune_message:
await ctx.respond(
"❌ Failed to generate fortune. Please try again.",
flags=hikari.MessageFlag.EPHEMERAL,
)
return

# mention user if present
if user is not None:
message = f"Dear {user.mention},\n```{fortune_message}```"
else:
Expand All @@ -25,34 +31,15 @@ async def fortune_command(
# do not exceed Discord's 2000 character limit
if len(message) > 2000:
await ctx.respond(
"The generated fortune is too long to send. Please try again.",
"❌ FThe generated fortune is too long to send. Please try again.",
flags=hikari.MessageFlag.EPHEMERAL,
)
return

# send fortune in a codeblock
await ctx.respond(message)


@fortune_cmd.set_error_handler
async def fortune_error_handler(ctx: arc.GatewayContext, exc: Exception) -> None:
user = ctx.get_option("user", arc.OptionType.USER)
assert user is not None

if isinstance(exc, AssertionError):
await ctx.respond(
"❌ Failed to generate the fortune. Please try again.",
flags=hikari.MessageFlag.EPHEMERAL,
)
return
if isinstance(exc, hikari.NotFoundError):
await ctx.respond(
"❌ Blockbot can't find that user.", flags=hikari.MessageFlag.EPHEMERAL
)
return

raise exc


@arc.loader
def loader(client: arc.GatewayClient) -> None:
client.add_plugin(fortune_cmd)

0 comments on commit a329e04

Please sign in to comment.