Skip to content

Commit

Permalink
Add command & disable commands in DMs
Browse files Browse the repository at this point in the history
  • Loading branch information
novanai committed Jan 31, 2024
1 parent 912c7c8 commit 71f8489
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@

logging.info(f"Debug mode is {DEBUG}; You can safely ignore this.")

arc_client = arc.GatewayClient(bot)
arc_client = arc.GatewayClient(bot, is_dm_enabled=False)
arc_client.load_extensions_from("./src/extensions/")
68 changes: 68 additions & 0 deletions src/extensions/userroles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import arc
import hikari

plugin = arc.GatewayPlugin("User Roles")

role = plugin.include_slash_group("role", "Get/remove assignable roles.")

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


@role.include
@arc.slash_subcommand("add", "Add an assignable role.")
async def add_role(
ctx: arc.GatewayContext, role: arc.Option[str, arc.StrParams("The role to add.", choices=role_choices)]
) -> None:
assert ctx.guild_id
assert ctx.member

role_id = int(role)
if role_id not in ctx.member.role_ids:
await ctx.client.rest.add_role_to_member(
ctx.guild_id, ctx.author, int(role), reason=f"{ctx.author} added 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)


@role.include
@arc.slash_subcommand("remove", "Remove an assignable role.")
async def remove_role(
ctx: arc.GatewayContext, role: arc.Option[str, arc.StrParams("The role to remove.", choices=role_choices)]
) -> None:
assert ctx.guild_id
assert ctx.member

role_id = int(role)
if role_id in ctx.member.role_ids:
await ctx.client.rest.remove_role_from_member(
ctx.guild_id, ctx.author, int(role), reason=f"{ctx.author} removed 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)


@role.set_error_handler
async def role_error_handler(ctx: arc.GatewayContext, exc: Exception) -> 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)
return

raise exc


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

0 comments on commit 71f8489

Please sign in to comment.