-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
175 lines (146 loc) · 4.25 KB
/
util.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
import io
import os
import re
import json
from datetime import timedelta
import wonderwords
from PIL import Image, ImageDraw, ImageFont
import logging
configuration = {}
from discord import Color
COLORS = {
"teal": Color.teal(),
"dark_teal": Color.dark_teal(),
"brand green": Color.brand_green(),
"green": Color.green(),
"dark_green": Color.dark_green(),
"blue": Color.blue(),
"dark_blue": Color.dark_blue(),
"magenta": Color.magenta(),
"dark magenta": Color.dark_magenta(),
"gold": Color.gold(),
"dark gold": Color.dark_gold(),
"orange": Color.orange(),
"dark orange": Color.dark_orange(),
"brand red": Color.brand_red(),
"red": Color.red(),
"dark red": Color.dark_red(),
"lighter grey": Color.lighter_grey(),
"lighter gray": Color.lighter_gray(),
"light grey": Color.light_grey(),
"light gray": Color.light_gray(),
"dark grey": Color.dark_grey(),
"dark gray": Color.dark_gray(),
"darker grey": Color.darker_grey(),
"darker gray": Color.darker_gray(),
"og blurple": Color.og_blurple(),
"blurple": Color.blurple(),
"greyple": Color.greyple(),
"dark theme": Color.dark_theme(),
"fuchsia": Color.fuchsia(),
"yellow": Color.yellow(),
"light_embed": Color.light_embed(),
"dark embed": Color.dark_embed(),
}
try:
configuration = {
"owner_id": int(os.environ["OWNER_ID"]),
"custom_role_id": int(os.environ["CUSTOM_ROLE_ID"]),
"guild_id": int(os.environ["GUILD_ID"]),
"role_ids": [int(role_id) for role_id in os.environ["ROLE_IDS"].split(",")],
"emoji_server_id": int(os.environ["EMOJI_SERVER_ID"]),
"connection_string": os.environ["CONNECTION_STRING"],
"token": os.environ["TOKEN"]
}
except KeyError:
configuration = json.loads(
open("config.json", 'r').read()
)
import psycopg2_pool
pool = psycopg2_pool.ConnectionPool(minconn=5, maxconn=20, dsn=configuration["connection_string"], idle_timeout=60)
def execute_query(query, params=None):
with pool.getconn() as conn:
with conn.cursor() as cursor:
if params:
cursor.execute(query, params)
else:
cursor.execute(query)
conn.commit()
def delete_query(query):
execute_query(query)
def insert_query(query):
execute_query(query)
def insert_with_params(query, params):
execute_query(query, params)
def select_one(query):
with pool.getconn() as conn:
with conn.cursor() as cursor:
cursor.execute(query)
result = cursor.fetchone()
return result
def select_all(query):
with pool.getconn() as conn:
with conn.cursor() as cursor:
cursor.execute(query)
result = cursor.fetchall()
return result
def get_maaldar_user(user_id):
return select_one(f"SELECT * FROM Maaldar WHERE user_id = '{user_id}'")
DAYS_IN_SECONDS_REQUIRED_FOR_ROLE = 15_552_000
def is_old_maaldar(user_id):
data = select_one(f"SELECT boosting_since FROM MaaldarDuration WHERE user_id = '{user_id}'")
if data is None:
return False
return data[0] >= DAYS_IN_SECONDS_REQUIRED_FOR_ROLE
def make_image(dominant_color):
image = Image.new(
mode="RGBA",
size=(50, 50),
color=(0, 0, 0, 0)
)
ImageDraw.Draw(image).rounded_rectangle(
(0, 0, 50, 50),
radius=20,
fill=dominant_color
)
return image
def concatenate_images(images):
# size = width of 10 images and height of 1 image
image = Image.new(
mode="RGBA",
size=(50 * 10, 50),
color=(0, 0, 0, 0)
)
ImageDraw.Draw(image).rounded_rectangle(
(0, 0, 500, 50),
radius=20
)
width = 0
for index, image_to_paste in enumerate(images, start=1):
font = ImageFont.load_default()
font
draw = ImageDraw.Draw(image_to_paste)
draw.text(
(10, 10),
f"{index}",
font=font,
stroke_width=1,
stroke_fill="white"
)
image.paste(image_to_paste, (width, 0))
width += 50
bytes_array = io.BytesIO()
image.save(bytes_array, format="PNG")
bytes_array.seek(0)
return bytes_array
def rgb_to_hex(rgb):
return "%02x%02x%02x" % (rgb)
def match_url_regex(string):
# It works 🤷♀️
return re.findall(r'(?:http\:|https\:)?\/\/.*\.(?:png|jpg)', string, re.IGNORECASE)
def create_session_token() -> str:
random_word = wonderwords.RandomWord()
session_tokens: list[str] = random_word.random_words(5, word_max_length=4, include_parts_of_speech=["verbs", "nouns"])
session_tokens = [token.capitalize() for token in session_tokens]
session = "".join(session_tokens)
return session