Skip to content

Commit 1b86b70

Browse files
committed
Added docstring to utils.py
1 parent e852292 commit 1b86b70

File tree

1 file changed

+100
-3
lines changed

1 file changed

+100
-3
lines changed

core/utils.py

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88

99
class User(commands.IDConverter):
10+
"""
11+
A custom discord.py `Converter` that
12+
supports `Member`, `User`, and string ID's.
13+
"""
14+
1015
# noinspection PyCallByClass,PyTypeChecker
1116
async def convert(self, ctx, argument):
1217
try:
@@ -24,10 +29,39 @@ async def convert(self, ctx, argument):
2429

2530

2631
def truncate(text: str, max: int = 50) -> str:
32+
"""
33+
Reduces the string to `max` length, by trimming the message into "...".
34+
35+
Parameters
36+
----------
37+
text : str
38+
The text to trim.
39+
max : int, optional
40+
The max length of the text.
41+
Defaults to 50.
42+
43+
Returns
44+
-------
45+
str
46+
The truncated text.
47+
"""
2748
return text[:max-3].strip() + '...' if len(text) > max else text
2849

2950

30-
def format_preview(messages):
51+
def format_preview(messages: typing.List[typing.Dict[str, typing.Any]]):
52+
"""
53+
Used to format previews.
54+
55+
Parameters
56+
----------
57+
messages : List[Dict[str, Any]]
58+
A list of messages.
59+
60+
Returns
61+
-------
62+
str
63+
A formatted string preview.
64+
"""
3165
messages = messages[:3]
3266
out = ''
3367
for message in messages:
@@ -43,11 +77,36 @@ def format_preview(messages):
4377

4478

4579
def is_image_url(url: str, _=None) -> bool:
80+
"""
81+
Check if the URL is pointing to an image.
82+
83+
Parameters
84+
----------
85+
url : str
86+
The URL to check.
87+
88+
Returns
89+
-------
90+
bool
91+
Whether the URL is a valid image URL.
92+
"""
4693
return bool(parse_image_url(url))
4794

4895

4996
def parse_image_url(url: str) -> str:
50-
"""Checks if a url leads to an image."""
97+
"""
98+
Convert the image URL into a sized Discord avatar.
99+
100+
Parameters
101+
----------
102+
url : str
103+
The URL to convert.
104+
105+
Returns
106+
-------
107+
str
108+
The converted URL, or '' if the URL isn't in the proper format.
109+
"""
51110
types = ['.png', '.jpg', '.gif', '.jpeg', '.webp']
52111
url = parse.urlsplit(url)
53112

@@ -57,14 +116,39 @@ def parse_image_url(url: str) -> str:
57116

58117

59118
def days(day: typing.Union[str, int]) -> str:
119+
"""
120+
Humanize the number of days.
121+
122+
Parameters
123+
----------
124+
day: Union[int, str]
125+
The number of days passed.
126+
127+
Returns
128+
-------
129+
str
130+
A formatted string of the number of days passed.
131+
"""
60132
day = int(day)
61133
if day == 0:
62134
return '**today**'
63135
return f'{day} day ago' if day == 1 else f'{day} days ago'
64136

65137

66138
def cleanup_code(content: str) -> str:
67-
"""Automatically removes code blocks from the code."""
139+
"""
140+
Automatically removes code blocks from the code.
141+
142+
Parameters
143+
----------
144+
content : str
145+
The content to be cleaned.
146+
147+
Returns
148+
-------
149+
str
150+
The cleaned content.
151+
"""
68152
# remove ```py\n```
69153
if content.startswith('```') and content.endswith('```'):
70154
return '\n'.join(content.split('\n')[1:-1])
@@ -74,6 +158,19 @@ def cleanup_code(content: str) -> str:
74158

75159

76160
def match_user_id(text: str) -> int:
161+
"""
162+
Matches a user ID in the format of "User ID: 12345".
163+
164+
Parameters
165+
----------
166+
text : str
167+
The text of the user ID.
168+
169+
Returns
170+
-------
171+
int
172+
The user ID if found. Otherwise, -1.
173+
"""
77174
match = re.match(r'^User ID: (\d+)$', text)
78175
if match is not None:
79176
return int(match.group(1))

0 commit comments

Comments
 (0)