-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.py
353 lines (269 loc) · 9.42 KB
/
database.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
"""Functions for interacting with the database throughout the bot."""
import motor.motor_asyncio
import constants
from rglapi import RglApi
RGL: RglApi = RglApi()
db_client: motor.motor_asyncio.AsyncIOMotorClient = (
motor.motor_asyncio.AsyncIOMotorClient(constants.DB_URL + "/?w=majority")
)
class BotCollection:
"""Class used to interact with different parts of the bots database in an easier way."""
def __init__(self, database: str, collection: str) -> None:
self.client = db_client
self.database = db_client[database][collection]
async def add_item(self, item: dict):
"""Insert an item to the collection.
Args:
item (dict): The item to add.
"""
await self.database.insert_one(item)
async def update_item(self, search: dict, item: dict):
"""Update an item in the collection.
Args:
search (dict): The key to look for in the collection.
item (dict): The thing to insert into the item.
"""
await self.database.update_one(search, item, upsert=True)
async def find_item(self, search: dict):
"""Search for an item in the collection.
Args:
search (dict): The key to look for.
"""
result = await self.database.find_one(search)
if result is None:
raise LookupError
return result
async def delete_item(self, search: dict):
"""Delete an item from the collection.
Args:
search (dict): The key to look for.
"""
await self.database.delete_one(search)
async def find_all_items(self) -> list[dict]:
"""Returns all items in the collection."""
results = []
async for item in self.database.find():
results.append(item)
return results
async def is_server_setup(guild: int):
"""Check if a server is setup.
Args:
guild (int): The guild ID to check.
Returns:
bool: True if the server is setup, False otherwise.
"""
database = db_client.guilds.config
if await database.find_one({"guild": guild}) is None:
return False
return True
async def add_new_guild(guild: int, role: int, connect: int | None, rcon: int | None):
"""Add a new guild to the database.
Args:
guild (int): The guild ID to add.
role (int): The runner role ID.
connect (int | None): The connect channel ID.
rcon (int | None): The rcon channel ID.
"""
database = db_client.guilds.config
await database.update_one(
{"guild": guild},
{"$set": {"role": role, "connect": connect, "rcon": rcon, "immune": []}},
upsert=True,
)
async def get_server(guild: int):
"""Get the server data from the database.
Args:
guild (int): The guild ID to get.
Returns:
dict: The server data from the db.
"""
database = db_client.guilds.config
if await database.find_one({"guild": guild}) is None:
return None
return await database.find_one({"guild": guild})
def get_all_servers():
"""Get all servers from the database."""
database = db_client.guilds.config
return database.find()
async def set_registration_settings(guild: int, registration):
"""Set the registration settings for a guild.
Args:
guild (int): The guild ID to set.
registration (dict): The registration settings to set.
"""
database = db_client.guilds.config
await database.update_one(
{"guild": guild},
{"$set": {"registration": registration}},
upsert=True,
)
async def set_guild_serveme(guild: int, serveme: str):
"""Set the serveme key for a guild.
Args:
guild (int): The guild ID to set.
serveme (str): The serveme key to set.
"""
database = db_client.guilds.config
await database.update_one(
{"guild": guild},
{"$set": {"serveme": serveme}},
upsert=True,
)
async def add_player(steam: str, discord: str):
"""Add a new player to the database.
Args:
steam (int): The steam ID to add.
discord (int): The discord ID to add.
"""
database = db_client.players.data
await database.update_one(
{"steam": str(steam)},
{"$set": {"steam": str(steam), "discord": discord}},
upsert=True,
)
async def add_med_immune_player(guild: int, discord: int):
"""Add a player to the med immunity array for the guild
Args:
guild (int): The guild ID to set in
discord (str): The discord ID of the player to set
"""
database = db_client.guilds.config
await database.update_one({"guild": guild}, {"$push": {"immune": discord}})
async def remove_med_immune_player(guild: int, discord: int):
"""Remove a player from the med immunity array for the guild
Args:
guild (int): The guild ID to remove from
discord (str): The discord ID of the player to remove
"""
database = db_client.guilds.config
await database.update_one({"guild": guild}, {"$pull": {"immune": discord}})
async def clear_med_immunity_by_guild(guild: int):
"""Clear the med immunity field of all discord ids for a given guild
Args:
guild: The guild ID to clear the med immunity field of
"""
database = db_client.guilds.config
await database.update_one({"guild": guild}, {"$set": {"immune": []}})
async def clear_med_immunity_all_guilds():
"""Clear the med immunity field for all guilds
THIS FUNCTION SHOULD NEVER BE CALLED WITHIN A SLASH COMMAND
"""
database = db_client.guilds.config
await database.update_many({}, {"$set": {"immune": []}})
async def get_player_stats(steam: int):
"""Get the player stats from the database.
Args:
steam (int): The steam ID to get.
Returns:
dict: The player stats from the db.
"""
database = db_client.players.stats
if await database.find_one({"steam": steam}) is None:
return None
return await database.find_one({"steam": steam})
async def update_player_stats(steam: str, stats: dict):
"""Update the player stats in the database.
Args:
steam (str): The steam ID to update.
stats (dict): The stats to update.
"""
database = db_client.players.stats
await database.update_one({"steam": steam}, {"$set": stats}, upsert=True)
async def get_steam_from_discord(discord: int):
"""Get the steam ID from the database.
Args:
discord (int): The discord ID to get.
Returns:
dict: The steam ID from the db.
"""
database = db_client.players.data
player = await database.find_one({"discord": str(discord)})
if player is None:
return None
return player["steam"]
async def get_player_from_steam(steam: int) -> dict:
"""Get the player from the database.
Args:
steam (int): The steam ID to get.
Returns:
dict: Player data
"""
database = db_client.players.data
player = await database.find_one({"steam": str(steam)})
if player is None:
raise LookupError
return player
async def get_player_from_discord(discord: int):
"""Get the player from the database.
Args:
discord (int): The discord ID to get.
Returns:
dict: Player data
"""
database = db_client.players.data
player = await database.find_one({"discord": str(discord)})
if player is None:
raise LookupError("Player not found in database")
return player
def get_all_players():
"""Get all players from the database."""
database = db_client.players.data
return database.find()
async def player_count():
"""Returns the amount of players in the database."""
database = db_client.players.data
return await database.count_documents({})
async def log_count():
"""Returns the amount of logs in the database."""
database = db_client.logs.list
return await database.count_documents({})
async def get_divisions(discord: int):
"""Get the RGL divisons of a player.
Args:
discord (int): The discord ID to get.
Returns:
dict: The top divs of the player.
"""
database = db_client.players.data
if database.find_one({"discord": str(discord)}) is None:
return None
try:
player = await database.find_one({"discord": str(discord)})
if player:
return player["divison"]
return None
except KeyError:
return None
async def update_divisons(steam: int, divisons):
"""Update the RGL divisons of a player.
Args:
steam (int): The steam ID to update.
divisons (dict): The divisons to set for the player.
"""
database = db_client.players.data
if divisons["hl"]["highest"] == -1 and await get_divisions(steam) is not None:
print("Skipping updating divs as there is no data.")
return
await database.update_one(
{"steam": str(steam)},
{"$set": {"divison": divisons}},
upsert=True,
)
async def update_rgl_ban_status(steam: int) -> bool:
"""Update the RGL ban status of a player.
Args:
steam (int): The steam ID to update.
Returns:
bool: True if the player is banned, False otherwise.
"""
database = db_client.players.data
try:
ban_status = await RGL.check_banned(steam)
except LookupError:
return False
await database.update_one(
{"steam": str(steam)},
{"$set": {"rgl_ban": ban_status}},
upsert=True,
)
return ban_status