Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add union. #141

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions cogs/fagforening.py
Original file line number Diff line number Diff line change
@@ -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))
25 changes: 23 additions & 2 deletions cogs/utils/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:",
Expand Down Expand Up @@ -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")
7 changes: 7 additions & 0 deletions cogs/utils/my_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ class NoWorplace(Exception):

class MultipleWorplaces(Exception):
pass

class NoUnion(Exception):
pass


class MultipleUnions(Exception):
pass
Loading