-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Copy pathutils.py
375 lines (298 loc) · 8.96 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import base64
import functools
import re
import string
import typing
from difflib import get_close_matches
from distutils.util import strtobool as _stb # pylint: disable=import-error
from itertools import takewhile, zip_longest
from urllib import parse
import discord
from discord.ext import commands
__all__ = [
"strtobool",
"User",
"truncate",
"format_preview",
"is_image_url",
"parse_image_url",
"human_join",
"days",
"cleanup_code",
"match_user_id",
"create_not_found_embed",
"parse_alias",
"normalize_alias",
"format_description",
"trigger_typing",
"escape_code_block",
"format_channel_name",
"tryint",
"match_title",
]
def strtobool(val):
if isinstance(val, bool):
return val
try:
return _stb(str(val))
except ValueError:
val = val.lower()
if val == "enable":
return 1
if val == "disable":
return 0
raise
class User(commands.MemberConverter):
"""
A custom discord.py `Converter` that
supports `Member`, `User`, and string ID's.
"""
# noinspection PyCallByClass,PyTypeChecker
async def convert(self, ctx, argument):
try:
return await commands.MemberConverter().convert(ctx, argument)
except commands.BadArgument:
pass
try:
return await commands.UserConverter().convert(ctx, argument)
except commands.BadArgument:
pass
match = self._get_id_match(argument)
if match is None:
raise commands.BadArgument('User "{}" not found'.format(argument))
return discord.Object(int(match.group(1)))
def truncate(text: str, max: int = 50) -> str: # pylint: disable=redefined-builtin
"""
Reduces the string to `max` length, by trimming the message into "...".
Parameters
----------
text : str
The text to trim.
max : int, optional
The max length of the text.
Defaults to 50.
Returns
-------
str
The truncated text.
"""
text = text.strip()
return text[: max - 3].strip() + "..." if len(text) > max else text
def format_preview(messages: typing.List[typing.Dict[str, typing.Any]]):
"""
Used to format previews.
Parameters
----------
messages : List[Dict[str, Any]]
A list of messages.
Returns
-------
str
A formatted string preview.
"""
messages = messages[:3]
out = ""
for message in messages:
if message.get("type") in {"note", "internal"}:
continue
author = message["author"]
content = str(message["content"]).replace("\n", " ")
name = author["name"] + "#" + str(author["discriminator"])
prefix = "[M]" if author["mod"] else "[R]"
out += truncate(f"`{prefix} {name}:` {content}", max=75) + "\n"
return out or "No Messages"
def is_image_url(url: str, **kwargs) -> str:
"""
Check if the URL is pointing to an image.
Parameters
----------
url : str
The URL to check.
Returns
-------
bool
Whether the URL is a valid image URL.
"""
if url.startswith("https://gyazo.com") or url.startswith("http://gyazo.com"):
# gyazo support
url = re.sub(
r"(http[s]?:\/\/)((?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)",
r"\1i.\2.png",
url,
)
return parse_image_url(url, **kwargs)
def parse_image_url(url: str, *, convert_size=True) -> str:
"""
Convert the image URL into a sized Discord avatar.
Parameters
----------
url : str
The URL to convert.
Returns
-------
str
The converted URL, or '' if the URL isn't in the proper format.
"""
types = [".png", ".jpg", ".gif", ".jpeg", ".webp"]
url = parse.urlsplit(url)
if any(url.path.lower().endswith(i) for i in types):
if convert_size:
return parse.urlunsplit((*url[:3], "size=128", url[-1]))
else:
return parse.urlunsplit(url)
return ""
def human_join(strings):
if len(strings) <= 2:
return " or ".join(strings)
return ", ".join(strings[: len(strings) - 1]) + " or " + strings[-1]
def days(day: typing.Union[str, int]) -> str:
"""
Humanize the number of days.
Parameters
----------
day: Union[int, str]
The number of days passed.
Returns
-------
str
A formatted string of the number of days passed.
"""
day = int(day)
if day == 0:
return "**today**"
return f"{day} day ago" if day == 1 else f"{day} days ago"
def cleanup_code(content: str) -> str:
"""
Automatically removes code blocks from the code.
Parameters
----------
content : str
The content to be cleaned.
Returns
-------
str
The cleaned content.
"""
# remove ```py\n```
if content.startswith("```") and content.endswith("```"):
return "\n".join(content.split("\n")[1:-1])
# remove `foo`
return content.strip("` \n")
TOPIC_TITLE_REGEX = re.compile(r"\bTitle: (.*)\n(?:User ID: )\b", flags=re.IGNORECASE | re.DOTALL)
TOPIC_UID_REGEX = re.compile(r"\bUser ID:\s*(\d{17,21})\b", flags=re.IGNORECASE)
def match_title(text: str) -> int:
"""
Matches a title in the format of "Title: XXXX"
Parameters
----------
text : str
The text of the user ID.
Returns
-------
Optional[str]
The title if found
"""
match = TOPIC_TITLE_REGEX.search(text)
if match is not None:
return match.group(1)
def match_user_id(text: str) -> int:
"""
Matches a user ID in the format of "User ID: 12345".
Parameters
----------
text : str
The text of the user ID.
Returns
-------
int
The user ID if found. Otherwise, -1.
"""
match = TOPIC_UID_REGEX.search(text)
if match is not None:
return int(match.group(1))
return -1
def create_not_found_embed(word, possibilities, name, n=2, cutoff=0.6) -> discord.Embed:
# Single reference of Color.red()
embed = discord.Embed(
color=discord.Color.red(), description=f"**{name.capitalize()} `{word}` cannot be found.**"
)
val = get_close_matches(word, possibilities, n=n, cutoff=cutoff)
if val:
embed.description += "\nHowever, perhaps you meant...\n" + "\n".join(val)
return embed
def parse_alias(alias, *, split=True):
def encode_alias(m):
return "\x1AU" + base64.b64encode(m.group(1).encode()).decode() + "\x1AU"
def decode_alias(m):
return base64.b64decode(m.group(1).encode()).decode()
alias = re.sub(
r"(?:(?<=^)(?:\s*(?<!\\)(?:\")\s*)|(?<=&&)(?:\s*(?<!\\)(?:\")\s*))(.+?)"
r"(?:(?:\s*(?<!\\)(?:\")\s*)(?=&&)|(?:\s*(?<!\\)(?:\")\s*)(?=$))",
encode_alias,
alias,
).strip()
aliases = []
if not alias:
return aliases
if split:
iterate = re.split(r"\s*&&\s*", alias)
else:
iterate = [alias]
for a in iterate:
a = re.sub("\x1AU(.+?)\x1AU", decode_alias, a)
if a[0] == a[-1] == '"':
a = a[1:-1]
aliases.append(a)
return aliases
def normalize_alias(alias, message=""):
aliases = parse_alias(alias)
contents = parse_alias(message, split=False)
final_aliases = []
for a, content in zip_longest(aliases, contents):
if a is None:
break
if content:
final_aliases.append(f"{a} {content}")
else:
final_aliases.append(a)
return final_aliases
def format_description(i, names):
return "\n".join(
": ".join((str(a + i * 15), b))
for a, b in enumerate(takewhile(lambda x: x is not None, names), start=1)
)
def trigger_typing(func):
@functools.wraps(func)
async def wrapper(self, ctx: commands.Context, *args, **kwargs):
await ctx.trigger_typing()
return await func(self, ctx, *args, **kwargs)
return wrapper
def escape_code_block(text):
return re.sub(r"```", "`\u200b``", text)
def format_channel_name(bot, author, exclude_channel=None, force_null=False):
"""Sanitises a username for use with text channel names"""
guild = bot.modmail_guild
if force_null:
name = new_name = "null"
else:
if bot.config["use_user_id_channel_name"]:
name = new_name = str(author.id)
else:
name = author.name.lower()
if force_null:
name = "null"
name = new_name = (
"".join(l for l in name if l not in string.punctuation and l.isprintable())
or "null"
) + f"-{author.discriminator}"
counter = 1
existed = set(c.name for c in guild.text_channels if c != exclude_channel)
while new_name in existed:
new_name = f"{name}_{counter}" # multiple channels with same name
counter += 1
return new_name
def tryint(x):
try:
return int(x)
except (ValueError, TypeError):
return x