From 66e0d4707e1728a1a44e94e7442e0814f0186045 Mon Sep 17 00:00:00 2001 From: Jocadbz Date: Fri, 13 Jan 2023 17:34:49 -0300 Subject: [PATCH] Add leave role function --- bot.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index 270aef3..91604b9 100644 --- a/bot.py +++ b/bot.py @@ -40,7 +40,7 @@ async def on_ready(): print('------') -@client.tree.command(name="change_hostname") +@client.tree.command(name="join_role") @app_commands.describe(distro='Assign yourself a role') async def distro(interaction: discord.Interaction, distro: eval("typing.Literal[\""+ "\", \"".join(config["roles"].keys()) +"\"]")): if not interaction.guild.id==config["guild"]: @@ -53,12 +53,31 @@ async def distro(interaction: discord.Interaction, distro: eval("typing.Literal[ try: await user.add_roles(role) await interaction.response.send_message(content=f'Added {role}.') - prnt("Added role.") + print("Added role.") except: await interaction.response.send_message(content='Something went wrong.') print("Failed to add role. Permissions set correctly?") +@client.tree.command(name="leave_role") +@app_commands.describe(distro='Unassign yourself a role') +async def distro(interaction: discord.Interaction, distro: eval("typing.Literal[\""+ "\", \"".join(config["roles"].keys()) +"\"]")): + if not interaction.guild.id==config["guild"]: + print("Role assignment attempted from unknown guild.") + return + + user = interaction.user + role = discord.utils.get(interaction.guild.roles, id=config["roles"][distro]) + + try: + await user.remove_roles(role) + await interaction.response.send_message(content=f'Left {role}.') + print("Removed role.") + except: + await interaction.response.send_message(content='Something went wrong.') + print("Failed to remove role. Permissions set correctly?") + + # This context menu command only works on messages @client.tree.context_menu(name='Report to Moderators') async def report_message(interaction: discord.Interaction, message: discord.Message): -- 2.30.2