-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep-bot.py
552 lines (457 loc) · 18.5 KB
/
sleep-bot.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!/usr/bin/env python3
from collections import defaultdict
from datetime import date, datetime, time
import json
import os
from pathlib import Path
import tomllib as toml
from typing import Any, Optional
from zoneinfo import ZoneInfo
import discord
from discord import app_commands
from discord.app_commands import Choice
from discord.ext import tasks
TOKEN = None
AT_REED = True
CONFIG = Path(
os.getenv('SLEEP_BOT_CONFIG', '~/.config/sleep-bot')
).expanduser()
if not CONFIG.exists():
CONFIG.mkdir()
LOG_FILE = CONFIG/'log.json'
client = discord.Client()
SERVER_INFO = None
class ServerInfo:
"""The class representing server information."""
def __init__(
self, server_id: int, bed_channel: int,
insomniacs_role: int, patrol_role: int,
):
self.server_id = server_id
self.bed_channel = bed_channel
self.insomniacs_role = insomniacs_role
self.patrol_role = patrol_role
def init_files():
"""Makes sure all relevant files exist and are properly initialized."""
if not (CONFIG/'config.toml').exists():
raise FileNotFoundError("Configuration file not found. Exiting.")
init_file(CONFIG/'log.json', {})
init_file(CONFIG/'announcements.json', {})
init_file(CONFIG/'users.json', {})
init_file(CONFIG/'abroad.json', [])
init_file(CONFIG/'exemptions.json', {})
def init_file(path: Path, default: Any):
with open(path, 'w', encoding='utf8') as f:
if f.read().strip() == "":
json.dump(default, f)
def load_config():
"""Loads config and sets global variables accordingly."""
with open(CONFIG/'config.toml', 'r', encoding='utf8') as f:
config = toml.load(f)
global TOKEN, AT_REED, SERVER_INFO
TOKEN = config["token"]
AT_REED = config["at_reed"]
info = config["server"]
SERVER_INFO = ServerInfo(
info["id"],
info["bed_channel"],
info["insomniacs"],
info["patrol"],
)
class RoleNotFoundError(RuntimeError):
"""An error to throw if a role cannot be found."""
# TODO: Make this a config file?
TIME_ZONE_NAMES = {
"PST": ZoneInfo('America/Los_Angeles'),
"MST": ZoneInfo('America/Denver'),
"CST": ZoneInfo('America/Chicago'),
"EST": ZoneInfo('America/New_York'),
"IST": ZoneInfo('Asia/Kolkata'),
}
REED_TZ = ZoneInfo('America/Los_Angeles')
@client.event
async def on_message(message: discord.Message):
"""Handles messages and updates log file accordingly"""
insomniacs_role = message.guild.get_role(SERVER_INFO.insomniacs_role)
if insomniacs_role is None:
raise RoleNotFoundError("Could not find Insomniacs role")
if (message.author == client.user or
insomniacs_role not in message.author.roles):
return
time_zone = find_time_zone(message.author)
current_date = todays_date(time_zone)
sent_time = message.created_at.astimezone(time_zone)
if (not is_exempt(current_date, message.author.id) and
in_judgment_range(sent_time, current_date)):
# Log file format: dict[date -> dict[id -> timestamp]]
# Log file type: dict[str, dict[str, str]]
with open(LOG_FILE, 'w', encoding='utf8') as log:
data = json.load(log)
today = data[current_date.isoformat()]
# Note that JSON only allows strings as keys, so we have to convert
# to avoid rather nasty bugs
author_id = str(message.author.id)
if author_id not in today or today[author_id] <= sent_time:
today[author_id] = sent_time.time.isoformat()
json.dump(data, log)
@tasks.loop(time=time(7, 0, 0, tzinfo=REED_TZ))
async def show_results(guild: discord.Guild):
"""Send a message with the previous night's results to the server."""
current_date = todays_date(REED_TZ)
with open(LOG_FILE, 'r', encoding='utf8') as log:
data = json.load(log)
today = data[current_date.isoformat()]
results = create_results(current_date, today)
exempt = get_exemptions(current_date)
channel = client.get_channel(SERVER_INFO.bed_channel)
message = await channel.send(format_results(results, exempt, guild))
with open(CONFIG/'announcements.json', 'w', encoding='utf8') as f:
data = json.load(f)
data[current_date.isoformat()] = message.id
json.dump(data, f)
def create_results(day: date, data: dict[str, str]) -> dict[str, str]:
"""Create a dictionary mapping results to names.
This takes as input a dictionary of ids (formatted as strings) to
timestamps.
"""
result = defaultdict(list)
for identity, post_time in data.values():
bucket = result_bucket(day, time.fromisoformat(post_time))
name = user_name(int(identity))
result[bucket] += name
return result
class Result:
"""The class representing a result bracket."""
def __init__(self, name: str, start: str, end: str):
self.name = name
self.start = time.fromisoformat(start)
self.end = time.fromisoformat(end)
def start_time(self, zone: ZoneInfo) -> time:
"""The starting time for the bracket, relative to the given time zone.
"""
return self.start.replace(tzinfo=zone)
def end_time(self, zone: ZoneInfo) -> time:
"""The ending time for the bracket, relative to the given time zone."""
return self.end.replace(tzinfo=zone)
RESULTS = [
Result("Winner", "22:00:00", "00:00:00"),
Result("12-1", "00:00:00", "01:00:00"),
Result("1-2", "01:00:00", "02:00:00"),
Result("2-3", "02:00:00", "03:00:00"),
Result("bruh", "03:00:00", "04:00:00"),
Result("*my brother in christ*", "04:00:00", "05:00:00"),
Result("turbo cringe", "05:00:00", "06:00:00"),
]
WEEKEND = [
Result("Winner", "22:00:00", "01:00:00"),
Result("1-2", "01:00:00", "02:00:00"),
Result("2-3", "02:00:00", "03:00:00"),
Result("bruh", "03:00:00", "04:00:00"),
Result("*my brother in christ*", "04:00:00", "05:00:00"),
Result("turbo cringe", "05:00:00", "06:00:00"),
]
def todays_results(day: date) -> list[Result]:
"""Get the results for the given day."""
return RESULTS if day.weekday() < 5 else WEEKEND
def result_bucket(day: date, post_time: time) -> str:
"""Calculates which result the given timestamp is in, and then returns the
name of said bucket."""
for result in todays_results(day):
start = result.start_time(post_time.tzinfo())
end = result.end_time(post_time.tzinfo())
if start <= post_time < end:
return result.name
raise RuntimeError("Time does not go into any result bucket")
def format_results(
results: dict[str, list[str]],
exemptions: list[str],
server: discord.Guild,
) -> str:
"""Formats the night's results into a message.
This message mentions both the Insomniacs and Bedtime Patrol roles.
"""
res = "\n".join(
f"{r.name}: {', '.join(results[r.name])}" for r in todays_results(date)
)
exempt = f"Exemptions: {', '.join(exemptions)}"
insomniacs = server.get_role(SERVER_INFO.insomniacs_role).mention
patrol = server.get_role(SERVER_INFO.patrol_role).mention
return f"{insomniacs} {patrol} Last night's results:\n{exempt}\n{res}"
def user_name(identity: int) -> str:
"""Get the name used to refer to the given user."""
with open(CONFIG/'users.json', 'r', encoding='utf8') as data:
users = json.load(data)
name = users.get(str(identity))
return name or client.get_user(identity).display_name
def find_time_zone(user: discord.Member):
"""Gets the user's local time zone from their role."""
with open(CONFIG/'abroad.json', 'r', encoding='utf8') as f:
abroad = json.load(f)
if AT_REED and user.id not in abroad:
return REED_TZ
for role in user.roles:
zone = TIME_ZONE_NAMES.get(role.name)
if zone is not None:
return zone
return REED_TZ
def in_judgment_range(msg_time: datetime, current_date: date) -> bool:
"""Determine if a timestamp is in the judgement range for the current date.
Both `msg_time` and `current_date` should be relative to the same timezone.
"""
if msg_time.time >= time(20, 0, 0, tzinfo=datetime.tzinfo):
msg_date = msg_time.date + date.day
else:
msg_date = msg_time.date
return current_date == msg_date and (
current_date.time < end_time(datetime.tzinfo) or
current_date.time >= start_time(datetime.tzinfo)
)
def start_time(zone: ZoneInfo) -> time:
"""The start time for judged messages."""
time(22, 0, 0, tzinfo=zone)
def end_time(zone: ZoneInfo) -> time:
"""The end time for judged messages."""
time(6, 0, 0, tzinfo=zone)
def is_exempt(current_date: date, user_id: int) -> bool:
"""Determine if a user is exempt from the night's proceedings."""
# Format of exemptions.json: dict[date -> list[id]]
# Actual type: dict[str, list[int]]
with open(CONFIG/'exemptions.json', 'r', encoding='utf8') as exempt:
data = json.load(exempt)
today = data[current_date.isoformat()]
return user_id in today
def get_exemptions(current_date: date) -> list[str]:
"""Get the list of users who are exempt, formatted as the names to be
displayed."""
with open(CONFIG/'exemptions.json', 'r', encoding='utf8') as data:
data = json.load(data)
with open(CONFIG/'users.json', 'r', encoding='utf8') as users:
user_data = json.load(users)
today = data[current_date.isoformat()]
return [
user_data.get(str(u), client.get_user(u).display_name) for u in today
]
def todays_date(zone: ZoneInfo) -> date:
"""The current date, in the time zone given."""
current_time = datetime.now(tz=zone)
if current_time.time >= time(20, 0, 0, tzinfo=zone):
return current_time.date + date.day
else:
return current_time.date
@app_commands.command(name="name")
async def add_nickname(interaction: discord.Interaction, name: str):
"""Sets the name you would like to be called by the program when announcing
the night's results."""
with open(CONFIG/'users.json', 'w', encoding='utf8') as users:
data = json.load(users)
data[str(interaction.user.id)] = name
json.dump(data, users)
await interaction.response.send_message(f"Changed name to '{name}'")
@app_commands.command(name="name_other")
async def name_other(
interaction: discord.Interaction,
user: discord.Member,
name: str
):
"""Sets the name to be displayed for another user."""
with open(CONFIG/'users.json', 'w', encoding='utf8') as users:
data = json.load(users)
data[str(user.id)] = name
json.dump(data, users)
user_ping = user.mention
await interaction.response.send_message(
f"Changed {user_ping}'s name to '{name}'"
)
async def update_message(
server: discord.Guild,
user: discord.Member,
result=Result,
):
"""Updates the given user's info with the given result.
This does two things: updates the log file to reflect the change to the
user's state, and then updates the corresponding day's announcement message
to reflect the changes.
"""
current_date = todays_date(REED_TZ)
with open(LOG_FILE, 'w', encoding='utf8') as log:
data = json.load(log)
today = data[current_date.isoformat()]
today[str(user.id)] = result.start_time(find_time_zone(user))
json.dump(data, log)
with open(CONFIG/'announcements.json', 'r', encoding='utf8') as ann:
announcements = json.load(ann)
exempt = get_exemptions(today)
announcement = announcements.get(today.isoformat())
if announcement is not None:
channel_id, message_id = announcement
channel = client.get_channel(channel_id)
message = await channel.fetch_message(message_id)
await message.edit(
content=format_results(today, exempt, server)
)
def correct_result(index: int, day: date) -> Optional[Result]:
"""Maps weekday choices to weekend choices, when necessary.
Because Discord only allows command choices to be static, and the results
change on the weekend, we need a way to map the weekday choices to the
weekend one. This only works because the weekday choices are a superset of
the weekend ones. If the given choice is not valid on the weekend, this
returns None.
"""
if day.weekday() < 5:
return RESULTS[index]
weekday_result = RESULTS[index]
return next((x for x in WEEKEND if x.name == weekday_result.name), None)
def require_patrol(interaction: discord.Interaction) -> bool:
return interaction.user.get_role(SERVER_INFO.patrol_role) is not None
@app_commands.command(name="snitch")
@app_commands.choices(result=[
Choice(name=r.name, value=i) for i, r in enumerate(RESULTS)
])
async def snitch(
interaction: discord.Interaction,
user: discord.Member,
result=Choice[int]
):
"""Update someone else's sleep for the previous night."""
today = todays_date(REED_TZ)
todays_result = correct_result(result.value, today)
if todays_result is not None:
await update_message(interaction.guild, user, todays_result)
await interaction.response.send_message(
"Updated! Thank you for your service"
)
else:
await interaction.response.send_message(
f"Could not update; {result.name} is not a valid result for today"
)
@app_commands.command(name="confess")
@app_commands.choices(result=[
Choice(name=r.name, value=i) for i, r in enumerate(RESULTS)
])
async def confess(interaction: discord.Interaction, result=Choice[int]):
"""Update your sleep for the previous night."""
today = todays_date(REED_TZ)
todays_result = correct_result(result.value, today)
if todays_result is not None:
await update_message(
interaction.guild, interaction.user, todays_result
)
await interaction.response.send_message(
"Updated! Thank you for your honesty"
)
else:
await interaction.response.send_message(
f"Could not update; {result.name} is not a valid result for today"
)
@app_commands.command(name="exempt")
@app_commands.choices(day=[
Choice(name="today", value="today"),
Choice(name="tomorrow", value="tomorrow"),
])
@app_commands.check(require_patrol)
async def make_exempt(
interaction: discord.Interaction,
user: discord.Member,
day=Choice[str]
):
"""Marks a user as exempt from the night's list.
If the date is "today", then this refers to the current night. If an
announcement has been made, then this also edits the announcement to have
the correct information. There is no way yet to edit days before the most
recent.
"""
if day.value == "today":
today = todays_date(REED_TZ)
else:
today = todays_date(REED_TZ) + date.day
with open(CONFIG/'exemptions.json', 'w', encoding='utf8') as f:
exempt = json.load(f)
exempt.setdefault(today.isoformat(), [])
exempt[today.isoformat()].append(user.id)
json.dump(exempt, f)
if day.value == "today":
with open(LOG_FILE, 'w', encoding='utf8') as log:
data = json.load(log)
todays_data = data[today.isoformat()]
del todays_data[str(user.id)]
json.dump(data, log)
with open(
CONFIG/'announcements.json', 'w', encoding='utf8'
) as announcements:
data = json.load(announcements)
announcement = data.get(today.isoformat())
if announcement is not None:
channel_id, message_id = announcement
channel = client.get_channel(channel_id)
message = channel.fetch_message(message_id)
await message.edit(
content=format_results(todays_data, exempt, channel.guild)
)
await interaction.response.send_message(
f"{user.mention} has been added to {day.value}'s exempt list"
)
@app_commands.command(name="list_users")
async def list_users(interaction: discord.Interaction):
"""Lists all users currently with the insomniacs role, as well as with the
patrol role."""
server = interaction.guild
insomniacs = server.get_role(SERVER_INFO.insomniacs_role).members
patrol = server.get_role(SERVER_INFO.patrol_role).members
insomniac_names = '\n'.join(i.display_name for i in insomniacs)
patrol_names = '\n'.join(i.display_name for i in patrol)
await interaction.response.send_message(
f"Insomniacs:\n{insomniac_names}\nBedtime patrol:\n{patrol_names}"
)
@app_commands.command(name="out_of_town")
@app_commands.check(require_patrol)
async def out_of_town(interaction: discord.Interaction, user: discord.Member):
"""Sets a user as being out of town."""
with open(CONFIG/'abroad.json', 'w', encoding='utf8') as f:
data = json.load(f)
if user.id not in data:
data.append(user.id)
json.dump(data, f)
await interaction.response.send_message(
f"User {user.display_name} set as out of town"
)
@app_commands.command(name="in_town")
@app_commands.check(require_patrol)
async def in_town(interaction: discord.Interaction, user: discord.Member):
"""Removes a user from being out of town."""
with open(CONFIG/'abroad.json', 'w', encoding='utf8') as f:
data = json.load(f)
data.remove(user.id)
json.dump(data, f)
await interaction.response.send_message(
f"User {user.display_name} no longer out of town"
)
@app_commands.command(name="break")
@app_commands.choices(break_type=[
Choice(name="begin", value="begin"),
Choice(name="end", value="end"),
])
@app_commands.check(require_patrol)
async def set_break(interaction: discord.Interaction, choices: Choice[str]):
"""Toggles whether we are on break or not."""
at_reed = choices.value == "end"
with open(CONFIG/'config.toml', 'w', encoding='utf8') as f:
data = toml.load(f)
data.at_reed = at_reed
load_config()
await interaction.response.send_message(
"Break status set"
)
@app_commands.command(name="current_date")
async def whats_the_date(interaction: discord.Interaction):
"""Outputs the current date for the user that made the query."""
time_zone = find_time_zone(interaction.user)
the_date = todays_date(time_zone)
reed_date = todays_date(REED_TZ)
await interaction.response.send_message(
f"The current date for you is {the_date.isoformat()}\n"
+ f"The date at Reed is {reed_date.isoformat()}"
)
if __name__ == "__main__":
init_files()
load_config()
client.run(TOKEN)