Skip to content

Commit 69682dc

Browse files
committed
Did the following:
* Removed the bare exception and instead return None upon IndexError * Wrap the embed sending and reaction listening in a try/catch block & added a timeout variable set to 10 * Clear reactions to that message upon TimeoutError
1 parent 6ebfb85 commit 69682dc

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

modules/github.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""
2323
from __future__ import annotations
2424

25+
import asyncio
2526
import re
2627
from typing import TYPE_CHECKING
2728

@@ -51,6 +52,7 @@ class GitHub(core.Cog):
5152
def __init__(self, bot: Bot) -> None:
5253
self.bot: Bot = bot
5354
self.code_highlight_emoji = "📃"
55+
self.highlight_timeout = 10
5456

5557
def _strip_content_path(self, url: str) -> str:
5658
file_path = url[len(GITHUB_BASE_URL):]
@@ -59,11 +61,12 @@ def _strip_content_path(self, url: str) -> str:
5961
async def format_highlight_block(self, url: str, line_adjustment: int = 10):
6062
try:
6163
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
6467

6568
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
6770

6871
code = ""
6972
async with aiohttp.ClientSession() as session:
@@ -106,15 +109,16 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10):
106109

107110
msg += highlighted_msg_format
108111
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
110114
msg += display_str.format(currLineNum, line_list.get(key))
111115
key += 1
112116

113117
msg += "\n```"
114118

115119
github_dict = {
116120
"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
118122
"max": _maxBoundary,
119123
"msg": msg
120124
}
@@ -133,34 +137,31 @@ async def on_message(self, message: discord.Message) -> None:
133137

134138
await message.channel.send(GITHUB_ISSUE_URL.format(lib, issue))
135139

136-
codeSegment = await self.format_highlight_block(message.content)
140+
code_segment = await self.format_highlight_block(message.content)
137141

138-
if codeSegment is None:
142+
if code_segment is None:
139143
return
140144

141145
await message.add_reaction(self.code_highlight_emoji)
142146

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']
147151

148152
def check(reaction, user):
149153
return reaction.emoji == self.code_highlight_emoji and user != self.bot.user \
150154
and message.id == reaction.message.id
151155

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()
164165

165166

166167
async def setup(bot: Bot) -> None:

0 commit comments

Comments
 (0)