From cc214cb1abb8937d5d390024b234cd119fcc2cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sk=C3=BDpala?= Date: Sun, 26 Mar 2023 14:51:41 +0200 Subject: [PATCH] Added Cog for role managment --- cogs/roles.py | 35 +++++++++++++++++++++++++++++++++++ main.py | 1 + 2 files changed, 36 insertions(+) create mode 100644 cogs/roles.py diff --git a/cogs/roles.py b/cogs/roles.py new file mode 100644 index 0000000..7bafd29 --- /dev/null +++ b/cogs/roles.py @@ -0,0 +1,35 @@ +import discord +import data +from discord.ext import commands +from discord.utils import get + +ROLES_JSON = "roles" + +class Roles(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @discord.slash_command() + @discord.default_permissions(manage_roles=True) + async def addsecretrole(self, ctx, role: discord.role.Role, password: str): + roles = data.load_data(ROLES_JSON) + if password in roles["secret_roles"]: + return await ctx.respond(f"Password ``{password}`` is already used.", ephemeral=True) + + roles["secret_roles"][password] = role.id + data.dump_data(ROLES_JSON, roles) + return await ctx.respond(f"Secret role {role.mention} added with password {password}.", ephemeral=True) + + @discord.slash_command() + async def secretrole(self, ctx, password: str): + roles = data.load_data(ROLES_JSON) + 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.") + return await ctx.respond(f"You now have role {role.mention}.", ephemeral=True) + else: + return await ctx.respond("Incorrect password.", ephemeral=True) + +def setup(bot): + bot.add_cog(Roles(bot)) diff --git a/main.py b/main.py index 761c80a..7a77f2f 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ bot = commands.Bot() cogs_list = [ 'basic', + 'roles', ] for cog in cogs_list: