7
7
8
8
9
9
class User (commands .IDConverter ):
10
+ """
11
+ A custom discord.py `Converter` that
12
+ supports `Member`, `User`, and string ID's.
13
+ """
14
+
10
15
# noinspection PyCallByClass,PyTypeChecker
11
16
async def convert (self , ctx , argument ):
12
17
try :
@@ -24,10 +29,39 @@ async def convert(self, ctx, argument):
24
29
25
30
26
31
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
+ """
27
48
return text [:max - 3 ].strip () + '...' if len (text ) > max else text
28
49
29
50
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
+ """
31
65
messages = messages [:3 ]
32
66
out = ''
33
67
for message in messages :
@@ -43,11 +77,36 @@ def format_preview(messages):
43
77
44
78
45
79
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
+ """
46
93
return bool (parse_image_url (url ))
47
94
48
95
49
96
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
+ """
51
110
types = ['.png' , '.jpg' , '.gif' , '.jpeg' , '.webp' ]
52
111
url = parse .urlsplit (url )
53
112
@@ -57,14 +116,39 @@ def parse_image_url(url: str) -> str:
57
116
58
117
59
118
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
+ """
60
132
day = int (day )
61
133
if day == 0 :
62
134
return '**today**'
63
135
return f'{ day } day ago' if day == 1 else f'{ day } days ago'
64
136
65
137
66
138
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
+ """
68
152
# remove ```py\n```
69
153
if content .startswith ('```' ) and content .endswith ('```' ):
70
154
return '\n ' .join (content .split ('\n ' )[1 :- 1 ])
@@ -74,6 +158,19 @@ def cleanup_code(content: str) -> str:
74
158
75
159
76
160
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
+ """
77
174
match = re .match (r'^User ID: (\d+)$' , text )
78
175
if match is not None :
79
176
return int (match .group (1 ))
0 commit comments