diff --git a/db/PCP.py b/db/PCP.py new file mode 100644 index 0000000..b031115 --- /dev/null +++ b/db/PCP.py @@ -0,0 +1,15 @@ +from db import Base +from sqlalchemy import Column, BigInteger, String + + +class PCP(Base): + __tablename__ = "pcp" + guild_id = Column(BigInteger, primary_key=True) + roles_re = Column(String, nullable=False) + start_role_re = Column(String) + + def __init__(self, guild_id: int, roles_re: str, start_role_re: str = None): + self.guild_id = guild_id + self.roles_re = roles_re + if start_role_re: + self.start_role_re = start_role_re diff --git a/db/__init__.py b/db/__init__.py index a983286..3d2878c 100644 --- a/db/__init__.py +++ b/db/__init__.py @@ -14,4 +14,5 @@ from db.Warn import Warn from db.WarnAction import WarnAction from db.InviteRole import InviteRole from db.Tomuss import Tomuss +from db.PCP import PCP Base.metadata.create_all(engine) diff --git a/extensions/pcp.py b/extensions/pcp.py index 4a3b1b8..00721a1 100644 --- a/extensions/pcp.py +++ b/extensions/pcp.py @@ -2,17 +2,17 @@ import re from discord import Embed, Member from discord.ext import commands -from discord.ext.commands import BadArgument +from discord.ext.commands import BadArgument, MissingPermissions +import db from administrator.logger import logger extension_name = "PCP" logger = logger.getChild(extension_name) -group_re = re.compile(r"(G[0-9]S[0-9]|ASPE|LP DEVOPS|LP ESSIR|LP SID)") -msg_url_re = re.compile(r"https://.*discord.*\.com/channels/[0-9]+/([0-9+]+)/([0-9]+)") -role_mention_re = re.compile(r"<@&[0-9]+>") -user_mention_re = re.compile(r"<@![0-9]+>") +msg_url_re = re.compile(r"^https://.*discord.*\.com/channels/[0-9]+/([0-9+]+)/([0-9]+)$") +role_mention_re = re.compile(r"^<@&[0-9]+>$") +user_mention_re = re.compile(r"^<@![0-9]+>$") class PCP(commands.Cog): @@ -26,38 +26,65 @@ class PCP(commands.Cog): @commands.guild_only() async def pcp(self, ctx: commands.Context): group = ctx.message.content.replace(f"{ctx.prefix}{ctx.command} ", "").upper() - if group and group_re.fullmatch(group): - await ctx.message.add_reaction("\U000023f3") - role = next(filter(lambda r: r.name.upper() == group, ctx.guild.roles), None) + if group: + s = db.Session() + p = s.query(db.PCP).get(ctx.guild.id) + s.close() + if p and re.fullmatch(p.roles_re, group): + await ctx.message.add_reaction("\U000023f3") + role = next(filter(lambda r: r.name.upper() == group, ctx.guild.roles), None) - if not role: + def roles() -> list: + return list(filter( + lambda r: re.fullmatch(p.roles_re, r.name.upper()) or + (p.start_role_re and re.fullmatch(p.start_role_re, r.name.upper())), + ctx.author.roles + )) + + if not role or role.name in map(lambda r: r.name, roles()): + await ctx.message.remove_reaction("\U000023f3", self.bot.user) + raise BadArgument() + + while roles(): + await ctx.author.remove_roles(*roles()) + + while role not in ctx.author.roles: + await ctx.author.add_roles(role) await ctx.message.remove_reaction("\U000023f3", self.bot.user) - raise BadArgument() + await ctx.message.add_reaction("\U0001f44d") + return - roles = list(filter(lambda r: group_re.fullmatch(r.name.upper()) or r.name == "nouveau venu", - ctx.author.roles)) - if role.name in map(lambda r: r.name, roles): - await ctx.message.remove_reaction("\U000023f3", self.bot.user) - raise BadArgument() - elif roles: - await ctx.author.remove_roles(*roles) - - await ctx.author.add_roles(role, atomic=True) - await ctx.message.remove_reaction("\U000023f3", self.bot.user) - await ctx.message.add_reaction("\U0001f44d") - elif ctx.invoked_subcommand is None: + if ctx.invoked_subcommand is None: await ctx.invoke(self.pcp_help) @pcp.group("help", pass_context=True) async def pcp_help(self, ctx: commands.Context): embed = Embed(title="PCP help") - embed.add_field(name="pcp ", value="Join your group", inline=False) + s = db.Session() + p = s.query(db.PCP).get(ctx.guild.id) + s.close() + if p: + embed.add_field(name="pcp ", value="Join your group", inline=False) if await self.pcp_group.can_run(ctx): embed.add_field(name="pcp group", value="Manage PCP group", inline=False) + if await self.pcp_pin.can_run(ctx): + embed.add_field(name="pcp pin ", value="Pin a message with the url", inline=False) + if await self.pcp_unpin.can_run(ctx): + embed.add_field(name="pcp unpin ", value="Unpin a message with the url", inline=False) + if not embed.fields: + raise MissingPermissions(None) await ctx.send(embed=embed) @pcp.group("pin", pass_context=True) async def pcp_pin(self, ctx: commands.Context, url: str): + await self.pin(ctx, url, True) + + @pcp.group("unpin", pass_context=True) + async def pcp_unpin(self, ctx: commands.Context, url: str): + await self.pin(ctx, url, False) + + @staticmethod + async def pin(ctx: commands.Context, url: str, action: bool): r = msg_url_re.fullmatch(url) if not r: raise BadArgument() @@ -71,9 +98,14 @@ class PCP(commands.Cog): if not m: raise BadArgument() - await m.pin() + if action: + await m.pin() + msg = "pinned a message" + else: + await m.unpin() + msg = "unpinned a message" - await ctx.send(f"{ctx.author.mention} pinned a message") + await ctx.send(f"{ctx.author.mention} {msg}") @pcp.group("group", pass_context=True) @commands.has_permissions(administrator=True) @@ -84,14 +116,23 @@ class PCP(commands.Cog): @pcp_group.group("help", pass_context=True) async def pcp_group_help(self, ctx: commands.Context): embed = Embed(title="PCP group help") + embed.add_field(name="pcp group set [Welcome role Regex]", + value="Set regex for group role", inline=False) + embed.add_field(name="pcp group unset", value="Unset regex for group role", inline=False) + embed.add_field(name="pcp group subject", value="Manage subjects for group", inline=False) embed.add_field(name="pcp group fix_vocal", value="Check all text channel permissions to reapply vocal permissions", inline=False) - embed.add_field(name="pcp group subject", value="Manage subjects for group", inline=False) await ctx.send(embed=embed) @pcp_group.group("fix_vocal", pass_context=True) async def pcp_group_fix_vocal(self, ctx: commands.Context): - for cat in filter(lambda c: group_re.fullmatch(c.name.upper()), ctx.guild.categories): + s = db.Session() + p = s.query(db.PCP).get(ctx.guild.id) + s.close() + if not p: + raise BadArgument() + + for cat in filter(lambda c: re.fullmatch(p.roles_re, c.name.upper()), ctx.guild.categories): await ctx.send(f"{cat.name}...") teachers = [] for t in cat.text_channels: @@ -104,6 +145,32 @@ class PCP(commands.Cog): await ctx.send(f"{cat.name} done") await ctx.message.add_reaction("\U0001f44d") + @pcp_group.group("set", pass_context=True) + async def pcp_group_set(self, ctx: commands.Context, roles_re: str, start_role_re: str = None): + s = db.Session() + p = s.query(db.PCP).get(ctx.guild.id) + if p: + p.roles_re = roles_re.upper() + p.start_role_re = start_role_re.upper() if start_role_re else None + else: + p = db.PCP(ctx.guild.id, roles_re.upper(), start_role_re.upper() if start_role_re else None) + s.add(p) + s.commit() + s.close() + await ctx.message.add_reaction("\U0001f44d") + + @pcp_group.group("unset", pass_context=True) + async def pcp_group_unset(self, ctx: commands.Context): + s = db.Session() + p = s.query(db.PCP).get(ctx.guild.id) + if not p: + s.close() + raise BadArgument() + s.delete(p) + s.commit() + s.close() + await ctx.message.add_reaction("\U0001f44d") + @pcp_group.group("subject", pass_context=True) async def pcp_group_subject(self, ctx: commands.Context): if ctx.invoked_subcommand is None: