-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmongo_tracker.py
242 lines (207 loc) · 8.17 KB
/
mongo_tracker.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
from helpers import Collections
class Tracker:
def __init__(self, db):
self.attendees = db[Collections.ATTENDEES]
self.decliners = db[Collections.DECLINERS]
self.cancellers = db[Collections.CANCELLERS]
self.dreamers = db[Collections.DREAMERS]
self.inventories = db[Collections.INVENTORIES]
self.config = db[Collections.CONFIG]
self.players = db[Collections.PLAYERS]
@staticmethod
def _get_user(user):
return {"name": user.name, "id": user.id}
def get_all(self, guild_id):
attendees = self.get_attendees_for_guild(guild_id)
decliners = self.get_decliners_for_guild(guild_id)
dreamers = self.get_dreamers_for_guild(guild_id)
cancellers = self.get_cancellers_for_guild(guild_id)
return (attendees, decliners, dreamers, cancellers)
def get_attendees_for_guild(self, guild_id):
try:
return self.attendees.find_one(
{"guild": guild_id}, {Collections.ATTENDEES: 1, "_id": 0}
)[Collections.ATTENDEES]
except TypeError:
return []
def get_decliners_for_guild(self, guild_id):
try:
return self.decliners.find_one(
{"guild": guild_id}, {Collections.DECLINERS: 1, "_id": 0}
)[Collections.DECLINERS]
except TypeError:
return []
def get_cancellers_for_guild(self, guild_id):
try:
return self.cancellers.find_one(
{"guild": guild_id}, {Collections.CANCELLERS: 1, "_id": 0}
)[Collections.CANCELLERS]
except TypeError:
return []
def get_dreamers_for_guild(self, guild_id):
try:
return self.dreamers.find_one(
{"guild": guild_id}, {Collections.DREAMERS: 1, "_id": 0}
)[Collections.DREAMERS]
except TypeError:
return []
def get_inventories_for_guild(self, guild_id):
return self.inventories.find({"guild": guild_id})
def get_inventory_for_player(self, guild_id, player):
try:
return self.inventories.find_one(
{"guild": guild_id, "player": self._get_user(player)}
)["inv"]
except TypeError:
return []
def get_config_for_guild(self, guild_id):
try:
return self.config.find_one(
{"guild": guild_id}, {Collections.CONFIG: 1, "_id": 0}
)[Collections.CONFIG]
except TypeError:
return None
def get_players_for_guild(self, guild_id):
try:
return self.players.find_one(
{"guild": guild_id}, {Collections.PLAYERS: 1, "_id": 0}
)[Collections.PLAYERS]
except TypeError:
return None
def reset(self, guild_id):
query = {"guild": guild_id}
self.attendees.delete_one(query)
self.decliners.delete_one(query)
self.cancellers.delete_one(query)
self.dreamers.delete_one(query)
def skip(self, guild_id):
query = {"guild": guild_id}
self.db.config.update_one({query}, {"config.alerts": False})
def add_attendee_for_guild(self, guild_id, attendee):
return self.attendees.update_one(
{"guild": guild_id},
{"$addToSet": {"attendees": self._get_user(attendee)}},
upsert=True,
)
def rm_attendee_for_guild(self, guild_id, attendee):
return self.attendees.update_one(
{"guild": guild_id}, {"$pull": {"attendees": self._get_user(attendee)}}
)
def add_decliner_for_guild(self, guild_id, decliner):
return self.decliners.update_one(
{"guild": guild_id},
{"$addToSet": {"decliners": self._get_user(decliner)}},
upsert=True,
)
def rm_decliner_for_guild(self, guild_id, decliner):
return self.decliners.update_one(
{"guild": guild_id}, {"$pull": {"decliners": self._get_user(decliner)}}
)
def add_canceller_for_guild(self, guild_id, canceller):
return self.cancellers.update_one(
{"guild": guild_id},
{"$addToSet": {"cancellers": self._get_user(canceller)}},
upsert=True,
)
def rm_canceller_for_guild(self, guild_id, canceller):
return self.cancellers.update_one(
{"guild": guild_id}, {"$pull": {"cancellers": self._get_user(canceller)}}
)
def add_dreamer_for_guild(self, guild_id, dreamer):
return self.dreamers.update_one(
{"guild": guild_id},
{"$addToSet": {"dreamers": self._get_user(dreamer)}},
upsert=True,
)
def rm_dreamer_for_guild(self, guild_id, dreamer):
return self.dreamers.update_one(
{"guild": guild_id}, {"$pull": {"dreamers": self._get_user(dreamer)}}
)
def add_player_for_guild(self, guild_id, player):
return self.players.update_one(
{"guild": guild_id},
{"$addToSet": {"players": self._get_user(player)}},
upsert=True,
)
def rm_player_for_guild(self, guild_id, player):
return self.players.update_one(
{"guild": guild_id}, {"$pull": {"players": self._get_user(player)}}
)
def add_to_player_inventory(self, guild_id, player, item, qty):
return self.inventories.update_one(
{"guild": guild_id, "player": self._get_user(player)},
{"$addToSet": {"inv": {"item": item.strip(), "qty": qty}}},
upsert=True,
)
def rm_from_player_inventory(self, guild_id, player, item):
return self.inventories.update_one(
{"guild": guild_id, "player": self._get_user(player)},
{"$pull": {"inv": {"item": item}}},
)
def update_player_inventory(self, guild_id, player, item, qty):
return self.inventories.update_one(
{
"guild": guild_id,
"player": self._get_user(player),
"inv.item": item.strip(),
},
{"$set": {"inv.$.qty": qty}},
)
def create_guild_config(
self,
guild_id,
dm_user,
session_day,
session_time,
meeting_room,
first_alert,
second_alert,
):
return self.config.update_one(
{"guild": guild_id},
{
"$set": {
"guild": guild_id,
"config": {
"session-dm": self._get_user(dm_user),
"session-day": session_day,
"session-time": str(session_time),
"meeting-room": meeting_room,
"first-alert": first_alert,
"second-alert": second_alert,
"alerts": True,
},
}
},
upsert=True,
)
def rm_guild_config(self, guild_id):
query = {"guild": guild_id}
return self.config.delete_one(query)
def get_first_alert_configs(self, day_of_the_week: int):
return self.config.find(
{"config.first-alert": day_of_the_week, "config.alerts": True}
)
def get_second_alert_configs(self, day_of_the_week: int):
return self.config.find(
{"config.second-alert": day_of_the_week, "config.alerts": True}
)
def get_session_day_configs(self, day_of_the_week: int):
return self.config.find(
{"config.session-day": day_of_the_week, "config.alerts": True}
)
def register_player(self, guild_id: int, player):
return self.players.update_one(
{"guild": guild_id},
{"$addToSet": {Collections.PLAYERS: self._get_user(player)}},
upsert=True,
)
def unregister_player(self, guild_id: int, player):
return self.players.update_one(
{"guild": guild_id},
{"$pull": {Collections.PLAYERS: self._get_user(player)}},
)
def is_full_group(self, guild_id: int) -> bool:
players = [player["id"] for player in self.get_players_for_guild(guild_id)]
attendees = [att["id"] for att in self.get_attendees_for_guild(guild_id)]
return len(players) == len(attendees)