aboutsummaryrefslogtreecommitdiffstats
path: root/arthur/exts/fun/devops_rules.py
blob: eeb54436364374061f59541ca33235793b9d02da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""The rules all devops members must follow."""

import discord
from discord.ext.commands import Cog, Context, Greedy, MissingRole, group

from arthur.bot import KingArthur
from arthur.config import CONFIG

RULES_URL = (
    "https://raw.githubusercontent.com/python-discord/infra/main/docs/docs/onboarding/rules.md"
)


class Rules(Cog):
    """The rules all devops members must follow."""

    def __init__(self, bot: KingArthur) -> None:
        self.bot = bot
        self.rules: dict

    async def cog_load(self) -> None:
        """Fetch Devops rules from notion of cog load."""
        async with self.bot.http_session.get(RULES_URL) as resp:
            resp.raise_for_status()
            raw_content = await resp.text()
        # Ignore markdown frontmatter
        parsed_content = raw_content.split("---")[-1].strip()
        # Ignore first 4 lines, as they are not the rules
        parsed_content = parsed_content.split("\n")[4:]
        self.rules = {}
        for line in parsed_content:
            number, rule = line.split(".", maxsplit=1)
            self.rules[int(number)] = rule.strip()

    @group(name="rules", aliases=("rule",), invoke_without_command=True)
    async def rules_group(self, ctx: Context, rules: Greedy[int]) -> None:
        """List the requested rule(s), or all of them if not defined."""
        role_ids = {r.id for r in ctx.author.roles}
        if CONFIG.helpers_role not in role_ids:
            raise MissingRole(CONFIG.helpers_role)
        if CONFIG.devops_role not in role_ids:
            rules = [4]

        if rules:
            output_rules = set(rules) & set(self.rules.keys())
        else:
            output_rules = self.rules.keys()

        if not output_rules:
            await ctx.send(f":x: Rule{'s'[: len(rules) ^ 1]} not found.")
            return

        output = "\n".join(
            f"{key}: {value}" for key, value in self.rules.items() if key in output_rules
        )
        await ctx.send(
            embed=discord.Embed(
                title=f"Rule{'s'[: len(output_rules) ^ 1]}",
                description=output,
                colour=discord.Colour.og_blurple(),
                url="https://docs.pydis.wtf/onboarding/rules.html",
            )
        )

    @rules_group.command(name="refresh", aliases=("fetch", "update"))
    async def update_rules(self, ctx: Context) -> None:
        """Re-fetch the list of rules from notion."""
        await self.cog_load()
        await ctx.reply(":+1:")


async def setup(bot: KingArthur) -> None:
    """Add cog to bot."""
    await bot.add_cog(Rules(bot))