-
Notifications
You must be signed in to change notification settings - Fork 1
/
info.py
418 lines (341 loc) · 15.5 KB
/
info.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import os
import json
import discord
import datetime
from discord.ext import commands
class Info():
def __init__(self):
self.info_fn = "info.json" # fn = filename
self.datetime_format = "%d/%m/%Y %H:%M"
self.default_presets = ["monday", "wednesday", "friday"]
if os.path.exists(self.info_fn):
with open(self.info_fn, "r") as fn:
self.info = json.load(fn)
else:
self.info = {
"emojis": {
"jigsaw": ":jigsaw:",
"brain": ":brain:",
"speech": ":speech_balloon:",
"heart": ":heart:",
"cross": ":x:"
},
"monday": {
"role_name": "weekly puzzles",
"channel_id": 892032997220573204,
"release_datetime": "19/02/2024 16:00",
"week_num": 0,
"img_urls": [],
"submission_link": "",
"interactive_link": "",
"releasing": False
},
"wednesday": {
"role_name": "weekly puzzles",
"channel_id": 892032997220573204,
"release_datetime": "21/02/2024 16:00",
"week_num": 0,
"img_urls": [],
"submission_link": "",
"interactive_link": "",
"releasing": False
},
"friday": {
"role_name": "weekly puzzles",
"channel_id": 892032997220573204,
"release_datetime": "23/02/2024 16:00",
"week_num": 0,
"img_urls": [],
"submission_link": "",
"interactive_link": "",
"releasing": False
},
"rebuscryptic": {
"role_name": "weekly puzzles",
"channel_id": 892032997220573204,
"release_datetime": "08/08/2022 16:00",
"week_num": -1,
"img_urls": [],
"submission_link": "",
"interactive_link": "",
"releasing": False
},
"minipuzz": {
"role_name": "weekly puzzles",
"channel_id": 892032997220573204,
"release_datetime": "08/08/2022 16:00",
"week_num": -1,
"img_urls": [],
"submission_link": "",
"interactive_link": "",
"releasing": False
},
"crossword": {
"role_name": "crosswords",
"channel_id": 1074683905405358171,
"release_datetime": "08/08/2022 16:00",
"week_num": -1,
"img_urls": [],
"submission_link": "",
"interactive_link": "",
"releasing": False
},
"wordsearch": {
"role_name": "word searches",
"channel_id": 1135184991928721448,
"release_datetime": "08/08/2022 16:00",
"week_num": -1,
"img_urls": [],
"submission_link": "",
"interactive_link": "",
"releasing": False
},
"logicpuzz": {
"role_name": "logic puzzles",
"channel_id": 1074684130794672138,
"release_datetime": "08/08/2022 16:00",
"week_num": -1,
"img_urls": [],
"submission_link": "",
"interactive_link": "",
"releasing": False
},
"ciyk": {
"role_name": "weekly games",
"channel_id": 1001742058601590824,
"discuss_id": 1001742642427744326,
"release_datetime": "08/08/2022 16:00",
"week_num": -1,
"img_urls": [],
"submission_link": "",
"interactive_link": "",
"releasing": False
}
}
self.minipuzz_datetime = self.str_to_datetime(self.info["minipuzz"]["release_datetime"])
self.ciyk_datetime = self.str_to_datetime(self.info["ciyk"]["release_datetime"])
self.day_names = {
0: "Monday",
1: "Tuesday",
2: "Wednesday",
3: "Thursday",
4: "Friday",
5: "Saturday",
6: "Sunday"
}
def check_preset(self, preset: str):
shortened_presets = {
"mon": "monday",
"wed": "wednesday",
"fri": "friday"
}
preset = preset.lower()
try:
preset = shortened_presets[preset]
except KeyError:
pass
if preset not in self.default_presets:
return preset, False
return preset, True
# expects the %d/%m/%Y %H:%M format
def str_to_datetime(self, string: str) -> datetime.datetime:
date, time = string.split()
day, month, year = date.split("/")
hour, minute = time.split(":")
return datetime.datetime(int(year), int(month), int(day), int(hour), int(minute))
def check_is_date(self, msg: str):
try:
strday, strmonth, stryear = msg.split("/")
# check if it is a valid date
date = datetime.date(int(stryear), int(strmonth), int(strday))
day, month, year = int(strday), int(strmonth), int(stryear)
return day, month, year
except ValueError:
return False
def check_is_time(self, msg: str):
try:
strhour, strminute = msg.split(":")
# check if valid time
time = datetime.time(int(strhour), int(strminute))
hour, minute = int(strhour), int(strminute)
return hour, minute
except ValueError:
return False
# each get_text function needs to read from the json file since the info could have been updated
# this could potential pose problems if a read and write occur at the same time
# however, this shouldn't be a big issue as the commands that use these functions will only be accessible by a few people
def get_rebuscryptic_text(self, ctx: commands.context.Context, mention: bool) -> str:
with open(self.info_fn, "r") as fn:
self.info = json.load(fn)
puzz_info = self.info["rebuscryptic"]
role_name = puzz_info["role_name"]
if mention:
puzz_tag = f"{discord.utils.get(ctx.guild.roles, name=role_name).mention}\n\n"
else:
puzz_tag = f"@/{discord.utils.get(ctx.guild.roles, name=role_name)}\n\n"
lines = [
puzz_tag,
f"**WEEKLY PUZZLE COMPETITION: WEEK {puzz_info['week_num']}**\n",
"**REBUS + CRYPTIC**\n\n",
"_Hints will be unlimited after the top 3 solvers have finished!_\n\n",
f"Submit your answers here: {puzz_info['submission_link']}\n\n",
"_You can submit as many times as you want!_\n",
"_Your highest score will be kept._"
]
return "".join(lines)
def get_qtext(self, ctx: commands.context.Context, mention: bool, preset: str) -> str:
with open(self.info_fn, "r") as fn:
self.info = json.load(fn)
puzz_info = self.info[preset]
role_name = puzz_info["role_name"]
week_num = puzz_info["week_num"]
if mention:
puzz_tag = f"{discord.utils.get(ctx.guild.roles, name=role_name).mention}\n\n"
else:
puzz_tag = f"@/{discord.utils.get(ctx.guild.roles, name=role_name)}\n\n"
preset_puzzles = {
"wednesday": "**MINIPUZZLE**\n\n",
"friday": "**PRINTER'S DEVILRY**\n\n"
}
puzzle_list = ""
if preset == "monday":
if week_num % 2 == 0:
puzzle_list = "**LOGIC + CRYPTIC**\n\n"
else:
puzzle_list = "**REBUS + CRYPTIC**\n\n"
else:
puzzle_list = preset_puzzles[preset]
lines = [
puzz_tag,
f"**WEEKLY PUZZLE COMPETITION: WEEK {puzz_info['week_num']}**\n",
puzzle_list,
"_Hints will be unlimited after the top 3 solvers have finished!_\n\n",
f"Submit your answers here: {puzz_info['submission_link']}\n\n",
"_You can submit as many times as you want!_\n",
"_Your highest score will be kept._"
]
if puzz_info["interactive_link"]:
lines.append(f"\n\nInteractive version: {puzz_info['interactive_link']}")
return "".join(lines)
# mention lets the function know whether the role should be tagged
def get_minipuzz_text(self, ctx: commands.context.Context, mention: bool) -> str:
with open(self.info_fn, "r") as fn:
self.info = json.load(fn)
puzz_info = self.info["minipuzz"]
role_name = puzz_info["role_name"]
if mention:
puzz_tag = f"{discord.utils.get(ctx.guild.roles, name=role_name).mention}\n\n"
else:
puzz_tag = f"@/{discord.utils.get(ctx.guild.roles, name=role_name)}\n\n"
lines = [
puzz_tag,
f"**WEEKLY PUZZLE COMPETITION: WEEK {puzz_info['week_num']}**\n",
"**MINI-PUZZLE**\n\n",
"_Hints will be unlimited after the top 3 solvers have finished!_\n\n",
f"Submit your answers here: {puzz_info['submission_link']}\n\n",
"_You can submit as many times as you want!_\n",
"_Your highest score will be kept._"
]
interactive_link = puzz_info["interactive_link"]
if interactive_link:
lines.append(f"\n\nInteractive version: {interactive_link}")
return "".join(lines)
def get_crossword_text(self, ctx: commands.context.Context, mention: bool) -> str:
with open(self.info_fn, "r") as fn:
self.info = json.load(fn)
crossword_info = self.info["crossword"]
role_name = crossword_info["role_name"]
if mention:
crossword_tag = f"{discord.utils.get(ctx.guild.roles, name=role_name).mention}\n\n"
else:
crossword_tag = f"@/{discord.utils.get(ctx.guild.roles, name=role_name)}\n\n"
lines = [
crossword_tag,
f"**CROSSWORD: WEEK {crossword_info['week_num']}**"
]
interactive_link = crossword_info["interactive_link"]
if interactive_link:
lines.append(f"\n\nInteractive version: {interactive_link}")
return "".join(lines)
def get_wordsearch_text(self, ctx: commands.context.Context, mention: bool) -> str:
with open(self.info_fn, "r") as fn:
self.info = json.load(fn)
wordsearch_info = self.info["wordsearch"]
role_name = wordsearch_info["role_name"]
if mention:
wordsearch_tag = f"{discord.utils.get(ctx.guild.roles, name=role_name).mention}\n\n"
else:
wordsearch_tag = f"@/{discord.utils.get(ctx.guild.roles, name=role_name)}\n\n"
lines = [
wordsearch_tag,
f"**WORD SEARCH: WEEK {wordsearch_info['week_num']}**"
]
return "".join(lines)
def get_logicpuzz_text(self, ctx: commands.context.Context, mention: bool) -> str:
with open(self.info_fn, "r") as fn:
self.info = json.load(fn)
logicpuzz_info = self.info["logicpuzz"]
role_name = logicpuzz_info["role_name"]
if mention:
logicpuzz_tag = f"{discord.utils.get(ctx.guild.roles, name=role_name).mention}\n\n"
else:
logicpuzz_tag = f"@/{discord.utils.get(ctx.guild.roles, name=role_name)}\n\n"
lines = [
logicpuzz_tag,
f"**LOGIC PUZZLE: WEEK {logicpuzz_info['week_num']}**"
]
interactive_link = logicpuzz_info["interactive_link"]
if interactive_link:
lines.append(f"\n\nInteractive version: {interactive_link}")
return "".join(lines)
def get_ciyk_text(self, ctx: commands.context.Context, mention: bool) -> str:
with open(self.info_fn, "r") as fn:
self.info = json.load(fn)
emojis = self.info["emojis"]
ciyk_info = self.info["ciyk"]
role_name = ciyk_info["role_name"]
if mention:
ciyk_tag = f"{discord.utils.get(ctx.guild.roles, name=role_name).mention}\n\n"
else:
ciyk_tag = f"@/{discord.utils.get(ctx.guild.roles, name=role_name)}\n\n"
line1 = f'**COMMENT IF YOU KNOW: WEEK {ciyk_info["week_num"]}**\n\n'
line2 = f'If you think you know the pattern, comment an answer that follows it in <#{ciyk_info["discuss_id"]}>\n'
line3 = f'We\'ll react with a {emojis["heart"]} if you\'re right and a {emojis["cross"]} if you\'re wrong!\n\n'
return ciyk_tag + line1 + line2 + line3 + ciyk_info["img_url"]
def get_text(self, ctx: commands.context.Context, puzz_name: str, mention: bool):
get_text = {
"rebuscryptic": self.get_rebuscryptic_text,
"minipuzz": self.get_minipuzz_text,
"crossword": self.get_crossword_text,
"wordsearch": self.get_wordsearch_text,
"logicpuzz": self.get_logicpuzz_text,
"ciyk": self.get_ciyk_text,
}
return get_text[puzz_name](ctx, mention)
# this method exists as just an easy way to change the data in one method call in setpuzzles/setciyk
def change_data(self, puzz_name: str, new_data: "dict[str, any]"):
puzz_data = ["week_num", "submission_link",
"img_urls", "interactive_link"
]
for data in puzz_data:
self.info[puzz_name][data] = new_data[data]
# write the new info to the json file so that it is not lost if the bot shuts down
with open(self.info_fn, "w") as info:
new_json = json.dumps(self.info, indent=4)
info.write(new_json)
def change_time(self, puzz_name: str, new_time: datetime.datetime):
self.info[puzz_name]["release_datetime"] = new_time.strftime(self.datetime_format)
with open(self.info_fn, "w") as info:
new_json = json.dumps(self.info, indent=4)
info.write(new_json)
def change_week(self, puzz_name: str, new_week: int):
self.info[puzz_name]["week_num"] = new_week
with open (self.info_fn, "w") as info:
new_json = json.dumps(self.info, indent=4)
info.write(new_json)
# changes the state of whether a puzzle is releasing
def change_release(self, puzz_name: str, is_releasing: bool):
self.info[puzz_name]["releasing"] = is_releasing
with open(self.info_fn, "w") as info:
new_json = json.dumps(self.info, indent=4)
info.write(new_json)