From c71cb1b4ab1edd2b67b59ad671e00be141c148b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sk=C3=BDpala?= Date: Wed, 17 May 2023 21:59:22 +0200 Subject: [PATCH] Role for a password can be deleted --- hrochobot/cogs/roles.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/hrochobot/cogs/roles.py b/hrochobot/cogs/roles.py index 7c86089..64816fb 100644 --- a/hrochobot/cogs/roles.py +++ b/hrochobot/cogs/roles.py @@ -5,6 +5,9 @@ from discord.utils import get ROLES_JSON = "roles" +def role_mention(role): + return role.mention if role else "@deleted-role" + class Roles(commands.Cog): def __init__(self, bot): self.bot = bot @@ -36,7 +39,7 @@ class Roles(commands.Cog): msg = "" for passwd, role in roles["secret_roles"].items(): role = get(ctx.author.guild.roles, id=role) - msg += f"``{passwd}``: {role.mention}\n" + msg += f"``{passwd}``: {role_mention(role)}\n" return await ctx.respond(msg, ephemeral=True) @secret_roles.command(description="Deletes given password and its secret role.") @@ -50,7 +53,7 @@ class Roles(commands.Cog): del roles["secret_roles"][password] data.dump_guild_data(ctx.author.guild.id, ROLES_JSON, roles) return await ctx.respond( - f"Secret role {role.mention} no longer obtainable with password {password}.", + f"Secret role {role_mention(role)} no longer obtainable with password {password}.", ephemeral=True ) @@ -61,7 +64,9 @@ class Roles(commands.Cog): if password in roles["secret_roles"]: author = ctx.author role = get(author.guild.roles, id=roles["secret_roles"][password]) - await author.add_roles(role, reason="Roles assigned for password knowledge.") + if role is None: + return await ctx.respond(f"Role for this password was deleted.", ephemeral=True) + await author.add_roles(role, reason="Role assigned for password knowledge.") return await ctx.respond(f"You now have role {role.mention}.", ephemeral=True) else: return await ctx.respond("Incorrect password.", ephemeral=True)