24
24
25
25
import asyncio
26
26
import re
27
+ from enum import Enum
27
28
28
29
import discord
29
30
31
+ import constants
30
32
import core
31
33
32
34
33
35
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 )
35
37
GITHUB_CODE_REGION_REGEX = re .compile (
36
38
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]+)?"
37
39
)
38
40
39
41
GITHUB_BASE_URL = "https://github.com/"
40
42
GITHUB_RAW_CONTENT_URL = "https://raw.githubusercontent.com/"
41
43
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
+
42
53
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 ),
46
59
]
47
60
48
61
LIB_REPO_MAPPING = {key : value for keys , value in aliases for key in keys }
@@ -52,6 +65,7 @@ class GitHub(core.Cog):
52
65
def __init__ (self , bot : core .Bot ) -> None :
53
66
self .bot = bot
54
67
self .code_highlight_emoji = "📃"
68
+ self .bruhkitty = "<:bruhkitty:710507405347389451>"
55
69
self .highlight_timeout = 10
56
70
57
71
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
160
174
161
175
return github_dict
162
176
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
+
163
203
@core .Cog .listener ()
164
204
async def on_message (self , message : discord .Message ) -> None :
165
205
if message .author .bot :
166
206
return
167
207
168
208
# Check if we can find a valid issue format: lib##number | ##number
169
209
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 )
173
215
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 )
175
223
176
224
code_segment = await self .format_highlight_block (message .content )
177
225
0 commit comments