Skip to content

Commit 8d3b308

Browse files
authored
update libfinder logic for ##num shorthand (#58)
* update libfinder logic for ##num shorthand * run lint
1 parent 22fe178 commit 8d3b308

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

modules/github.py

+56-8
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,38 @@
2424

2525
import asyncio
2626
import re
27+
from enum import Enum
2728

2829
import discord
2930

31+
import constants
3032
import core
3133

3234

3335
GITHUB_ISSUE_URL = "https://github.com/{}/issues/{}"
34-
LIB_ISSUE_REGEX = re.compile(r"(?P<lib>[a-z]+)?##(?P<number>[0-9]+)", flags=re.IGNORECASE)
36+
LIB_ISSUE_REGEX = re.compile(r"(?P<lib>[a-z]+)?(?P<pounds>#{2,})(?P<number>[0-9]+)", flags=re.IGNORECASE)
3537
GITHUB_CODE_REGION_REGEX = re.compile(
3638
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]+)?"
3739
)
3840

3941
GITHUB_BASE_URL = "https://github.com/"
4042
GITHUB_RAW_CONTENT_URL = "https://raw.githubusercontent.com/"
4143

44+
45+
class LibEnum(Enum):
46+
wavelink = "PythonistaGuild/Wavelink"
47+
twitchio = "PythonistaGuild/TwitchIO"
48+
pythonistaBot = "PythonistaGuild/PythonistaBot"
49+
mystbin = "PythonistaGuild/Mystbin"
50+
discordpy = "rapptz/discord.py"
51+
52+
4253
aliases = [
43-
(("wavelink", "wave", "wl"), "PythonistaGuild/Wavelink"),
44-
(("discordpy", "discord", "dpy"), "Rapptz/discord.py"),
45-
(("twitchio", "tio"), "TwitchIO/TwitchIO"),
54+
(("wavelink", "wave", "wl"), LibEnum.wavelink),
55+
(("discordpy", "discord", "dpy"), LibEnum.discordpy),
56+
(("twitchio", "tio"), LibEnum.twitchio),
57+
(("mystbin", "mb"), LibEnum.mystbin),
58+
(("pythonistabot", "pb"), LibEnum.pythonistaBot),
4659
]
4760

4861
LIB_REPO_MAPPING = {key: value for keys, value in aliases for key in keys}
@@ -52,6 +65,7 @@ class GitHub(core.Cog):
5265
def __init__(self, bot: core.Bot) -> None:
5366
self.bot = bot
5467
self.code_highlight_emoji = "📃"
68+
self.bruhkitty = "<:bruhkitty:710507405347389451>"
5569
self.highlight_timeout = 10
5670

5771
def _strip_content_path(self, url: str) -> str:
@@ -160,18 +174,52 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10) -> d
160174

161175
return github_dict
162176

177+
def _smart_guess_lib(self, msg: discord.Message) -> LibEnum | None:
178+
# this is mostly the same as the function in manuals.py, however the enum is entirely different so the code isn't reusable.
179+
assert msg.channel
180+
181+
if msg.channel.id == constants.Channels.HELP_CHANNEL:
182+
return None # there's not much hope here, stay quick
183+
184+
if isinstance(msg.channel, discord.Thread) and msg.channel.parent_id == constants.Channels.HELP_FORUM:
185+
tags = set(x.name for x in msg.channel.applied_tags)
186+
187+
if "twitchio-help" in tags:
188+
return LibEnum.twitchio
189+
elif "wavelink-help" in tags:
190+
return LibEnum.wavelink
191+
elif "discord.py-help" in tags:
192+
return LibEnum.discordpy
193+
194+
return None
195+
196+
if msg.channel.id == constants.Channels.WAVELINK_DEV:
197+
return LibEnum.wavelink
198+
elif msg.channel.id == constants.Channels.TWITCHIO_DEV:
199+
return LibEnum.twitchio
200+
201+
return None
202+
163203
@core.Cog.listener()
164204
async def on_message(self, message: discord.Message) -> None:
165205
if message.author.bot:
166206
return
167207

168208
# Check if we can find a valid issue format: lib##number | ##number
169209
match = LIB_ISSUE_REGEX.search(message.content)
170-
if match:
171-
lib = LIB_REPO_MAPPING.get(match.group("lib"), "PythonistaGuild/Pythonista-Bot")
172-
issue = match.group("number")
210+
if match and len(match.group("pounds")) == 2:
211+
lib = LIB_REPO_MAPPING.get(match.group("lib"), None)
212+
213+
if not lib:
214+
lib = self._smart_guess_lib(message)
173215

174-
await message.channel.send(GITHUB_ISSUE_URL.format(lib, issue))
216+
if lib: # no, this should not be an else, as lib can be reassigned in the previous block
217+
issue = match.group("number")
218+
219+
await message.channel.send(GITHUB_ISSUE_URL.format(lib.value, issue))
220+
221+
else:
222+
await message.add_reaction(self.bruhkitty)
175223

176224
code_segment = await self.format_highlight_block(message.content)
177225

0 commit comments

Comments
 (0)