import discord
from discord.ext import commands


class Roles(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command(name="giveRole")
    @commands.has_permissions(manage_roles=True)
    async def _add_role(self, ctx, member: discord.Member, role):
        role = discord.utils.get(ctx.guild.roles, name=role)
        await member.add_roles(role)
        await ctx.reply("role given", delete_after=2)

    @commands.command(name="showRoles")
    @commands.has_permissions(manage_roles=True)
    async def _show_role(self, ctx):
        roles = [role for role in ctx.guild.roles][1:]
        embed = discord.Embed(
            colour=discord.Colour.purple(),
            timestamp=ctx.message.created_at,
            title=f"Roles",
        )
        embed.add_field(
            name="Roles:", value="\n".join([role.mention for role in roles])
        )
        await ctx.send(embed=embed)

    @commands.command(name="takeRole")
    @commands.has_permissions(manage_roles=True)
    async def _take_role(self, ctx, member: discord.Member, role):
        role = discord.utils.get(ctx.guild.roles, name=role)
        await member.remove_roles(role)
        await ctx.reply("role taken", delete_after=2)

    @commands.command(name="deleteRole")
    @commands.has_permissions(manage_roles=True)
    async def _delete_role(self, ctx, role_name):
        role_object = discord.utils.get(ctx.guild.roles, name=role_name)
        await role_object.delete()

    @commands.command("addRole")
    @commands.has_permissions(
        manage_roles=True
    )  # Check if the user executing the command can manage roles
    async def _create_role(self, ctx, name):
        guild = ctx.guild
        await guild.create_role(name=name)
        await ctx.send(f"Role `{name}` has been created")


async def setup(client):
    await client.add_cog(Roles(client))
