diff --git a/modules/github.py b/modules/github.py index 400dd38..7d466fa 100644 --- a/modules/github.py +++ b/modules/github.py @@ -24,14 +24,16 @@ import asyncio import re +from enum import Enum import discord +import constants import core GITHUB_ISSUE_URL = "https://github.com/{}/issues/{}" -LIB_ISSUE_REGEX = re.compile(r"(?P[a-z]+)?##(?P[0-9]+)", flags=re.IGNORECASE) +LIB_ISSUE_REGEX = re.compile(r"(?P[a-z]+)?(?P#{2,})(?P[0-9]+)", flags=re.IGNORECASE) GITHUB_CODE_REGION_REGEX = re.compile( r"https?://github\.com/(?P.*)/(?P.*)/blob/(?P[a-zA-Z0-9]+)/(?P.*)/(?P.*)(?:\#L)(?P[0-9]+)(?:-L)?(?P[0-9]+)?" ) @@ -39,10 +41,21 @@ GITHUB_BASE_URL = "https://github.com/" GITHUB_RAW_CONTENT_URL = "https://raw.githubusercontent.com/" + +class LibEnum(Enum): + wavelink = "PythonistaGuild/Wavelink" + twitchio = "PythonistaGuild/TwitchIO" + pythonistaBot = "PythonistaGuild/PythonistaBot" + mystbin = "PythonistaGuild/Mystbin" + discordpy = "rapptz/discord.py" + + aliases = [ - (("wavelink", "wave", "wl"), "PythonistaGuild/Wavelink"), - (("discordpy", "discord", "dpy"), "Rapptz/discord.py"), - (("twitchio", "tio"), "TwitchIO/TwitchIO"), + (("wavelink", "wave", "wl"), LibEnum.wavelink), + (("discordpy", "discord", "dpy"), LibEnum.discordpy), + (("twitchio", "tio"), LibEnum.twitchio), + (("mystbin", "mb"), LibEnum.mystbin), + (("pythonistabot", "pb"), LibEnum.pythonistaBot), ] LIB_REPO_MAPPING = {key: value for keys, value in aliases for key in keys} @@ -52,6 +65,7 @@ class GitHub(core.Cog): def __init__(self, bot: core.Bot) -> None: self.bot = bot self.code_highlight_emoji = "📃" + self.bruhkitty = "<:bruhkitty:710507405347389451>" self.highlight_timeout = 10 def _strip_content_path(self, url: str) -> str: @@ -160,6 +174,32 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10) -> d return github_dict + def _smart_guess_lib(self, msg: discord.Message) -> LibEnum | None: + # this is mostly the same as the function in manuals.py, however the enum is entirely different so the code isn't reusable. + assert msg.channel + + if msg.channel.id == constants.Channels.HELP_CHANNEL: + return None # there's not much hope here, stay quick + + if isinstance(msg.channel, discord.Thread) and msg.channel.parent_id == constants.Channels.HELP_FORUM: + tags = set(x.name for x in msg.channel.applied_tags) + + if "twitchio-help" in tags: + return LibEnum.twitchio + elif "wavelink-help" in tags: + return LibEnum.wavelink + elif "discord.py-help" in tags: + return LibEnum.discordpy + + return None + + if msg.channel.id == constants.Channels.WAVELINK_DEV: + return LibEnum.wavelink + elif msg.channel.id == constants.Channels.TWITCHIO_DEV: + return LibEnum.twitchio + + return None + @core.Cog.listener() async def on_message(self, message: discord.Message) -> None: if message.author.bot: @@ -167,11 +207,19 @@ async def on_message(self, message: discord.Message) -> None: # Check if we can find a valid issue format: lib##number | ##number match = LIB_ISSUE_REGEX.search(message.content) - if match: - lib = LIB_REPO_MAPPING.get(match.group("lib"), "PythonistaGuild/Pythonista-Bot") - issue = match.group("number") + if match and len(match.group("pounds")) == 2: + lib = LIB_REPO_MAPPING.get(match.group("lib"), None) + + if not lib: + lib = self._smart_guess_lib(message) - await message.channel.send(GITHUB_ISSUE_URL.format(lib, issue)) + if lib: # no, this should not be an else, as lib can be reassigned in the previous block + issue = match.group("number") + + await message.channel.send(GITHUB_ISSUE_URL.format(lib.value, issue)) + + else: + await message.add_reaction(self.bruhkitty) code_segment = await self.format_highlight_block(message.content)