22
22
"""
23
23
from __future__ import annotations
24
24
25
+ import asyncio
25
26
import re
26
27
from typing import TYPE_CHECKING
27
28
@@ -51,6 +52,7 @@ class GitHub(core.Cog):
51
52
def __init__ (self , bot : Bot ) -> None :
52
53
self .bot : Bot = bot
53
54
self .code_highlight_emoji = "📃"
55
+ self .highlight_timeout = 10
54
56
55
57
def _strip_content_path (self , url : str ) -> str :
56
58
file_path = url [len (GITHUB_BASE_URL ):]
@@ -59,11 +61,12 @@ def _strip_content_path(self, url: str) -> str:
59
61
async def format_highlight_block (self , url : str , line_adjustment : int = 10 ):
60
62
try :
61
63
highlighted_line = int (url .split ("#L" )[1 ]) # seperate the #L{n} highlight
62
- except :
63
- return None
64
+ except Exception as err :
65
+ if isinstance (err , IndexError ):
66
+ return None
64
67
65
68
file_path = self ._strip_content_path (url )
66
- raw_url = GITHUB_RAW_CONTENT_URL + file_path .replace ("blob/" , "" ) # Convert it to a raw user content URL
69
+ raw_url = GITHUB_RAW_CONTENT_URL + file_path .replace ("blob/" , "" ) # Convert it to a raw user content URL
67
70
68
71
code = ""
69
72
async with aiohttp .ClientSession () as session :
@@ -106,15 +109,16 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10):
106
109
107
110
msg += highlighted_msg_format
108
111
else :
109
- display_str = "{} {}\n " if line_list .get (key ) is not None else "" # if we hit the end of the file, just write an empty string
112
+ display_str = "{} {}\n " if line_list .get (
113
+ key ) is not None else "" # if we hit the end of the file, just write an empty string
110
114
msg += display_str .format (currLineNum , line_list .get (key ))
111
115
key += 1
112
116
113
117
msg += "\n ```"
114
118
115
119
github_dict = {
116
120
"path" : file_path ,
117
- "min" : _minBoundary if _minBoundary > 0 else highlighted_line , # Do not display negative numbers if <0
121
+ "min" : _minBoundary if _minBoundary > 0 else highlighted_line , # Do not display negative numbers if <0
118
122
"max" : _maxBoundary ,
119
123
"msg" : msg
120
124
}
@@ -133,34 +137,31 @@ async def on_message(self, message: discord.Message) -> None:
133
137
134
138
await message .channel .send (GITHUB_ISSUE_URL .format (lib , issue ))
135
139
136
- codeSegment = await self .format_highlight_block (message .content )
140
+ code_segment = await self .format_highlight_block (message .content )
137
141
138
- if codeSegment is None :
142
+ if code_segment is None :
139
143
return
140
144
141
145
await message .add_reaction (self .code_highlight_emoji )
142
146
143
- path = codeSegment ['path' ]
144
- _min = codeSegment ['min' ]
145
- _max = codeSegment ['max' ]
146
- code_fmt = codeSegment ['msg' ]
147
+ path = code_segment ['path' ]
148
+ _min = code_segment ['min' ]
149
+ _max = code_segment ['max' ]
150
+ code_fmt = code_segment ['msg' ]
147
151
148
152
def check (reaction , user ):
149
153
return reaction .emoji == self .code_highlight_emoji and user != self .bot .user \
150
154
and message .id == reaction .message .id
151
155
152
- await self .bot .wait_for ("reaction_add" , check = check )
153
-
154
- code_display_msg = await message .channel .send (
155
- content = "Showing lines `{}` - `{}` in: `{}`...\n {}" .format (_min , _max , path , code_fmt ),
156
- suppress_embeds = True
157
- )
158
-
159
- await self .bot .wait_for ("reaction_remove" , check = check )
160
- await code_display_msg .delete ()
161
-
162
- # clean up reactions
163
- await message .clear_reactions ()
156
+ try :
157
+ await self .bot .wait_for ("reaction_add" , check = check , timeout = self .highlight_timeout )
158
+ await message .channel .send (
159
+ content = "Showing lines `{}` - `{}` in: `{}`...\n {}" .format (_min , _max , path , code_fmt ),
160
+ suppress_embeds = True
161
+ )
162
+
163
+ except asyncio .TimeoutError :
164
+ await message .clear_reactions ()
164
165
165
166
166
167
async def setup (bot : Bot ) -> None :
0 commit comments