Skip to content

Commit

Permalink
Modify error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
novanai committed Jan 31, 2024
1 parent 71f8489 commit 393e644
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/extensions/userroles.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

role_choices = [
hikari.CommandChoice(name="Webgroup", value="1166751688598761583"),
hikari.CommandChoice(name="gamz", value="1089204642241581139"),
hikari.CommandChoice(name="Gamez", value="1089204642241581139"),
hikari.CommandChoice(name="Croomer", value="1172696659097047050"),
]

Expand All @@ -28,7 +28,7 @@ async def add_role(
await ctx.respond(f"Done! Added <@&{role}> to your roles.", flags=hikari.MessageFlag.EPHEMERAL)
return

await ctx.respond("You already have this role!", flags=hikari.MessageFlag.EPHEMERAL)
await ctx.respond(f"You already have <@&{role}>!", flags=hikari.MessageFlag.EPHEMERAL)


@role.include
Expand All @@ -47,17 +47,26 @@ async def remove_role(
await ctx.respond(f"Done! Removed <@&{role}> from your roles.", flags=hikari.MessageFlag.EPHEMERAL)
return

await ctx.respond("You don't have this role!", flags=hikari.MessageFlag.EPHEMERAL)
await ctx.respond(f"You don't have <@&{role}>!", flags=hikari.MessageFlag.EPHEMERAL)


@role.set_error_handler
async def role_error_handler(ctx: arc.GatewayContext, exc: Exception) -> None:
@add_role.set_error_handler
async def add_error_handler(ctx: arc.GatewayContext, exc: Exception) -> None:
await role_error_handler(ctx, exc, "obtain")


@remove_role.set_error_handler
async def remove_error_handler(ctx: arc.GatewayContext, exc: Exception) -> None:
await role_error_handler(ctx, exc, "remove")


async def role_error_handler(ctx: arc.GatewayContext, exc: Exception, type: str) -> None:
role = ctx.get_option("role", arc.OptionType.STRING)
assert role is not None
role_id = int(role)

if isinstance(exc, hikari.ForbiddenError):
await ctx.respond(f"You don't have permission to obtain <@&{role_id}>.", flags=hikari.MessageFlag.EPHEMERAL)
await ctx.respond(f"You don't have permission to {type} <@&{role_id}>.", flags=hikari.MessageFlag.EPHEMERAL)
return

raise exc
Expand Down

0 comments on commit 393e644

Please sign in to comment.