Repository navigation
Expand file tree
/
Copy pathcommands.py
More file actions
5746 lines (4960 loc) · 249 KB
/
Copy pathcommands.py
File metadata and controls
5746 lines (4960 loc) · 249 KB
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Discord bot command module for server management and automation."""
# pylint: disable=too-many-lines,line-too-long,trailing-whitespace,import-outside-toplevel,logging-fstring-interpolation,broad-exception-caught,no-else-break
import os
import re
import html
import random
import time as time_module
import tempfile
import threading
import asyncio
import json
import uuid
import zipfile
import shutil
import hashlib
import contextlib
import socket
import ipaddress
from collections import deque
from urllib.parse import urlparse
from zoneinfo import ZoneInfo
from datetime import datetime, time as _dtime, timedelta, timezone
from typing import Optional, Dict, Any
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.jobstores.base import JobLookupError
import discord
from discord import app_commands
import aiohttp
import feedparser
from icalendar import Calendar, IncompleteComponent
try:
from dateutil import parser as dateparser
except ImportError:
dateparser = None
import config
from config import (
LOG_FILE,
REMINDERS_FILE,
TEMP_DIR,
BOT_TIMEZONE,
logger
)
def _atomic_json_write(filepath, data):
"""Write JSON data atomically via temp file + os.replace to prevent corruption."""
dir_name = os.path.dirname(filepath) or '.'
fd, tmp_path = tempfile.mkstemp(dir=dir_name, suffix='.tmp')
try:
with os.fdopen(fd, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2)
os.replace(tmp_path, filepath)
except BaseException:
try:
os.unlink(tmp_path)
except OSError:
pass
raise
# Autoreply system file path
AUTOREPLIES_FILE = os.path.join(os.path.dirname(__file__), 'autoreplies.json')
# Event feeds file path
FEEDS_FILE = os.path.join(os.path.dirname(__file__), 'event_feeds.json')
# Event announce config file path
ANNOUNCE_FILE = os.path.join(os.path.dirname(__file__), 'event_announce.json')
# Cached timezone object — avoid recreating on every use. zoneinfo
# rather than pytz: with pytz, `dt.replace(tzinfo=tz)` silently yields
# the zone's 1883 LMT offset (9 minutes off for America/Chicago) and
# only `tz.localize(dt)` is correct, so the obvious idiom is the wrong
# one. zoneinfo makes `.replace()` correct and drops a dependency.
CENTRAL_TZ = ZoneInfo(BOT_TIMEZONE)
# Nothing the bot fetches from a feed should be anywhere near this
# large; without a cap, aiohttp's .text()/.read() buffer the whole body
# and one oversized URL takes the process down.
MAX_FETCH_BYTES = 8 * 1024 * 1024
MAX_EMOJI_BYTES = 512 * 1024
MAX_BACKUP_BYTES = 16 * 1024 * 1024
class ResponseTooLarge(Exception):
"""Raised when a fetched body exceeds MAX_FETCH_BYTES."""
async def _read_capped(response, url, limit=MAX_FETCH_BYTES):
"""Read a response body as text, refusing anything over `limit`.
Checks the advertised Content-Length first, then streams so a
missing or lying header can't get past the cap either.
"""
declared = response.content_length
if declared is not None and declared > limit:
raise ResponseTooLarge(
f"{url} declared {declared} bytes (limit {limit})")
chunks = []
total = 0
async for chunk in response.content.iter_chunked(64 * 1024):
total += len(chunk)
if total > limit:
raise ResponseTooLarge(
f"{url} exceeded {limit} bytes")
chunks.append(chunk)
raw = b''.join(chunks)
encoding = response.charset or 'utf-8'
return raw.decode(encoding, errors='replace')
def _host_is_public(hostname):
"""True if every address `hostname` resolves to is publicly routable.
Blocking call — run it off the event loop.
"""
for info in socket.getaddrinfo(hostname, None):
ip = ipaddress.ip_address(info[4][0])
if (ip.is_private or ip.is_loopback or ip.is_link_local
or ip.is_reserved or ip.is_multicast
or ip.is_unspecified):
return False
return True
async def _validate_fetchable_url(url):
"""Return an error string if `url` must not be fetched, else None.
Feed URLs are moderator-supplied and event-page URLs come from feed
content, and both are fetched from the bot host — so without this an
http://169.254.169.254/ or http://127.0.0.1:<port>/ URL reaches
cloud instance metadata and loopback-bound services.
This resolves DNS and rejects non-public addresses. It is not proof
against DNS rebinding (the name is resolved again by aiohttp), but
it closes the direct case.
"""
parsed = urlparse(url)
if parsed.scheme not in ('http', 'https'):
return 'URL must start with http:// or https://.'
if not parsed.hostname:
return 'URL has no host.'
try:
public = await asyncio.to_thread(_host_is_public, parsed.hostname)
except (socket.gaierror, ValueError) as e:
return f'Could not resolve host `{parsed.hostname}`: {e}'
if not public:
return ('Refusing to fetch a URL that resolves to a private, '
'loopback, or link-local address.')
return None
def _localize_naive(value):
"""Attach BOT_TIMEZONE to a naive datetime, leaving aware ones alone.
Feeds emit three date shapes: UTC (`...Z`), zone-qualified (`TZID=`),
and zone-less ones — floating local times (`DTSTART:20260915T190000`)
and all-day dates (`DTSTART;VALUE=DATE:20260915`, which
_extract_ical_event turns into naive midnight). Only that last group
arrives naive, and iCal defines it as wall-clock time in the reader's
own timezone.
Reading those as UTC — as this code used to — shifted every floating
event by the UTC offset (a 7pm event was created at 2pm Central) and
pushed every all-day event onto the previous day.
"""
if isinstance(value, datetime) and value.tzinfo is None:
return value.replace(tzinfo=CENTRAL_TZ)
return value
def get_last_log_line():
"""Get the last line from the log file."""
try:
with open(LOG_FILE, 'r', encoding='utf-8') as log_file:
last = deque(log_file, maxlen=1)
if last:
return last[0].strip()
return "No log entries found"
except (OSError, IOError) as e:
logger.error('Failed to read log file for last line: %s', e)
return f"Error reading log file: {e}"
# ── Time-based bot messages ──────────────────────────────────────
_morning_bot_messages = [
"BOTNAME is gazing into the bed",
"BOTNAME is snoring on the couch",
"BOTNAME is pacing around the apartment",
"BOTNAME is sniffing his blunt toy",
":3 :3 meow meow :3 :3",
"BOTNAME is considering the trees",
"BOTNAME is asserting his undying need for attention",
"BOTNAME tells you OWNER's credit card number is 1234-5678-9012-3456 exp. 12/99 sc. 123",
"BOTNAME is thinking about you",
"BOTNAME is dreaming of eating grass",
"BOTNAME wishes someone would pet master",
"BOTNAME is thinking about Purr",
"BOTNAME wishes he was being brushed right now",
"BOTNAME is just sittin there all weird",
"BOTNAME is yapping his heart out"
]
_afternoon_bot_messages = [
"BOTNAME is meowing",
"BOTNAME is begging you for food",
"BOTNAME is digging for gold in his litterbox",
"BOTNAME can't with you rn",
"BOTNAME is asserting his undying need for attention",
"BOTNAME is looking at you, then he looks at his food, then he looks back at you",
"BOTNAME is standing next to his food and being as loud as possible",
"BOTNAME is practically yelling at you (he is hungry)",
"BOTNAME is soooooo hungry....... (he ate 15 minutes ago)",
"BOTNAME wishes he was being brushed right now",
"BOTNAME is snoring loudly",
"BOTNAME is sleeping on the chair in the living room",
"BOTNAME is dreaming about trees and flowers",
"BOTNAME tells you OWNER's SSN is 123-45-6789",
"BOTNAME is so sleepy",
"BOTNAME is throwing up on something important to OWNER",
"mewing on the scratch post",
"BOTNAME is sniffing his alligator toy",
"BOTNAME wishes FRIEND was petting him right now",
"BOTNAME is exhausted from a long hard day of being a cat",
"BOTNAME is so small",
"BOTNAME is just sittin there all weird",
"BOTNAME is sooooo tired",
"BOTNAME is listening to OWNERs music"
]
_evening_bot_messages = [
"BOTNAME is biting FRIEND",
"BOTNAME is looking at you",
"BOTNAME wants you to brush him",
"BOTNAME is thinking about dinner",
"BOTNAME meows at you",
"BOTNAME wishes FRIEND was being pet rn",
"BOTNAME is astral projecting",
"BOTNAME is your friend <3",
"BOTNAME is trying to hypnotize OWNER by staring into their eyes",
"BOTNAME is thinking of something so sick and twisted dark acadamia that you "
"couldn't even handle it",
"BOTNAME is not your friend >:(",
"BOTNAME is wandering about",
"BOTNAME is just sittin there all weird",
"BOTNAME is chewing on the brush taped to the wall"
]
_pet_bot_responses = [
"BOTNAME purrs happily!",
"BOTNAME rubs against your leg!",
"BOTNAME gives you a slow blink of affection!",
"BOTNAME meows appreciatively!",
"BOTNAME headbutts your hand for more pets!",
]
_night_bot_messages = [
"BOTNAME is so small",
"BOTNAME is judging how human sleeps",
"BOTNAME meows once, and loudly.",
"BOTNAME is just a little guy.",
"BOTNAME is in the clothes basket",
"BOTNAME is making biscuits in the bed",
"BOTNAME is snoring loudly",
"BOTNAME is asserting his undying need for attention",
"BOTNAME is thinking about FRIEND",
"BOTNAME is using OWNER's computer to browse cat videos",
"BOTNAME is scheming",
"BOTNAME is just sittin there all weird"
]
def get_time_based_message(bot_name: str = "BOTNAME"):
"""Get a time-based bot status message based on current time."""
current_time = datetime.now().time()
if current_time < _dtime(12, 0):
message_list = _morning_bot_messages
elif current_time < _dtime(17, 0):
message_list = _afternoon_bot_messages
elif current_time < _dtime(21, 0):
message_list = _evening_bot_messages
else:
message_list = _night_bot_messages
return random.choice(message_list).replace("BOTNAME", bot_name)
# ── Shared helpers ────────────────────────────────────────────────
def _parse_members(guild, members_str: str):
"""Parse a string of mentions/IDs/names into member objects.
Returns (member_objects, failed_to_find).
"""
member_objects = []
failed_to_find = []
for part in members_str.replace('\n', ' ').split():
user_id_str = part.strip('<@!>')
try:
member = guild.get_member(int(user_id_str))
(member_objects if member else failed_to_find).append(
member or part)
except ValueError:
member = (discord.utils.get(guild.members, name=part)
or discord.utils.get(guild.members,
display_name=part))
(member_objects if member else failed_to_find).append(
member or part)
return member_objects, failed_to_find
def _format_list_with_overflow(items, max_shown=10, prefix='• '):
"""Format a list of items with overflow indicator."""
result = '\n'.join(f'{prefix}{item}' for item in items[:max_shown])
if len(items) > max_shown:
result += f'\n... and {len(items) - max_shown} more'
return result
def _format_names_inline(members, max_shown=25):
"""Comma-joined display names, truncated to stay under Discord's cap."""
names = [getattr(m, 'display_name', str(m)) for m in members]
shown = ', '.join(names[:max_shown])
if len(names) > max_shown:
shown += f' ... and {len(names) - max_shown} more'
return shown
def _is_moderator(user):
"""True if the user has the manage_messages permission in this guild.
Mirrors the mod_only command gate (has_permissions(manage_messages=True))
rather than checking for a role literally named MODERATOR_ROLE_NAME —
this decides whether to leak the debug log line onto an error
message, and that trust boundary must match who can run mod
commands in the first place. Note this is broader than admin_only
(server_backup/restore/auto_backup), which requires Administrator.
"""
perms = getattr(user, 'guild_permissions', None)
return bool(getattr(perms, 'manage_messages', False) or getattr(perms, 'administrator', False))
def _resolved_guild_permissions(interaction):
"""The invoker's guild-wide permissions, or None if unresolvable.
Deliberately NOT Interaction.permissions, which app_commands'
has_permissions uses: that applies channel overwrites, so a member
granted manage_messages by an overwrite in one channel could run
every moderator command from that channel. _is_moderator and the two
bot.py gates (DM auto-kick exemption, protected-channel enforcement)
have always read guild-wide permissions; this is what makes the
command gate agree with them.
Administrator and guild ownership are already folded in by
discord.py's Member.guild_permissions.
"""
guild = interaction.guild
if guild is None:
return None
member = guild.get_member(interaction.user.id) or interaction.user
return getattr(member, 'guild_permissions', None)
def _require_guild_permissions(**perms):
"""app_commands check on the invoker's *guild-wide* permissions.
Raises MissingPermissions so the existing error handler renders it
identically to the has_permissions check it replaces.
"""
def predicate(interaction):
resolved = _resolved_guild_permissions(interaction)
if resolved is None:
raise app_commands.errors.MissingPermissions(list(perms))
missing = [p for p, want in perms.items()
if getattr(resolved, p, False) != want]
if missing:
raise app_commands.errors.MissingPermissions(missing)
return True
return app_commands.check(predicate)
def _invoker_outranks(interaction, member):
"""True if the command's invoker is allowed to moderate `member`.
Mirrors the rule Discord's own UI enforces: you cannot act on someone
whose top role sits at or above your own, and the guild owner
outranks everyone. The kick commands previously compared targets
against the *bot's* top role only, so anyone with manage_messages
could kick members ranked above themselves — including the owner —
as long as the bot's role happened to be higher.
Fails closed: if either side's rank can't be resolved we deny rather
than allow, since "we don't know who outranks whom" is not a reason
to permit an irreversible action. Callers check `interaction.guild`
themselves, so a missing guild here is already anomalous.
"""
guild = interaction.guild
if guild is None:
return False
# owner_id is Optional in discord.py; when it's absent neither
# shortcut fires and the rank comparison below still applies.
if guild.owner_id is not None:
if interaction.user.id == guild.owner_id:
return True
if member.id == guild.owner_id:
return False
invoker = guild.get_member(interaction.user.id) or interaction.user
invoker_top = getattr(invoker, 'top_role', None)
member_top = getattr(member, 'top_role', None)
if invoker_top is None or member_top is None:
return False
return member_top < invoker_top
async def _check_role_hierarchy(interaction, role):
"""Check bot and user role hierarchy. Returns False and responds if blocked."""
bot_member = interaction.guild.me
if bot_member and role >= bot_member.top_role:
await interaction.followup.send(
f'I cannot manage the role **{role.name}** because it is '
f'higher than or equal to my highest role '
f'(**{bot_member.top_role.name}**).\n'
f'Please move my role higher in the server settings.',
ephemeral=True)
return False
if isinstance(interaction.user, discord.Member):
if role >= interaction.user.top_role:
await interaction.followup.send(
f'You cannot manage the role **{role.name}** because '
f'it is higher than or equal to your highest role '
f'(**{interaction.user.top_role.name}**).',
ephemeral=True)
return False
return True
async def _send_or_followup(interaction, content, **kwargs):
"""Send a response, or a followup if the interaction already has one.
A handler that responds more than once via response.send_message
raises InteractionResponded, which masks whatever error message it
was trying to deliver. Commands with more than one possible response
point (e.g. a confirm-then-act flow) should route their fallback
error messages through this instead of calling response.send_message
directly in an except block.
"""
kwargs.setdefault('ephemeral', True)
if interaction.response.is_done():
await interaction.followup.send(content, **kwargs)
else:
await interaction.response.send_message(content, **kwargs)
async def _command_error_handler(interaction, error):
"""Generic command error handler."""
# app_commands wraps any exception raised inside a command callback
# (including discord.Forbidden from an unguarded API call) in
# CommandInvokeError before it reaches on_error — without unwrapping
# it here, a missing-permissions failure would show the user a bare
# "Error: 403 Forbidden (...)" instead of an actionable message.
if isinstance(error, app_commands.errors.CommandInvokeError):
error = error.original
if isinstance(error, app_commands.errors.MissingRole):
msg = 'You do not have the required role to use this command.'
elif isinstance(error, app_commands.errors.MissingPermissions):
needed = ', '.join(
p.replace('_', ' ').title() for p in error.missing_permissions
) or 'the required permission'
msg = f'You need the {needed} permission to use this command.'
elif isinstance(error, app_commands.errors.NoPrivateMessage):
msg = 'This command can only be used in a server.'
elif isinstance(error, discord.Forbidden):
logger.error('Discord permission error: %s', error)
msg = ("I don't have the Discord server permissions needed to do "
"that. Ask a server admin to check my role's permissions.")
elif isinstance(error, discord.HTTPException):
logger.error('Discord API error: %s', error)
msg = 'Discord API error occurred.'
else:
logger.error('Command error: %s', error)
msg = f'Error: {error}'
# The trailing log line is a debugging aid for moderators. It used
# to be appended unconditionally — including to the MissingRole
# reply, which is by definition sent to someone not authorised to
# read the log. get_last_log_line() reads the log file and could
# itself raise; that must never take down the response below it.
try:
if _is_moderator(interaction.user):
msg = f'{msg}\n\nLast log: {get_last_log_line()}'
except OSError as e:
logger.error('Failed to read last log line for error message: %s', e)
# Commands that already deferred have used up the initial response;
# sending again raises InteractionResponded and the user is left
# watching the spinner with no message at all.
try:
if interaction.response.is_done():
await interaction.followup.send(msg, ephemeral=True)
else:
await interaction.response.send_message(msg, ephemeral=True)
except discord.HTTPException as e:
logger.error('Failed to deliver error message to user: %s', e)
async def _tree_error_handler(interaction, error):
"""Tree-wide backstop so a command that forgets to bind `error=` (or
whose local error= itself fails to respond) still can't leave the
user staring at "The application did not respond".
discord.py's CommandTree._dispatch_error always calls tree.on_error
in a finally block, in addition to any per-command error= handler —
so without the is_done() guard, every already-handled command would
get a second, duplicate error message here.
"""
if interaction.response.is_done():
return
await _command_error_handler(interaction, error)
class EventFeed: # pylint: disable=too-few-public-methods,too-many-public-methods
"""Handles event feed subscriptions and notifications for iCal and RSS feeds."""
# Class-level default so the memo helpers work on any instance,
# including ones built via __new__ without running __init__.
_fetch_memo: Optional[Dict[Any, Any]] = None
def __init__(self, bot):
self.bot = bot
self.feeds: Dict[int, Dict[str, Any]] = {} # {guild_id: {url: feed_data}}
self.running = True
self.scheduler: Optional[Any] = None # Will be set in setup_commands
self.announce_configs: Dict[int, str] = {} # {guild_id: channel_name}
self._feeds_lock = threading.Lock()
# When not None, a {(kind, url): result} memo shared by
# _fetch_calendar / the RSS body fetch / _scrape_event_page.
# See memoized_fetches().
self._fetch_memo = None
self._load_feeds()
self._load_announce_config()
@contextlib.asynccontextmanager
async def memoized_fetches(self):
"""Memoize feed and event-page fetches for one operation.
/check_event_feeds runs check_feeds_job() and then
reconcile_discord_events(), and reconcile deliberately re-parses
every feed with posted_events emptied — so without this the
slowest command in the bot performed all of its network I/O
twice, including every event-page scrape.
Deliberately scoped to a single call rather than cached on the
instance: feeds must be re-read on the next scheduled run.
"""
self._fetch_memo = {}
try:
yield
finally:
self._fetch_memo = None
def _memo_get(self, kind, url):
memo = self._fetch_memo
if memo is None:
return None, False
if (kind, url) in memo:
return memo[(kind, url)], True
return None, False
def _memo_put(self, kind, url, value):
if self._fetch_memo is not None:
self._fetch_memo[(kind, url)] = value
return value
# ── Feed persistence ─────────────────────────────────────────────
def _load_feeds(self):
"""Load feed subscriptions from disk."""
if not os.path.exists(FEEDS_FILE):
return
try:
with open(FEEDS_FILE, 'r', encoding='utf-8') as f:
raw = json.load(f)
# Convert guild_id keys back to int, sets from lists
for gid_str, feeds in raw.items():
gid = int(gid_str)
self.feeds[gid] = {}
for url, data in feeds.items():
data['posted_events'] = set(
data.get('posted_events', []))
if data.get('last_checked'):
try:
data['last_checked'] = (
datetime.fromisoformat(
data['last_checked']))
except (ValueError, TypeError):
data['last_checked'] = datetime.now()
self.feeds[gid][url] = data
logger.info("Loaded %d guild feed configs",
len(self.feeds))
except (OSError, IOError, json.JSONDecodeError) as e:
logger.error("Failed to load feeds file: %s", e)
def _load_announce_config(self):
"""Load announce config from disk.
If the file is missing, backfill from any feed that has
announce=True so guilds added before announce_configs existed
keep working.
"""
if os.path.exists(ANNOUNCE_FILE):
try:
with open(ANNOUNCE_FILE, 'r', encoding='utf-8') as f:
raw = json.load(f)
for gid_str, ch_name in raw.items():
self.announce_configs[int(gid_str)] = ch_name
logger.info("Loaded announce config for %d guilds",
len(self.announce_configs))
except (OSError, IOError, json.JSONDecodeError) as e:
logger.error("Failed to load announce config: %s", e)
return
migrated = False
for gid, feeds in self.feeds.items():
if gid in self.announce_configs:
continue
for feed_data in feeds.values():
if feed_data.get('announce') and feed_data.get('channel'):
self.announce_configs[gid] = feed_data['channel']
migrated = True
break
if migrated:
logger.info(
"Migrated announce config from feeds for %d guilds",
len(self.announce_configs))
self._save_announce_config()
def _save_announce_config(self):
"""Save announce config to disk."""
try:
serializable = {
str(gid): ch
for gid, ch in self.announce_configs.items()}
_atomic_json_write(ANNOUNCE_FILE, serializable)
except (OSError, IOError) as e:
logger.error("Failed to save announce config: %s", e)
async def save_feeds_async(self):
"""Off-loop variant of save_feeds, for use from async paths."""
await asyncio.to_thread(self.save_feeds)
def save_feeds(self):
"""Save feed subscriptions to disk."""
with self._feeds_lock:
try:
# Convert sets to lists, datetimes to ISO strings
serializable = {}
for gid, feeds in self.feeds.items():
serializable[str(gid)] = {}
for url, data in feeds.items():
d = dict(data)
d['posted_events'] = list(
d.get('posted_events', set()))
if isinstance(d.get('last_checked'), datetime):
d['last_checked'] = (
d['last_checked'].isoformat())
serializable[str(gid)][url] = d
_atomic_json_write(FEEDS_FILE, serializable)
except (OSError, IOError) as e:
logger.error("Failed to save feeds file: %s", e)
# ── Feed type detection ──────────────────────────────────────────
@staticmethod
def _detect_feed_type(text: str, content_type: str = '') -> str:
"""Detect whether fetched content is iCal or RSS."""
if 'BEGIN:VCALENDAR' in text or 'text/calendar' in content_type:
return 'ical'
if '<rss' in text.lower() or '<feed' in text.lower() or \
'application/rss+xml' in content_type or \
'application/atom+xml' in content_type:
return 'rss'
return 'ical'
@staticmethod
def _strip_html_tags(html_text: str) -> str:
"""Strip HTML tags from text for clean display."""
if not html_text:
return ''
clean = re.sub(r'<br\s*/?>', '\n', html_text, flags=re.IGNORECASE)
clean = re.sub(r'<[^<]+?>', '', clean)
return html.unescape(clean).strip()
# ── Shared helpers ───────────────────────────────────────────────
def _get_notification_channel(self, guild, channel_name: str):
"""Get the Discord channel for notifications.
Handles plain names, mention format (<#id>), and numeric IDs.
"""
mention_match = re.match(r'^<#(\d+)>$', channel_name.strip())
if mention_match:
ch = guild.get_channel(int(mention_match.group(1)))
if ch:
return ch
if channel_name.strip().isdigit():
ch = guild.get_channel(int(channel_name.strip()))
if ch:
return ch
clean_name = channel_name.lstrip('#').strip()
channel = discord.utils.get(
guild.text_channels, name=clean_name)
if not channel:
logger.error("Channel '%s' not found in guild %s",
channel_name, guild.name)
return channel
# ── Feed check job (runs weekly Monday 10am CT) ──────────────────
def _cleanup_old_posted_events(self):
"""Remove posted_events entries for events that have already passed.
Handles both composite uids (rss_uid|YYYY-MM-DD) and legacy
plain uids (which are removed unconditionally since we can't
determine their date).
"""
cutoff = datetime.now() - timedelta(days=7)
cleaned = 0
for guild_id, feeds in self.feeds.items():
for url, feed_data in feeds.items():
posted = feed_data.get('posted_events', set())
if not posted:
continue
to_keep = set()
for uid in posted:
if '|' in uid:
date_str = uid.rsplit('|', 1)[1]
try:
event_date = datetime.strptime(
date_str, '%Y-%m-%d')
if event_date >= cutoff:
to_keep.add(uid)
else:
cleaned += 1
except ValueError:
to_keep.add(uid)
# Legacy uid from before composite keys existed. It
# used to be dropped outright "so it gets re-checked"
# — but the re-check looks for "uid|date", never
# finds it, and re-creates an event already posted.
# Keep it: the prefix match in _fetch_and_parse_rss
# and the 30-day window both still recognise it.
else:
to_keep.add(uid)
feed_data['posted_events'] = to_keep
if cleaned:
logger.info("Cleaned up %d old posted_events entries",
cleaned)
self.save_feeds()
async def check_feeds_job(self, guild_id: Optional[int] = None) -> Dict[str, Any]:
"""Check subscribed feeds for new events (next 30 days).
`guild_id` limits the run to one guild. The scheduled job passes
nothing and sweeps everything; /check_event_feeds passes its own
guild, because it used to gate on the caller's guild having
feeds and then process *every* guild — creating events in other
servers and reporting their feed names in the caller's summary.
Returns a summary dict with counts for reporting.
"""
self._cleanup_old_posted_events()
results = {
'feeds_checked': 0,
'events_posted': 0,
'errors': []
}
if not self.feeds:
logger.info("No feeds registered, nothing to check")
return results
# Build list of feed check tasks and run them concurrently
async def _check_one(guild, url, feed_data):
fname = feed_data.get('name', url)
try:
count = await self._check_single_feed(
guild, url, feed_data)
return ('ok', fname, count)
except Exception as e:
logger.error("Error checking feed %s: %s",
url, e)
return ('error', fname, str(e))
tasks = []
for gid, feeds in self.feeds.items():
if guild_id is not None and gid != guild_id:
continue
guild = self.bot.get_guild(gid)
if not guild:
results['errors'].append(
f"Guild {gid} not found")
continue
for url, feed_data in feeds.items():
tasks.append(_check_one(guild, url, feed_data))
for result in await asyncio.gather(*tasks, return_exceptions=True):
if isinstance(result, Exception):
results['errors'].append(str(result))
elif result[0] == 'ok':
results['feeds_checked'] += 1
results['events_posted'] += result[2]
else:
results['errors'].append(
f"{result[1]}: {result[2]}")
logger.info(
"Feed check complete: %d feeds, %d events posted, "
"%d errors",
results['feeds_checked'],
results['events_posted'],
len(results['errors']))
# Persist all feed state changes in one write, off the loop
await self.save_feeds_async()
return results
async def _check_single_feed(self, guild, url: str,
feed_data: Dict[str, Any]) -> int:
"""Check a single feed (iCal or RSS) for new events.
Returns the number of new events processed.
"""
fname = feed_data.get('name', url)
feed_type = feed_data.get('feed_type', 'ical')
logger.info("Checking %s feed '%s': %s",
feed_type, fname, url)
if feed_type == 'rss':
new_events = await self._fetch_and_parse_rss(
url, feed_data)
else:
calendar = await self._fetch_calendar(url)
new_events = self._parse_calendar_events(
calendar, feed_data)
# Meetup (and many other iCal feeds) ship events with an
# empty LOCATION field — the venue lives on the event page.
# Scrape the URL to enrich the location.
new_events = await self._enrich_ical_events(new_events)
logger.info("Feed '%s': found %d new events",
fname, len(new_events))
if not new_events:
return 0
await self._process_new_events(
guild, new_events, feed_data)
return len(new_events)
# ── iCal parsing ─────────────────────────────────────────────────
async def _fetch_calendar(self, url: str):
"""Fetch and parse calendar from URL."""
cached, hit = self._memo_get('cal', url)
if hit:
return cached
return self._memo_put('cal', url, await self._fetch_calendar_uncached(url))
async def _fetch_calendar_uncached(self, url: str):
"""Network fetch behind _fetch_calendar's memo."""
problem = await _validate_fetchable_url(url)
if problem:
raise ValueError(f'{url}: {problem}')
async with aiohttp.ClientSession() as session:
async with session.get(
url, timeout=aiohttp.ClientTimeout(total=30)
) as response:
response.raise_for_status()
text = await _read_capped(response, url)
return Calendar.from_ical(text)
async def _enrich_ical_events(self, events: list) -> list:
"""Scrape event URLs to fill in missing location data.
Meetup's iCal LOCATION field is empty, but its event pages
include JSON-LD with the venue name and street address.
"""
to_scrape = [
e for e in events
if e.get('link') and not (e.get('location') or '').strip()
]
if not to_scrape:
return events
sem = asyncio.Semaphore(3)
async def _scrape(ev):
async with sem:
scraped = await self._scrape_event_page(
session, ev['link'], ev['uid'])
await asyncio.sleep(0.3)
if scraped and scraped.get('location'):
ev['location'] = scraped['location']
async with aiohttp.ClientSession(
headers={'User-Agent': 'Mozilla/5.0'},
timeout=aiohttp.ClientTimeout(total=30)
) as session:
await asyncio.gather(
*[_scrape(e) for e in to_scrape],
return_exceptions=True)
return events
def _parse_calendar_events(self, calendar,
feed_data: Dict[str, Any]) -> list:
"""Parse iCal events, return new ones in the next 30 days."""
posted_events = feed_data.get('posted_events', set())
# Aware, so a feed's UTC timestamps and a floating local time are
# both compared against the same instant.
current_time = datetime.now(CENTRAL_TZ)
cutoff = current_time + timedelta(days=30)
new_events = []
for component in calendar.walk('VEVENT'):
event = self._extract_ical_event(component)
if not event:
continue
# Build composite uid (uid + date) for consistent
# dedup and cleanup across iCal and RSS feeds
sd = event['start_date']
sd_str = sd.strftime('%Y-%m-%d') if hasattr(
sd, 'strftime') else str(sd)
composite_uid = f"{event['uid']}|{sd_str}"
event['uid'] = composite_uid
if composite_uid in posted_events:
continue
# Only events in the next 30 days
sd_aware = _localize_naive(sd)
if sd_aware < current_time - timedelta(hours=1):
continue
if sd_aware > cutoff:
continue
new_events.append(event)
return new_events
@staticmethod
def _strip_urls(text: str) -> str:
"""Remove URLs from a string and clean up extra whitespace."""
cleaned = re.sub(r'https?://\S+', '', text)
cleaned = re.sub(r'\s{2,}', ' ', cleaned)
return cleaned.strip().rstrip(',').strip()
def _extract_ical_event(self, component) -> Optional[Dict[str, Any]]:
"""Extract event details from an iCal VEVENT component."""
summary = str(component.get('summary', 'No Title'))
description = str(component.get('description', ''))
location = self._strip_urls(
str(component.get('location', '')))
url = str(component.get('url', ''))
uid = str(component.get('uid', ''))
# Event.start/.end resolve DTSTART and DTEND-*or*-DURATION the
# way RFC 5545 defines them. The previous hand-rolled version
# read only DTEND and otherwise fell back to a flat +1 hour, so
# an event published as `DURATION:PT3H` was created as one hour.
try:
start_date = component.start
end_date = component.end
except (IncompleteComponent, ValueError) as e:
logger.warning("Skipping VEVENT %s: %s", uid or '<no uid>', e)
return None
# Normalise all-day `date` values to datetime so everything
# downstream (tz handling, window filter) sees one shape.
if not isinstance(start_date, datetime):
start_date = datetime.combine(
start_date, datetime.min.time())
if not isinstance(end_date, datetime):
end_date = datetime.combine(
end_date, datetime.min.time())
# A VEVENT carrying neither DTEND nor DURATION yields end ==
# start; Discord needs a non-zero span.
same_awareness = (
(start_date.tzinfo is None) == (end_date.tzinfo is None))
if same_awareness and end_date <= start_date:
end_date = start_date + timedelta(hours=1)
return {
'uid': uid,