Newer
Older
import os
import re
from datetime import datetime
from discord.ext.commands import Context
from discord import ButtonStyle, Embed, User, Member
from dotenv import load_dotenv
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
load_dotenv()
DATE_TIME_FMT = os.getenv("DISCORD_DATE_TIME_FORMAT")
async def send_dm(user, message, embed=None):
""" Send DM to a user/member """
try:
if type(user) is User or type(user) is Member:
if user.dm_channel is None:
await user.create_dm()
return await user.dm_channel.send(message, embed=embed)
except:
print(f"Cannot send DM to {user} with text: {message}")
def is_mod(context_or_interaction):
if isinstance(context_or_interaction, Context):
author = context_or_interaction.author
else:
author = context_or_interaction.user
roles = author.roles
for role in roles:
if role.id == int(os.getenv("DISCORD_MOD_ROLE")):
return True
return False
def is_valid_time(time):
return re.match(r"^\d+[mhd]?$", time)
def to_minutes(time):
if time[-1:] == "m":
return int(time[:-1])
elif time[-1:] == "h":
h = int(time[:-1])
return h * 60
elif time[-1:] == "d":
d = int(time[:-1])
h = d * 24
return h * 60
return int(time)
async def confirm(channel, title, description, message="", custom_prefix="", callback=None):
embed = Embed(title=title,
description=description,
color=19607)
return await channel.send(message, embed=embed, view=DialogView([
{"emoji": "👍", "custom_id": f"{custom_prefix}_yes", "style": ButtonStyle.green},
{"emoji": "👎", "custom_id": f"{custom_prefix}_no", "style": ButtonStyle.red},
]))
def date_to_string(date: datetime):
return date.strftime(DATE_TIME_FMT)
def date_from_string(date: str):
return datetime.strptime(date, DATE_TIME_FMT)
async def files_from_attachments(attachments):
files = []
for attachment in attachments:
files.append(await attachment.to_file(spoiler=attachment.is_spoiler()))
return files