26
26
import re
27
27
from typing import TYPE_CHECKING
28
28
29
- import aiohttp
30
29
import discord
31
30
32
31
import core
33
32
33
+
34
34
if TYPE_CHECKING :
35
35
from bot import Bot
36
36
@@ -55,10 +55,10 @@ def __init__(self, bot: Bot) -> None:
55
55
self .highlight_timeout = 10
56
56
57
57
def _strip_content_path (self , url : str ) -> str :
58
- file_path = url [len (GITHUB_BASE_URL ):]
58
+ file_path = url [len (GITHUB_BASE_URL ) :]
59
59
return file_path
60
60
61
- async def format_highlight_block (self , url : str , line_adjustment : int = 10 ):
61
+ async def format_highlight_block (self , url : str , line_adjustment : int = 10 ) -> dict [ str , str | int ] | None :
62
62
try :
63
63
highlighted_line = int (url .split ("#L" )[1 ]) # seperate the #L{n} highlight
64
64
except IndexError :
@@ -76,7 +76,7 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10):
76
76
77
77
code = code .splitlines ()
78
78
79
- code_block_dict = {"lines" : {}}
79
+ code_block_dict : dict [ str , dict [ int , str ]] = {"lines" : {}}
80
80
j = 0
81
81
for i in code :
82
82
# populate the dict
@@ -91,8 +91,8 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10):
91
91
return None
92
92
93
93
bound_adj = line_adjustment # adjustment for upper and lower bound display
94
- _minBoundary = ( highlighted_line - 1 - bound_adj )
95
- _maxBoundary = ( highlighted_line - 1 + bound_adj )
94
+ _minBoundary = highlighted_line - 1 - bound_adj
95
+ _maxBoundary = highlighted_line - 1 + bound_adj
96
96
97
97
# loop through all the lines, and adjust the formatting
98
98
msg = "```ansi\n "
@@ -101,14 +101,13 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10):
101
101
currLineNum = str (key + 1 )
102
102
# insert a space if there is no following char before the first character...
103
103
if key + 1 == highlighted_line :
104
- highlighted_msg_format = "\u001b [0;37m\u001b [4;31m{} {}\u001b [0;0m\n " .format (
105
- currLineNum , line_list [key ]
106
- )
104
+ highlighted_msg_format = "\u001b [0;37m\u001b [4;31m{} {}\u001b [0;0m\n " .format (currLineNum , line_list [key ])
107
105
108
106
msg += highlighted_msg_format
109
107
else :
110
- display_str = "{} {}\n " if line_list .get (
111
- key ) is not None else "" # if we hit the end of the file, just write an empty string
108
+ display_str = (
109
+ "{} {}\n " if line_list .get (key ) is not None else ""
110
+ ) # if we hit the end of the file, just write an empty string
112
111
msg += display_str .format (currLineNum , line_list .get (key ))
113
112
key += 1
114
113
@@ -118,7 +117,7 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10):
118
117
"path" : file_path ,
119
118
"min" : _minBoundary if _minBoundary > 0 else highlighted_line , # Do not display negative numbers if <0
120
119
"max" : _maxBoundary ,
121
- "msg" : msg
120
+ "msg" : msg ,
122
121
}
123
122
return github_dict
124
123
@@ -142,20 +141,20 @@ async def on_message(self, message: discord.Message) -> None:
142
141
143
142
await message .add_reaction (self .code_highlight_emoji )
144
143
145
- path = code_segment [' path' ]
146
- _min = code_segment [' min' ]
147
- _max = code_segment [' max' ]
148
- code_fmt = code_segment [' msg' ]
144
+ path = code_segment [" path" ]
145
+ _min = code_segment [" min" ]
146
+ _max = code_segment [" max" ]
147
+ code_fmt = code_segment [" msg" ]
149
148
150
- def check (reaction , user ):
151
- return reaction .emoji == self .code_highlight_emoji and user != self .bot .user \
152
- and message .id == reaction .message .id
149
+ def check (reaction : discord .Reaction , user : discord .User ) -> bool :
150
+ return (
151
+ reaction .emoji == self .code_highlight_emoji and user != self .bot .user and message .id == reaction .message .id
152
+ )
153
153
154
154
try :
155
155
await self .bot .wait_for ("reaction_add" , check = check , timeout = self .highlight_timeout )
156
156
await message .channel .send (
157
- content = "Showing lines `{}` - `{}` in: `{}`...\n {}" .format (_min , _max , path , code_fmt ),
158
- suppress_embeds = True
157
+ content = "Showing lines `{}` - `{}` in: `{}`...\n {}" .format (_min , _max , path , code_fmt ), suppress_embeds = True
159
158
)
160
159
except asyncio .TimeoutError :
161
160
return
0 commit comments