From 9913a4cae1d01560b4b0cd81926eadfb6712aa29 Mon Sep 17 00:00:00 2001 From: Julian Silden Langlo Date: Tue, 25 Jun 2024 22:54:07 +0200 Subject: [PATCH] Add union. --- cogs/fagforening.py | 106 ++++++++++++++++++++++++++++++++++++++++ cogs/utils/defaults.py | 25 +++++++++- cogs/utils/my_errors.py | 7 +++ 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 cogs/fagforening.py diff --git a/cogs/fagforening.py b/cogs/fagforening.py new file mode 100644 index 0000000..b97edca --- /dev/null +++ b/cogs/fagforening.py @@ -0,0 +1,106 @@ +# Discord Packages +import discord +from discord.ext import commands + +# Bot Utilities +from cogs.utils.defaults import easy_embed, get_union, list_unions +from cogs.utils.my_errors import NoWorplace + +from typing import Union + + +class Union(commands.Cog): + """ + Klasse for diverse komandoer + """ + + def __init__(self, bot): + self.bot = bot + + @commands.guild_only() + @commands.group(name="fagforening", aliases=["fag"]) + async def union_group(self, ctx: discord.Message): + """ + Kategori for styring av fagforening + """ + + if ctx.invoked_subcommand is None: + await ctx.send_help(ctx.command) + + @union_group.command(name="leggtil", aliases=["add", "medlem"]) + async def add(self, ctx: discord.Message, bruker: discord.Member): + """ + Kommando for å legge til medlemmer + """ + try: + cwk = await get_union(ctx.author) + except NoWorplace: + await ctx.reply("Du er ikke tilknyttet en fagforening", delete_after=5) + return await ctx.message.delete(delay=5) + + unions = await list_unions(ctx.guild) + + try: + uwk = await get_union(bruker) + return await ctx.reply(f"{bruker.display_name} er allerede en {unions[uwk]}", delete_after=5) + except NoWorplace: + await bruker.add_roles(discord.Object(cwk, type=discord.Role), reason="Bound new union") + try: + await bruker.send(f"Du er nå registrert som {unions[cwk]} i {ctx.guild.name} " + f"av {ctx.author.display_name}") + except discord.Forbidden: + await ctx.reply(f"{bruker.mention} er nå registrert som {unions[cwk]}", + allowed_mentions=discord.AllowedMentions(users=True), delete_after=5) + return await ctx.message.delete(delay=5) + + @union_group.command(name="fjern", aliases=["remove", "oppsigelse", "slutt"]) + async def remove(self, ctx: discord.Message): + """ + Kommando for å avslutte et arbeidsforhold + """ + unions = await list_unions(ctx.guild) + try: + cwk = await get_union(ctx.author) + await ctx.author.remove_roles(discord.Object(cwk, type=discord.Role), reason="Removed from union") + await ctx.reply(f"Du ikke en {unions[cwk]} lengere", delete_after=5) + except NoWorplace: + await ctx.reply("Du er ikke tilknyttet en fagforening", delete_after=5) + return await ctx.message.delete(delay=5) + + @union_group.command(name="liste", aliases=["alle"]) + async def get_list(self, ctx: discord.Message): + """ + Lister alle fagforeningene som er registrert + """ + + unions = await list_unions(ctx.guild) + union_list = sorted([y.replace("-ansatt", "") for x, y in unions.items()], key=str.lower) + union_text = "\n".join(union_list) + desc = union_text + "\n\nSer du ikke fagforeningen din? " + \ + "Send logo og fagforenings-navn til en moderator, så ordner de det!" + + embed = easy_embed(self, ctx) + embed.title = "Disse fagforeningene er registrert på serveren" + embed.description = desc + return await ctx.reply(embed=embed) + + @union_group.command(name="firma", aliases=["fagforening", "se"]) + async def show_union(self, ctx: discord.Message, fagforening: Union[discord.Role, str]): + """ + Viser enkel informasjon om en fagforening + """ + if isinstance(fagforening, str): + for _role in ctx.guild.roles: + if _role.name.lower() == f"{fagforening.lower()}-ansatt": + fagforening = _role + break + + if not isinstance(fagforening, discord.Role): + return await ctx.reply("Fant ingen fagforening med dette navnet") + + await ctx.invoke(self.bot.get_command("rolle"), rolle=fagforening) + + +async def setup(bot): + # pylint: disable=missing-function-docstring + await bot.add_cog(union(bot)) diff --git a/cogs/utils/defaults.py b/cogs/utils/defaults.py index a6c22e6..b8559c5 100644 --- a/cogs/utils/defaults.py +++ b/cogs/utils/defaults.py @@ -2,7 +2,7 @@ import discord # Bot Utilities -from cogs.utils.my_errors import MultipleWorplaces, NoWorplace +from cogs.utils.my_errors import MultipleWorplaces, NoWorplace, MultipleUnions, NoUnion booler = { True: ":white_check_mark:", @@ -173,8 +173,29 @@ async def get_workplace(user: discord.Member) -> int: if len(roles) == 1: return int(roles[0]) elif len(roles) > 1: - raise MultipleWorplaces("Multiple workplaces was found") + raise MultipleWorplaces("Multiple workplaces were found") elif len(roles) == 0: raise NoWorplace("No workplace was found") else: raise Exception("Unknown error while determining workplace") + +async def list_unions(guild: discord.Guild) -> dict: + """ + Hjelpefunkjson for å liste alle registrerte fagforeninger + """ + return {int(x.id): x.name for x in guild.roles if x.name.endswith("-medlem")} + + +async def get_union(user: discord.Member) -> int: + """ + Hjelpefunkson for henting av brukers fagforeninger + """ + roles = [int(x.id) for x in user.roles if x.name.endswith("-medlem")] + if len(roles) == 1: + return int(roles[0]) + elif len(roles) > 1: + raise MultipleUnions("Multiple unions were found") + elif len(roles) == 0: + raise NoUnion("No union was found") + else: + raise Exception("Unknown error while determining union") diff --git a/cogs/utils/my_errors.py b/cogs/utils/my_errors.py index d015619..dc31bc4 100644 --- a/cogs/utils/my_errors.py +++ b/cogs/utils/my_errors.py @@ -17,3 +17,10 @@ class NoWorplace(Exception): class MultipleWorplaces(Exception): pass + +class NoUnion(Exception): + pass + + +class MultipleUnions(Exception): + pass \ No newline at end of file