diff options
Diffstat (limited to 'arthur')
| -rw-r--r-- | arthur/config.py | 3 | ||||
| -rw-r--r-- | arthur/exts/fun/devops_rules.py | 69 | 
2 files changed, 9 insertions, 63 deletions
diff --git a/arthur/config.py b/arthur/config.py index ddfa479..9c02743 100644 --- a/arthur/config.py +++ b/arthur/config.py @@ -25,8 +25,5 @@ class Config(      # Guild id      guild_id: int = 267624335836053506 -    # Token for authorising with the Notion API -    notion_api_token: str | None = None -  CONFIG = Config() diff --git a/arthur/exts/fun/devops_rules.py b/arthur/exts/fun/devops_rules.py index 66b0078..cbfb6ce 100644 --- a/arthur/exts/fun/devops_rules.py +++ b/arthur/exts/fun/devops_rules.py @@ -1,51 +1,11 @@  """The rules all devops members must follow.""" -from typing import TypedDict -  import discord  from discord.ext.commands import Cog, Context, Greedy, group -from arthur.bot import KingArthur, logger -from arthur.config import CONFIG - -NOTION_API_BASE_URL = "https://api.notion.com/v1" -DEVOPS_RULES_PAGE_CONTENT = ( -    f"{NOTION_API_BASE_URL}/blocks/149bc48f-6f79-47af-add8-036f11d4e9a7/children" -) -DISCORD_MARKDOWN_LOOKUP = { -    "bold": "**{}**", -    "italic": "_{}_", -    "strikethrough": "~~{}~~", -    "underline": "__{}__", -} - - -class NotionAnnotations(TypedDict): -    """The markdown annotations attached to a block of text in Notion.""" - -    bold: bool -    italic: bool -    strikethrough: bool -    underline: bool +from arthur.bot import KingArthur - -class NotionRichText(TypedDict): -    """A block of text with markdown annotations attached.""" - -    plain_text: str -    annotations: NotionAnnotations - - -def notion_block_to_discord_markdown(block: list[NotionRichText]) -> str: -    """Convert the given notion API "block" into Discord markdown text.""" -    block_string_parts = [] -    for rich_text_part in block: -        block_string_part = rich_text_part["plain_text"] -        for annotation, enabled in rich_text_part["annotations"].items(): -            if enabled and annotation in DISCORD_MARKDOWN_LOOKUP: -                block_string_part = DISCORD_MARKDOWN_LOOKUP[annotation].format(block_string_part) -        block_string_parts.append(block_string_part) -    return "".join(block_string_parts) +RULES_URL = "https://raw.githubusercontent.com/python-discord/infra/main/docs/onboarding/rules.md"  class Rules(Cog): @@ -57,20 +17,15 @@ class Rules(Cog):      async def cog_load(self) -> None:          """Fetch Devops rules from notion of cog load.""" -        headers = { -            "Authorization": f"Bearer {CONFIG.notion_api_token}", -            "accept": "application/json", -            "Notion-Version": "2022-06-28", -        } -        async with self.bot.http_session.get(DEVOPS_RULES_PAGE_CONTENT, headers=headers) as resp: +        async with self.bot.http_session.get(RULES_URL) as resp:              resp.raise_for_status() -            page_content = await resp.json() +            raw_content = await resp.text() +        parsed_content = raw_content.split("---")[-1].strip().split("\n") -        self.rules = { -            i: notion_block_to_discord_markdown(block["numbered_list_item"]["rich_text"]) -            for i, block in enumerate(page_content["results"], 1) -            if block.get("type") == "numbered_list_item" -        } +        self.rules = {} +        for line in parsed_content: +            number, rule = line.split(".", maxsplit=1) +            self.rules[number] = rule.strip()      @group(name="rules", aliases=("rule",))      async def rules_group(self, ctx: Context, rules: Greedy[int]) -> None: @@ -104,10 +59,4 @@ class Rules(Cog):  async def setup(bot: KingArthur) -> None:      """Add cog to bot.""" -    if not CONFIG.notion_api_token: -        logger.info( -            f"Not loading {__name__} as env var " -            f"{CONFIG.Config.env_prefix}NOTION_API_TOKEN is not set." -        ) -        return      await bot.add_cog(Rules(bot))  |