-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
83 lines (64 loc) · 2.49 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
from os.path import join
from os import remove
from discord import HTTPException
from emoji import emojize
import settings
# Returns a path relative to the bot directory
def get_rel_path(rel_path):
return join(settings.BASE_DIR, rel_path)
# Returns an emoji as required to send it in a message
# You can pass the emoji name with or without colons
# If fail_silently is True, it will not raise an exception
# if the emoji is not found, it will return the input instead
def get_emoji(emoji_name, fail_silently=False):
alias = emoji_name if emoji_name[0] == emoji_name[-1] == ":" else f":{emoji_name}:"
the_emoji = emojize(alias, use_aliases=True)
if the_emoji == alias and not fail_silently:
raise ValueError(f"Emoji {alias} not found!")
return the_emoji
# A shortcut to get a channel by a certain attribute
# Uses the channel name by default
# If many matching channels are found, returns the first one
def get_channel(client, value, attribute="name"):
channel = next(
(
c
for c in client.get_all_channels()
if getattr(c, attribute).lower() == value.lower()
),
None,
)
if not channel:
raise ValueError("No such channel")
return channel
# Shortcut method to send a message in a channel with a certain name
# You can pass more positional arguments to send_message
# Uses get_channel, so you should be sure that the bot has access to only
# one channel with such name
async def send_in_channel(client, channel_name, *args):
await client.send_message(get_channel(client, channel_name), *args)
# Attempts to upload a file in a certain channel
# content refers to the additional text that can be sent alongside the file
# delete_after_send can be set to True to delete the file afterwards
async def try_upload_file(
client, channel, file_path, content=None, delete_after_send=False, retries=3
):
used_retries = 0
sent_msg = None
while not sent_msg and used_retries < retries:
try:
sent_msg = await client.send_file(channel, file_path, content=content)
except HTTPException:
used_retries += 1
if delete_after_send:
remove(file_path)
if not sent_msg:
await client.send_message(
channel, "Oops, something happened. Please try again."
)
return sent_msg
def parse_mention(mention: str):
"""turns a mention into an id string"""
id = mention.replace(">", "")
id = id.replace("<@", "")
return id.split("!")[-1]