Browse Source

Role for a password can be deleted

news
Daniel Skýpala 2 years ago
parent
commit
c71cb1b4ab
  1. 11
      hrochobot/cogs/roles.py

11
hrochobot/cogs/roles.py

@ -5,6 +5,9 @@ from discord.utils import get
ROLES_JSON = "roles" ROLES_JSON = "roles"
def role_mention(role):
return role.mention if role else "@deleted-role"
class Roles(commands.Cog): class Roles(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
@ -36,7 +39,7 @@ class Roles(commands.Cog):
msg = "" msg = ""
for passwd, role in roles["secret_roles"].items(): for passwd, role in roles["secret_roles"].items():
role = get(ctx.author.guild.roles, id=role) 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) return await ctx.respond(msg, ephemeral=True)
@secret_roles.command(description="Deletes given password and its secret role.") @secret_roles.command(description="Deletes given password and its secret role.")
@ -50,7 +53,7 @@ class Roles(commands.Cog):
del roles["secret_roles"][password] del roles["secret_roles"][password]
data.dump_guild_data(ctx.author.guild.id, ROLES_JSON, roles) data.dump_guild_data(ctx.author.guild.id, ROLES_JSON, roles)
return await ctx.respond( 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 ephemeral=True
) )
@ -61,7 +64,9 @@ class Roles(commands.Cog):
if password in roles["secret_roles"]: if password in roles["secret_roles"]:
author = ctx.author author = ctx.author
role = get(author.guild.roles, id=roles["secret_roles"][password]) 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) return await ctx.respond(f"You now have role {role.mention}.", ephemeral=True)
else: else:
return await ctx.respond("Incorrect password.", ephemeral=True) return await ctx.respond("Incorrect password.", ephemeral=True)

Loading…
Cancel
Save