Skip to content

Commit

Permalink
update libfinder logic for ##num shorthand (#58)
Browse files Browse the repository at this point in the history
* update libfinder logic for ##num shorthand

* run lint
  • Loading branch information
IAmTomahawkx authored Oct 18, 2023
1 parent 22fe178 commit 8d3b308
Showing 1 changed file with 56 additions and 8 deletions.
64 changes: 56 additions & 8 deletions modules/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,38 @@

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<lib>[a-z]+)?##(?P<number>[0-9]+)", flags=re.IGNORECASE)
LIB_ISSUE_REGEX = re.compile(r"(?P<lib>[a-z]+)?(?P<pounds>#{2,})(?P<number>[0-9]+)", flags=re.IGNORECASE)
GITHUB_CODE_REGION_REGEX = re.compile(
r"https?://github\.com/(?P<user>.*)/(?P<repo>.*)/blob/(?P<hash>[a-zA-Z0-9]+)/(?P<path>.*)/(?P<file>.*)(?:\#L)(?P<linestart>[0-9]+)(?:-L)?(?P<lineend>[0-9]+)?"
)

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}
Expand All @@ -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:
Expand Down Expand Up @@ -160,18 +174,52 @@ 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:
return

# 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)

Expand Down

0 comments on commit 8d3b308

Please sign in to comment.