forked from hypergonial/snedbot_v1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_init.py
More file actions
277 lines (265 loc) · 10.7 KB
/
database_init.py
File metadata and controls
277 lines (265 loc) · 10.7 KB
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
import asyncio
import os
import asyncpg
from config import config
from dotenv import load_dotenv
# Modify this line & change it to your PSQL DB address
# Do not modify the variables, DBPASS is retrieved from .env,
# while db_name is decided on runtime.
dsn = config["postgres_dsn"]
try:
print(
"""Sned-Bot Database Initialization
The following steps need to be taken BEFORE running this script:
Create a postgresql database on the address specified in the DSN,
and point the postgres_dsn in config.py to it. The database's name
must be either 'sned' or 'sned_exp' with default user 'postgres'.
Current DSN: {dsn}"""
)
while True:
is_experimental = input(
"Do you want to initialize the database for the stable or the experimental version? Type 'stable' for stable, 'exp' for experimental.\n> "
)
if is_experimental in ["stable", "exp"]:
if is_experimental == "stable":
db_name = "sned"
else:
db_name = "sned_exp"
break
else:
print("Invalid input. Try again.\n")
async def init_tables():
"""
Create all tables necessary for the functioning of this bot.
"""
pool = await asyncpg.create_pool(dsn=dsn.format(db_name=db_name))
async with pool.acquire() as con:
print("Creating tables...")
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.global_config
(
guild_id bigint NOT NULL,
prefix text[],
PRIMARY KEY (guild_id)
)"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.users
(
user_id bigint NOT NULL,
guild_id bigint NOT NULL,
flags json,
warns integer NOT NULL DEFAULT 0,
notes text[],
PRIMARY KEY (user_id, guild_id),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)"""
)
# guild_id is always needed in table, so I just hacked it in c:
# the table is not guild-specific though
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.blacklist
(
guild_id integer NOT NULL DEFAULT 0,
user_id bigint NOT NULL,
PRIMARY KEY (user_id)
)
"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.guild_blacklist
(
guild_id bigint NOT NULL
)"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.mod_config
(
guild_id bigint,
dm_users_on_punish bool NOT NULL DEFAULT true,
clean_up_mod_commands bool NOT NULL DEFAULT false,
automod_policies text NOT NULL DEFAULT '{}',
PRIMARY KEY (guild_id),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.timers
(
id serial NOT NULL,
guild_id bigint NOT NULL,
user_id bigint NOT NULL,
channel_id bigint,
event text NOT NULL,
expires bigint NOT NULL,
notes text,
PRIMARY KEY (id),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.permissions
(
guild_id bigint NOT NULL,
ptype text NOT NULL,
role_ids bigint[] NOT NULL DEFAULT '{}',
PRIMARY KEY (guild_id, ptype),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.modules
(
guild_id bigint NOT NULL,
module_name text NOT NULL,
is_enabled bool NOT NULL DEFAULT true,
PRIMARY KEY (guild_id, module_name),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)
"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.priviliged
(
guild_id bigint NOT NULL,
priviliged_role_id bigint NOT NULL,
PRIMARY KEY (guild_id, priviliged_role_id),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.button_roles
(
guild_id bigint NOT NULL,
entry_id serial NOT NULL,
channel_id bigint NOT NULL,
msg_id bigint NOT NULL,
emoji text NOT NULL,
buttonlabel text,
buttonstyle text,
role_id bigint NOT NULL,
PRIMARY KEY (guild_id, entry_id),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.events
(
guild_id bigint NOT NULL,
entry_id text NOT NULL,
channel_id bigint NOT NULL,
msg_id bigint NOT NULL,
recurring_in bigint,
permitted_roles bigint[],
categories json NOT NULL,
PRIMARY KEY (guild_id, entry_id),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)
"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.matchmaking_config
(
guild_id bigint,
init_channel_id bigint,
announce_channel_id bigint,
lfg_role_id bigint,
PRIMARY KEY (guild_id),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.matchmaking_listings
(
id text,
ubiname text NOT NULL,
host_id bigint NOT NULL,
gamemode text NOT NULL,
playercount text NOT NULL,
DLC text NOT NULL,
mods text NOT NULL,
timezone text NOT NULL,
additional_info text NOT NULL,
timestamp bigint NOT NULL,
guild_id bigint NOT NULL,
PRIMARY KEY (id)
)"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.tags
(
guild_id bigint NOT NULL,
tag_name text NOT NULL,
tag_owner_id bigint NOT NULL,
tag_aliases text[],
tag_content text NOT NULL,
PRIMARY KEY (guild_id, tag_name),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.log_config
(
guild_id bigint NOT NULL,
log_channels json,
PRIMARY KEY (guild_id),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)"""
)
await con.execute(
"""
CREATE TABLE IF NOT EXISTS public.ktp
(
guild_id bigint NOT NULL,
ktp_id serial NOT NULL,
ktp_channel_id bigint NOT NULL,
ktp_msg_id bigint NOT NULL,
ktp_content text NOT NULL,
PRIMARY KEY (guild_id, ktp_id),
FOREIGN KEY (guild_id)
REFERENCES global_config (guild_id)
ON DELETE CASCADE
)"""
)
print("Tables created, database is ready!")
asyncio.get_event_loop().run_until_complete(init_tables())
input("\nPress enter to exit...")
except KeyboardInterrupt:
print("\nKeyboard interrupt received, exiting...")