|
| 1 | +from __future__ import annotations |
| 2 | +import re |
| 3 | +from typing import List |
| 4 | +from nextcord.ext import commands |
| 5 | +import nextcord |
| 6 | +import aiohttp |
| 7 | + |
| 8 | + |
| 9 | +async def request(*args, **kwargs): |
| 10 | + async with aiohttp.ClientSession() as session: |
| 11 | + async with session.request(*args, **kwargs) as ans: |
| 12 | + return await ans.json() |
| 13 | + |
| 14 | + |
| 15 | +# so my LSP know it |
| 16 | + |
| 17 | +request.__doc__ = aiohttp.ClientSession.request.__doc__ |
| 18 | + |
| 19 | + |
| 20 | +FULL_MATCH_ANY_REPO = r"(https:\/\/github.com\/)?([A-Za-z1-9-]+)\/([A-Za-z1-9-]+)(\/pull)?(#|\/)(?P<pr_id>\d+)" |
| 21 | + |
| 22 | + |
| 23 | +MATCH_IS_A_DEV_ONLY = r"register#(\d+)" |
| 24 | +PR_CHANNEL_ID = 1130858271620726784 |
| 25 | +STAFF_ROLE_ID = 1197475623745110109 |
| 26 | + |
| 27 | + |
| 28 | +class _PRRawObject(object): |
| 29 | + def __init__(self, *, repo_owner: str, repo_name: str, pr_id: str) -> None: |
| 30 | + self.repo_owner = repo_owner |
| 31 | + self.repo_name = repo_name |
| 32 | + self.pr_id = pr_id |
| 33 | + |
| 34 | + |
| 35 | +class GitHub(commands.Cog): |
| 36 | + @commands.Cog.listener() |
| 37 | + async def on_message(self, message: nextcord.Message): |
| 38 | + # if message.channel.id != PR_CHANNEL_ID: return |
| 39 | + if message.author.bot: |
| 40 | + return |
| 41 | + full_matches: List[re.Match] = re.findall(FULL_MATCH_ANY_REPO, message.content) |
| 42 | + print(full_matches) |
| 43 | + is_a_dev_matches: List[re.Match] = re.findall( |
| 44 | + MATCH_IS_A_DEV_ONLY, message.content |
| 45 | + ) |
| 46 | + pr_list: List[_PRRawObject] = [] |
| 47 | + if len(full_matches) > 0: |
| 48 | + for x in full_matches: |
| 49 | + print(x) |
| 50 | + repo_owner = x[1] |
| 51 | + repo_name = x[2] |
| 52 | + pr_id = x[5] |
| 53 | + pr_list.append( |
| 54 | + _PRRawObject( |
| 55 | + repo_owner=repo_owner, repo_name=repo_name, pr_id=pr_id |
| 56 | + ) |
| 57 | + ) |
| 58 | + # TODO: Fix is-a-dev-only regex |
| 59 | + |
| 60 | + # elif len(is_a_dev_matches) > 0: |
| 61 | + # for x in is_a_dev_matches: |
| 62 | + # repo_owner = "is-a-dev" |
| 63 | + # repo_name = "register" |
| 64 | + # pr_id = x[0] |
| 65 | + # pr_list.append( |
| 66 | + # _PRRawObject( |
| 67 | + # repo_owner=repo_owner, repo_name=repo_name, pr_id=pr_id |
| 68 | + # ) |
| 69 | + # ) |
| 70 | + |
| 71 | + else: |
| 72 | + if message.channel.id == PR_CHANNEL_ID: |
| 73 | + if message.author.get_role(STAFF_ROLE_ID) is None: |
| 74 | + await message.delete() |
| 75 | + |
| 76 | + return |
| 77 | + embed_description = """""" |
| 78 | + for pr in pr_list: |
| 79 | + i = await request( |
| 80 | + "GET", |
| 81 | + f"https://api.github.com/repos/{pr.repo_owner}/{pr.repo_name}/issues/{pr.pr_id}", |
| 82 | + ) |
| 83 | + embed_description += f"[(#{pr.pr_id}) {i['title']}]({i[ |
| 84 | + 'html_url']})" |
| 85 | + |
| 86 | + await message.channel.send( |
| 87 | + embed=nextcord.Embed( |
| 88 | + title="PR/Issue", |
| 89 | + description=embed_description, |
| 90 | + color=nextcord.Color.from_rgb(136,225,180), |
| 91 | + ) |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def setup(bot: commands.Bot) -> None: |
| 96 | + bot.add_cog(GitHub()) |
0 commit comments