diff --git a/_orangcbot/__main__.py b/_orangcbot/__main__.py index 70f21ce..cd7e5c8 100644 --- a/_orangcbot/__main__.py +++ b/_orangcbot/__main__.py @@ -67,7 +67,7 @@ async def on_command_error( bot.load_extension("extensions.delete_response") bot.load_extension("extensions.nixwiki") bot.load_extension("extensions.archwiki") - +bot.load_extension("extensions.github") if os.getenv("HASDB"): bot.load_extension("extensions.tags_reworked") # bot.load_extension("extensions.forum") diff --git a/_orangcbot/extensions/dns.py b/_orangcbot/extensions/dns.py index d418348..e8657e8 100644 --- a/_orangcbot/extensions/dns.py +++ b/_orangcbot/extensions/dns.py @@ -4,6 +4,7 @@ from typing import TYPE_CHECKING import nextcord +import whois from dns import resolver as _dnsresolver from nextcord.ext import commands @@ -49,6 +50,8 @@ def __init__(self, url: str): DigSelectOption("AAAA"), DigSelectOption("MX"), DigSelectOption("TXT"), + DigSelectOption("SRV"), + DigSelectOption("PTR"), ] self._url: str = url super().__init__(options=options, placeholder="What records do you want?") @@ -99,7 +102,7 @@ class DNS(commands.Cog): def __init__(self, bot: commands.Bot) -> None: self._bot: commands.Bot = bot - @commands.command() + @commands.command(aliases=["dns"]) async def dig(self, ctx: commands.Context, url: str): """Dig an URL for its DNS records. Default to CNAME, if you want other then select in the dropdown.""" try: @@ -114,6 +117,18 @@ async def dig(self, ctx: commands.Context, url: str): msg = await ctx.send(embed=construct_embed(url, answer, "CNAME"), view=k) k.update_msg(msg) + @commands.command() + async def urlwhois(self, ctx: commands.Context, url: str) -> None: + """Dig an URL for WHOIS info.""" + r = whois.whois(url) + await ctx.send( + embed=nextcord.Embed( + title=f"WHOIS info for {url}", + description=r.text, + color=nextcord.Color.random(), + ) + ) + def setup(bot: commands.Bot) -> None: bot.add_cog(DNS(bot)) diff --git a/_orangcbot/extensions/github.py b/_orangcbot/extensions/github.py new file mode 100644 index 0000000..a689c40 --- /dev/null +++ b/_orangcbot/extensions/github.py @@ -0,0 +1,99 @@ +from __future__ import annotations + +import re +from typing import List + +import aiohttp +import nextcord +from nextcord.ext import commands + + +async def request(*args, **kwargs): + async with aiohttp.ClientSession() as session: + async with session.request(*args, **kwargs) as ans: + return await ans.json() + + +# so my LSP know it + +request.__doc__ = aiohttp.ClientSession.request.__doc__ + + +FULL_MATCH_ANY_REPO = r"(https:\/\/github.com\/)?([A-Za-z1-9-]+)\/([A-Za-z1-9-]+)(\/pull)?(#|\/)(?P\d+)" + + +MATCH_IS_A_DEV_ONLY = r"register#(\d+)" +PR_CHANNEL_ID = 1130858271620726784 +STAFF_ROLE_ID = 1197475623745110109 + + +class _PRRawObject(object): + def __init__(self, *, repo_owner: str, repo_name: str, pr_id: str) -> None: + self.repo_owner = repo_owner + self.repo_name = repo_name + self.pr_id = pr_id + + +class GitHub(commands.Cog): + @commands.Cog.listener() + async def on_message(self, message: nextcord.Message): + # if message.channel.id != PR_CHANNEL_ID: return + if message.author.bot: + return + full_matches: List[re.Match] = re.findall(FULL_MATCH_ANY_REPO, message.content) + print(full_matches) + is_a_dev_matches: List[re.Match] = re.findall( + MATCH_IS_A_DEV_ONLY, message.content + ) + pr_list: List[_PRRawObject] = [] + if len(full_matches) > 0: + for x in full_matches: + print(x) + repo_owner = x[1] + repo_name = x[2] + pr_id = x[5] + pr_list.append( + _PRRawObject( + repo_owner=repo_owner, repo_name=repo_name, pr_id=pr_id + ) + ) + # TODO: Fix is-a-dev-only regex + + # elif len(is_a_dev_matches) > 0: + # for x in is_a_dev_matches: + # repo_owner = "is-a-dev" + # repo_name = "register" + # pr_id = x[0] + # pr_list.append( + # _PRRawObject( + # repo_owner=repo_owner, repo_name=repo_name, pr_id=pr_id + # ) + # ) + + else: + if message.channel.id == PR_CHANNEL_ID: + if message.author.get_role(STAFF_ROLE_ID) is None: + await message.delete() + + return + if message.channel.id == PR_CHANNEL_ID: + await message.edit(suppress=True) + embed_description = """""" + for pr in pr_list: + i = await request( + "GET", + f"https://api.github.com/repos/{pr.repo_owner}/{pr.repo_name}/issues/{pr.pr_id}", + ) + embed_description += f"[(#{pr.pr_id}) {i['title']}]({i[ + 'html_url']})\n" + await message.channel.send( + embed=nextcord.Embed( + title="PR/Issue", + description=embed_description, + color=nextcord.Color.from_rgb(136, 225, 180), + ) + ) + + +def setup(bot: commands.Bot) -> None: + bot.add_cog(GitHub()) diff --git a/requirements.txt b/requirements.txt index 447aafe..c90967b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ aiohttp psycopg2-binary nextcord-ext-help-commands nextcord-ext-menus +python-whois