-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataIO.py
277 lines (235 loc) · 8.68 KB
/
dataIO.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
import json
import os
import aiofiles
from datetime import datetime
import discord
def get_time():
now = datetime.now()
time = now.strftime("%Y-%m-%d-%H-%M-%S")
return(time)
def get_int(time):
integer = ""
for char in time:
if char.isnumeric():
integer += char
integer = int(integer)
return(integer)
def validate_time(time):
while time[5] >= 60:
time[5] -= 60
time[4] += 1
while time[4] >= 60:
time[4] -= 60
time[3] += 1
while time[3] >= 24:
time[3] -= 12
time[2] += 1
while time[2] >= 30:
time[2] -= 30
time[1] += 1
while time[1] >= 12:
time[1] -= 12
time[0] += 1
t2 = []
for t in time:
if len(str(t)) == 1:
t2.append(f"0{str(t)}")
else:
t2.append(str(t))
return(f"{t2[0]}-{t2[1]}-{t2[2]}-{t2[3]}-{t2[4]}-{t2[5]}")
def convert_time(time):
duration = [0, 0, 0, 0, 0, 0]
if time.endswith("s"):
duration[5] = int(time[:-1])
if time.endswith("m"):
duration[4] = int(time[:-1])
if time.endswith("h"):
duration[3] = int(time[:-1])
if time.endswith("d"):
duration[2] = int(time[:-1])
if time.endswith("y"):
duration[0] = int(time[:-1])
duration = validate_time(duration)
return(duration)
def get_date(duration):
time = get_time()
newtime = []
newduration = []
for x in time.split("-"):
newtime.append(int(x))
for x in duration.split("-"):
newduration.append(int(x))
finaltime = []
for x in range(6):
finaltime.append(newtime[x] + newduration[x])
finaltime = validate_time(finaltime)
return(finaltime)
class Logger():
async def filter(self, message, words):
if not os.path.exists(f"./data/{message.guild.id}"):
os.mkdir(f"./data/{message.guild.id}")
async with aiofiles.open(f"./data/{message.guild.id}/chat_filter.log", mode="a") as logfile:
await logfile.write(f"[{get_time()}] | channel: {message.channel} | user: {message.author} [{message.author.mention}] | used banned words: {words} \nin message: {message.content}\n\n")
return
class Guild_data():
def __init__(self, guild, setup):
if not os.path.exists(f"./data/{guild.id}"):
os.mkdir(f"./data/{guild.id}")
if not os.path.exists(f"./data/{guild.id}/chat_filter.log"):
file = open(f"./data/{guild.id}/chat_filter.log", "w")
file.write(
f"\n# chat filter logs from server {guild.name} [{guild.id}]\n\n"
)
if not os.path.exists(f"./data/{guild.id}/server_data.json"):
file = open(f"./data/{guild.id}/server_data.json", "w")
file.write('{ "test" : "test" }')
file.close()
if setup == False:
return("You have not setup my functions on this server yet! to do so run '$ setup' this is necessary in order to store any data about the guild")
self.filename = f"./data/{guild.id}/server_data.json"
with open(self.filename) as file:
self.data = json.load(file)
return
def update(self):
with open(self.filename, "w") as file:
data = json.dumps(self.data, sort_keys=True, indent=4)
file.write(data)
def get_user(self, member):
self.data["members"][member.mention]["mention"] = member.mention
self.data["members"][member.mention]["id"] = member.id
self.data["members"][member.mention]["known-name"] = member.name
data = self.data["members"][member.mention]
return(data)
self.update()
def get_rules(self):
rules = []
for rule in self.data["server-rules"]:
rules.append((rule, self.data["server-rules"][rule]))
return(rules) # returns a tuple (name, description)
def add_rule(self, rule):
# rule is a tuple (name, description)
self.data["server-rules"][rule[0]] = f"{rule[1]}"
print(dict(sorted(self.data["server-rules"].items())))
self.data["server-rules"] = dict(
sorted(self.data["server-rules"].items())
)
self.update()
return("Successfully added rule")
def remove_rule(self, rule):
if rule in self.data["server-rules"]:
del self.data["server-rules"][rule]
self.update()
return("Successfully removed rule")
else:
self.update()
return("This rule does not exist, try providing the number / name of the role and not the description")
def get_bans(self):
bans = []
if "ban-list" in self.data:
for ban in self.data["ban-list"]:
bans.append(ban)
else:
bans.append(None)
self.update()
return
def get_mutes(self):
bans = []
if "mute-list" in self.data:
for ban in self.data["mute-list"]:
mutes.append(mute)
else:
mutes.append(None)
self.update()
return
def update_bans(self, guild, ban_profile):
if ban_profile != None:
self.data["ban-list"][ban_profile.user_id] = {
"reason": f"{ban_profile.reason}",
"initial-duration": f"{ban_profile.duration}",
"expiry-date": f"{ban_profile.expiry}"
}
# updates all bans
removes = []
for ban in self.data["ban-list"]:
if get_int(self.data["ban-list"][ban]["expiry-date"]) <= get_int(get_time()):
removes.append(ban)
for ban in removes:
del self.data["ban-list"][ban]
self.update()
return
def update_mutes(self, guild, mute_profile):
if mute_profile != None:
self.data["mute-list"][mute_profile.user_id] = {
"reason": f"{mute_profile.reason}",
"initial-duration": f"{mute_profile.duration}",
"expiry-date": f"{mute_profile.expiry}"
}
# updates all mutes
removes = []
print(self.data["mute-list"])
for mute in self.data["mute-list"]:
print(get_int(self.data["mute-list"][mute]
["expiry-date"]), get_int(get_time()))
if get_int(self.data["mute-list"][mute]["expiry-date"]) <= get_int(get_time()):
removes.append(mute)
for mute in removes:
del self.data["mute-list"][mute]
self.update()
return
def add_suggestion(self, member, suggestion):
self.data["suggestions"][get_time()] = {
"mention": f"{member.mention}",
"suggestion": f"{suggestion}"
}
self.update()
return
async def server_setup(self, guild, channels):
default_rules = {
"1:": "please keep bad language to a minimum",
"2:": "no NSFW content of any kind or links to such, this also includes malicious content or links",
"3:": "please keep this server friendly and relaxed",
"4:": "avoid controversial or political topics",
"5:": "absolutely no bullying or discrimination",
"6:": "please send messages in the correct channels; keep things relevant",
"7:": "please dont ping the owners / admins unless it is important, it gets annoying"
}
self.data = {
"header": {
"server-name": f"{guild.name}",
"server-id": f"{guild.id}",
"server-owner": f"{guild.owner_id}"
},
"members": {},
"ban-list": {},
"mute-list": {},
"channels": channels,
"server-prefixes": {"moderation": "$ ", "chatbot": ". ", "normal": "? "},
"server-suggestions": {},
"server-rules": default_rules
}
for member in guild.members:
try:
await guild.fetch_ban(member)
expiry = "never"
except discord.NotFound:
expiry = "none"
self.data["members"][f"{member.mention}"] = {
"known-name": f"{member.name}",
"mention": f"{member.mention}",
"id": f"{member.id}",
"punishments": {
"ban-reason": "none",
"ban-expiry": f"{expiry}",
"mute-reason": "none",
"mute-expiry": "none"
},
"level": "1"
}
self.update()
return
class Punishment_profile():
def __init__(self, user_id, duration, expiry, reason):
self.user_id = user_id
self.duration = duration
self.expiry = expiry
self.reason = reason