From d9776cfa2f22ef4c1bb2c1b85060710cdf2d1f8a Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 5 Nov 2020 00:24:13 +0100 Subject: [PATCH] Use regex on database instead of hardcoded one and add group set and unset command --- db/PCP.py | 15 ++++++++ db/__init__.py | 1 + extensions/pcp.py | 96 +++++++++++++++++++++++++++++++++++------------ 3 files changed, 87 insertions(+), 25 deletions(-) create mode 100644 db/PCP.py 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 b8939f2..6607103 100644 --- a/extensions/pcp.py +++ b/extensions/pcp.py @@ -2,15 +2,14 @@ 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 DEVOPS2?|LP ESSIR|LP SID)$") -change_group_role_re = re.compile(r"^(SANS CLASSE|NOUVEAU VENU)$") 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]+>$") @@ -27,37 +26,49 @@ 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) - def roles() -> list: - return list(filter( - lambda r: group_re.fullmatch(r.name.upper()) or change_group_role_re.fullmatch(r.name.upper()), - ctx.author.roles - )) + 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()): + 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 - 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) - 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 not embed.fields: + raise MissingPermissions(None) await ctx.send(embed=embed) @pcp.group("pin", pass_context=True) @@ -88,14 +99,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: @@ -108,6 +128,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: