forked from boviex/feubot
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmessageManager.py
132 lines (101 loc) · 4.02 KB
/
messageManager.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
import pickle
import cloudinary, urllib
import sys
ROLE_FILE = "Roles.pickle"
MESSAGE_PROPERTIES_FILE = "MessageProperties.pickle"
#Internal function for loading and returning the contents of the roles file
def load_roles():
try:
web_copy = cloudinary.api.resource(ROLE_FILE, resource_type='raw')['url']
response = urllib.request.urlopen(web_copy)
roleReact_db = pickle.load(response)
except Exception as e:
print(e)
roleReact_db = {}
with open(ROLE_FILE, "wb") as file:
file.seek(0)
pickle.dump(roleReact_db, file)
return roleReact_db
def add_role(messageID, reaction, role):
roleReact_db = load_roles()
if not roleReact_db:
roleReact_db = {}
if messageID not in roleReact_db:
temp = {messageID: {reaction: role}}
roleReact_db.update(temp)
with open(ROLE_FILE, "wb") as file:
roleReact_db[messageID][reaction] = role
pickle.dump(roleReact_db, file)
cloudinary.uploader.upload(ROLE_FILE, resource_type='raw', public_id=ROLE_FILE, invalidate=True)
def delete_reaction_role(messageID, reaction):
roleReact_db = load_roles()
if not roleReact_db:
return("There are no role reactions set")
if reaction == "ALL" and messageID in roleReact_db:
roleReact_db.pop(messageID)
elif messageID in roleReact_db and reaction in roleReact_db[messageID]:
roleReact_db[messageID].pop(reaction)
else:
return(f"{reaction} reaction for {messageID} is not in database")
with open(ROLE_FILE, "wb") as file:
pickle.dump(roleReact_db, file)
cloudinary.uploader.upload(ROLE_FILE, resource_type='raw', public_id=ROLE_FILE, invalidate=True)
return("Done")
#Internal function for checking database for reaction based roles
def find_role(messageID, reaction):
roleReact_db = load_roles()
if messageID in roleReact_db:
if reaction in roleReact_db[messageID]:
return roleReact_db[messageID][reaction]
else:
print(f"{reaction} not in {messageID}")
return None
else:
print(f"messageID {messageID} not found")
return None
def load_properties():
try:
web_copy = cloudinary.api.resource(MESSAGE_PROPERTIES_FILE, resource_type='raw')['url']
response = urllib.request.urlopen(web_copy)
property_db = pickle.load(response)
except Exception as e:
print(e)
property_db = {}
with open(MESSAGE_PROPERTIES_FILE, "wb") as file:
file.seek(0)
pickle.dump(property_db, file)
return property_db
def add_property(messageID, newProperty):
property_db = load_properties()
if not property_db:
property_db = {}
if messageID not in property_db:
temp = {messageID: {newProperty: True}}
property_db.update(temp)
#Write data
with open(MESSAGE_PROPERTIES_FILE, "wb") as file:
property_db[messageID][newProperty] = True
pickle.dump(property_db, file)
cloudinary.uploader.upload(MESSAGE_PROPERTIES_FILE, resource_type='raw', public_id=MESSAGE_PROPERTIES_FILE, invalidate=True)
def delete_property(messageID, newProperty):
property_db = load_properties()
if not property_db:
return("There are no message properties set")
if newProperty == "ALL" and messageID in property_db:
property_db.pop(messageID)
elif messageID in property_db and newProperty in property_db[messageID]:
property_db[messageID].pop(newProperty)
else:
return(f"{newProperty} property for {messageID} is not in database")
#Write data
with open(MESSAGE_PROPERTIES_FILE, "wb") as file:
pickle.dump(property_db, file)
cloudinary.uploader.upload(MESSAGE_PROPERTIES_FILE, resource_type='raw', public_id=MESSAGE_PROPERTIES_FILE, invalidate=True)
return("Done")
def get_properties(messageID):
property_db = load_properties()
if messageID in property_db:
return property_db[messageID]
else:
print(f"messageID {messageID} not found")
return {}