-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
1313 lines (1127 loc) · 48.4 KB
/
main.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
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
import os
import discord
import asyncio
import datetime
import json
import pytz
import time
import requests
from datetime import timezone
from discord import app_commands
from discord.ext import commands, tasks
from dotenv import load_dotenv
load_dotenv()
token = os.getenv("token")
DATA_FILE = "star_caller_data.json"
api = os.getenv("api")
AUTHORIZED_SERVER_IDS = [
1274620800896339968, #development
282907227017183232, #star-find
]
class StarCaller(commands.Bot):
def __init__(self):
super().__init__(
command_prefix="",
intents=discord.Intents.all(),
)
async def setup_hook(self):
self.auto_clear_restricted.start()
await self.tree.sync()
async def on_ready(self):
await self.change_presence(
activity=discord.CustomActivity(name="invoke /call to spot a star!")
)
async def close(self):
if hasattr(self, 'message_queue'):
await self.message_queue.stop()
await super().close()
def cog_unload(self):
self.auto_clear_restricted.cancel()
@tasks.loop(minutes=30)
async def auto_clear_restricted(self):
try:
class DummyResponse:
async def send_message(self, content, ephemeral=False):
return None
async def defer(self):
pass
class DummyFollowup:
async def send(self, content):
class DummyMessage:
async def edit(self, content):
pass
return DummyMessage()
class DummyInteraction:
def __init__(self, client):
self.client = client
self.response = DummyResponse()
self.followup = DummyFollowup()
dummy_interaction = DummyInteraction(self)
command = self.tree.get_command("clear-restricted")
if command:
await command.callback(dummy_interaction)
#print(f"[{datetime.datetime.now(pytz.UTC)}] Invoked clear-restricted.")
else:
print("Clear-restricted command not found")
except Exception as e:
print(f"Error in auto_clear_restricted: {str(e)}")
import traceback
traceback.print_exc()
@auto_clear_restricted.before_loop
async def before_auto_clear(self):
await self.wait_until_ready()
client = StarCaller()
client.clear_lock = asyncio.Lock()
class MessageQueue:
def __init__(self):
self.queue = asyncio.Queue()
self.task = None
self.running = True
async def process_queue(self):
while self.running:
try:
message, content = await self.queue.get()
await message.edit(content=content)
self.queue.task_done()
await asyncio.sleep(6)
except asyncio.CancelledError:
break
except Exception as e:
print(f"Error processing message edit: {e}")
def start(self):
self.task = asyncio.create_task(self.process_queue())
async def stop(self):
self.running = False
if self.task:
await self.queue.join()
self.task.cancel()
try:
await self.task
except asyncio.CancelledError:
pass
def load_table_data():
try:
with open(DATA_FILE, 'r') as f:
loaded_data = json.load(f)
return loaded_data
except FileNotFoundError:
return {
"is_locked": False,
"entries": [],
"message_id": None,
"channel_id": None,
"chunk_message_ids": []
}
def save_table_data(data):
with open(DATA_FILE, 'w') as f:
json.dump(data, f)
def check_authorized_server():
async def predicate(interaction: discord.Interaction) -> bool:
if interaction.guild_id not in AUTHORIZED_SERVER_IDS:
await interaction.response.send_message(
"This command can only be used in authorized servers.",
ephemeral=True
)
return False
return True
return app_commands.check(predicate)
def is_valid_size(size_value):
return size_value.startswith('s') and size_value[1:].isdigit()
def is_valid_game_time(time_str: str) -> bool:
try:
if len(time_str.split(':')) != 2:
return False
hours, minutes = map(int, time_str.split(':'))
return 0 <= hours <= 23 and 0 <= minutes <= 59
except (ValueError, AttributeError):
return False
def is_valid_game_time_full(time_str: str) -> bool:
try:
datetime.datetime.fromisoformat(time_str)
return True
except (ValueError, TypeError):
return False
@client.tree.error
async def on_app_command_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
if isinstance(error, app_commands.errors.CheckFailure):
if not interaction.response.is_done():
await interaction.response.send_message(
"You are not authorized to use this command.",
ephemeral=True
)
else:
print(f"Slash command error occurred: {str(error)}")
if not interaction.response.is_done():
await interaction.response.send_message(
f"An error occurred while processing the command: \n{error}",
ephemeral=True
)
@client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
return
print(f"Message command error occurred: {str(error)}")
def make_post_request(url, data):
try:
response = requests.post(url, json=data)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
#print(f"Error making POST request: {e}")
if response := getattr(e, 'response', None):
print(f"Response status code: {response.status_code}")
print(f"Response body: {response.text}")
return None
table_data = load_table_data()
world_data = [
(1, "Members"),
(2, "Members"),
(3, "Free-to-play"),
(4, "Members"),
(5, "Members"),
(6, "Members"),
(7, "Free-to-play"),
(8, "Free-to-play"),
(9, "Members"),
(10, "Members"),
(11, "Free-to-play"),
(12, "Members"),
(14, "Members"),
(15, "Members"),
(16, "Members"),
(17, "Free-to-play"),
(18, "Members"),
(19, "Free-to-play"),
(20, "Free-to-play"),
(21, "Members"),
(22, "Members"),
(23, "Members"),
(24, "Members"),
(25, "Members"),
(26, "Members"),
(27, "Members"),
(28, "Members"),
(29, "Free-to-play"),
(30, "Members", "Special"),
(31, "Members"),
(32, "Members"),
(33, "Free-to-play"),
(34, "Free-to-play"),
(35, "Members"),
(36, "Members"),
(37, "Members"),
(38, "Free-to-play"),
(39, "Members"),
(40, "Members"),
(41, "Free-to-play"),
(42, "Members"),
(43, "Free-to-play"),
(44, "Members"),
(45, "Members"),
(46, "Members"),
(47, "Members", "Local"),
(48, "Members", "Special"),
(49, "Members"),
(50, "Members"),
(51, "Members"),
(52, "Members", "Special"),
(53, "Members"),
(54, "Members"),
(55, "Free-to-play", "Local"),
(56, "Members"),
(57, "Free-to-play"),
(58, "Members"),
(59, "Members"),
(60, "Members"),
(61, "Free-to-play"),
(62, "Members"),
(63, "Members"),
(64, "Members"),
(65, "Members"),
(66, "Members"),
(67, "Members"),
(68, "Members"),
(69, "Members"),
(70, "Members"),
(71, "Members"),
(72, "Members"),
(73, "Members"),
(74, "Members"),
(75, "Members", "Local"),
(76, "Members"),
(77, "Members"),
(78, "Members"),
(79, "Members"),
(80, "Free-to-play"),
(81, "Free-to-play"),
(82, "Members"),
(83, "Members"),
(84, "Members"),
(85, "Members"),
(86, "Members", "Special"),
(87, "Members"),
(88, "Members"),
(89, "Members"),
(91, "Members"),
(92, "Members"),
(94, "Free-to-play", "Local"),
(96, "Members"),
(97, "Members"),
(98, "Members"),
(99, "Members"),
(100, "Members"),
(101, "Members", "Local"),
(102, "Members", "Local"),
(103, "Members"),
(104, "Members"),
(105, "Members"),
(106, "Members"),
(108, "Free-to-play"),
(114, "Members", "Special"),
(115, "Members"),
(116, "Members"),
(117, "Members"),
(118, "Members", "Local"),
(119, "Members"),
(120, "Free-to-play"),
(121, "Members", "Local"),
(122, "Free-to-play", "Local"),
(123, "Members"),
(124, "Members"),
(134, "Members"),
(135, "Free-to-play"),
(136, "Free-to-play"),
(137, "Members"),
(138, "Members"),
(139, "Members"),
(140, "Members"),
(141, "Free-to-play"),
(210, "Free-to-play"),
(215, "Free-to-play"),
(225, "Free-to-play"),
(236, "Free-to-play"),
(239, "Free-to-play"),
(245, "Free-to-play"),
(249, "Free-to-play"),
(250, "Free-to-play"),
(251, "Free-to-play", "Local"),
(252, "Members"),
(255, "Free-to-play"),
(256, "Free-to-play"),
(257, "Members"),
(258, "Members"),
(259, "Members"),
]
all_worlds = [world for world, *_ in world_data]
members_worlds = [world for world, status, *_ in world_data if status == "Members"]
free_to_play_worlds = [world for world, status, *_ in world_data if status == "Free-to-play"]
special_worlds = [world for world, status, *rest in world_data if len(rest) > 0 and rest[0] == "Special"]
local_worlds = [world for world, status, *rest in world_data if len(rest) > 0 and rest[0] == "Local"]
@client.tree.command(name="lock", description="Lock the star call table to prevent modifications.")
@app_commands.default_permissions(manage_events=True)
@check_authorized_server()
async def lock(interaction: discord.Interaction):
current_table_data = load_table_data()
if not current_table_data.get("message_id"):
await interaction.response.send_message("No table exists to lock.", ephemeral=True)
return
if current_table_data["is_locked"]:
await interaction.response.send_message("Table is already locked.", ephemeral=True)
return
table_data["is_locked"] = True
save_table_data(table_data)
await interaction.response.send_message("Star call table has been locked.", ephemeral=True)
@client.tree.command(name="unlock", description="Unlock the star call table to allow modifications.")
@app_commands.default_permissions(manage_events=True)
@check_authorized_server()
async def unlock(interaction: discord.Interaction):
current_table_data = load_table_data()
if not current_table_data.get("message_id"):
await interaction.response.send_message("No table exists to unlock.", ephemeral=True)
return
if not current_table_data["is_locked"]:
await interaction.response.send_message("Table is already unlocked.", ephemeral=True)
return
table_data["is_locked"] = False
save_table_data(table_data)
await interaction.response.send_message("Star call table has been unlocked.", ephemeral=True)
@client.tree.command(name="clear-all", description="Clear all entries in the star call table.")
@app_commands.default_permissions(manage_events=True)
@check_authorized_server()
async def clear(interaction: discord.Interaction):
if not table_data.get("chunk_message_ids"):
await interaction.response.send_message("No table exists to clear.", ephemeral=True)
return
if table_data["is_locked"] and not interaction.user.guild_permissions.manage_events:
await interaction.response.send_message("Table is locked. Cannot clear entries.", ephemeral=True)
return
await interaction.response.defer()
progress_message = await interaction.followup.send(
"Starting table clear..."
)
table_data["entries"] = [
{"world": world, "region": "", "size": "", "game_time": "", "game_time_full": ""}
for world in all_worlds
]
table_rows = []
for entry in table_data["entries"]:
world_name = entry["world"]
if entry["world"] in special_worlds:
world_name = f"\u001b[36m{world_name}\u001b[0m"
elif entry["world"] in local_worlds:
world_name = f"\u001b[30m{world_name}\u001b[0m"
elif entry["world"] in free_to_play_worlds:
world_name = f"\u001b[33m{world_name}\u001b[0m"
else:
world_name = f"\u001b[37m{world_name}\u001b[0m"
table_rows.append(
f"{world_name:<1} {entry['region']:<17} {entry['size']:<4} {entry['game_time']:<4}"
)
chunk_size = 32
chunks = [table_rows[i:i + chunk_size] for i in range(0, len(table_rows), chunk_size)]
channel = interaction.client.get_channel(table_data["channel_id"])
total_chunks = len(chunks)
async def process_chunk(chunk_index: int, chunk: list) -> None:
max_retries = 3
base_delay = 12.0
for attempt in range(max_retries):
try:
message_id = table_data["chunk_message_ids"][chunk_index]
message = await channel.fetch_message(message_id)
updated_chunk = "```ansi\n" + "\n".join(chunk) + "```"
await message.edit(content=updated_chunk)
return
except discord.HTTPException as e:
if e.code == 429:
retry_after = float(e.response.headers.get('Retry-After', 5))
print(f"Rate limited on chunk {chunk_index}, waiting {retry_after} seconds")
await asyncio.sleep(retry_after + 1)
base_delay = max(base_delay, retry_after + 2)
else:
print(f"Error editing chunk {chunk_index} (attempt {attempt + 1}): {e}")
await asyncio.sleep(base_delay)
except Exception as e:
print(f"Unexpected error on chunk {chunk_index} (attempt {attempt + 1}): {e}")
await asyncio.sleep(base_delay)
print(f"Failed to process chunk {chunk_index} after {max_retries} attempts")
last_progress_update = time.time()
for i, chunk in enumerate(chunks):
await process_chunk(i, chunk)
current_time = time.time()
if current_time - last_progress_update >= 5:
progress = f"Clearing table... ({i + 1}/{total_chunks} chunks)"
try:
await progress_message.edit(content=progress)
last_progress_update = current_time
except discord.HTTPException:
pass
await asyncio.sleep(12.0)
save_table_data(table_data)
try:
await progress_message.edit(content="Table cleared successfully!")
except discord.HTTPException:
pass
@client.tree.command(name="clear-old", description="Clear expired entries in the star call table.")
@app_commands.default_permissions(manage_events=True)
@check_authorized_server()
async def clear_old(interaction: discord.Interaction):
if not table_data.get("chunk_message_ids"):
await interaction.response.send_message("No table exists to clear.", ephemeral=True)
return
if table_data["is_locked"] and not interaction.user.guild_permissions.manage_events:
await interaction.response.send_message("Table is locked. Cannot clear entries.", ephemeral=True)
return
await interaction.response.defer()
progress_message = await interaction.followup.send(
"Starting table clear of expired entries..."
)
current_time = datetime.datetime.now(pytz.UTC)
new_entries = []
for entry in table_data["entries"]:
if entry["game_time_full"]:
try:
entry_time = datetime.datetime.fromisoformat(entry["game_time_full"])
if entry_time > current_time:
new_entries.append(entry)
continue
except ValueError:
pass
new_entries.append({
"world": entry["world"],
"region": "",
"size": "",
"game_time": "",
"game_time_full": ""
})
table_data["entries"] = new_entries
table_rows = []
for entry in table_data["entries"]:
world_name = entry["world"]
if entry["world"] in special_worlds:
world_name = f"\u001b[36m{world_name}\u001b[0m"
elif entry["world"] in local_worlds:
world_name = f"\u001b[30m{world_name}\u001b[0m"
elif entry["world"] in free_to_play_worlds:
world_name = f"\u001b[33m{world_name}\u001b[0m"
else:
world_name = f"\u001b[37m{world_name}\u001b[0m"
table_rows.append(
f"{world_name:<1} {entry['region']:<17} {entry['size']:<4} {entry['game_time']:<4}"
)
chunk_size = 32
chunks = [table_rows[i:i + chunk_size] for i in range(0, len(table_rows), chunk_size)]
channel = interaction.client.get_channel(table_data["channel_id"])
total_chunks = len(chunks)
async def process_chunk(chunk_index: int, chunk: list) -> None:
max_retries = 3
base_delay = 12.0
for attempt in range(max_retries):
try:
message_id = table_data["chunk_message_ids"][chunk_index]
message = await channel.fetch_message(message_id)
updated_chunk = "```ansi\n" + "\n".join(chunk) + "```"
await message.edit(content=updated_chunk)
return
except discord.HTTPException as e:
if e.code == 429:
retry_after = float(e.response.headers.get('Retry-After', 5))
print(f"Rate limited on chunk {chunk_index}, waiting {retry_after} seconds")
await asyncio.sleep(retry_after + 1)
base_delay = max(base_delay, retry_after + 2)
else:
print(f"Error editing chunk {chunk_index} (attempt {attempt + 1}): {e}")
await asyncio.sleep(base_delay)
except Exception as e:
print(f"Unexpected error on chunk {chunk_index} (attempt {attempt + 1}): {e}")
await asyncio.sleep(base_delay)
print(f"Failed to process chunk {chunk_index} after {max_retries} attempts")
last_progress_update = time.time()
for i, chunk in enumerate(chunks):
await process_chunk(i, chunk)
current_time = time.time()
if current_time - last_progress_update >= 5:
progress = f"Clearing expired entries... ({i + 1}/{total_chunks} chunks)"
try:
await progress_message.edit(content=progress)
last_progress_update = current_time
except discord.HTTPException:
pass
await asyncio.sleep(12.0)
save_table_data(table_data)
try:
await progress_message.edit(content="Expired entries cleared successfully!")
except discord.HTTPException:
pass
@client.tree.command(name="clear-restricted", description="Clear entries that expired over 30 minutes ago.")
@check_authorized_server()
async def clear_restricted(interaction: discord.Interaction):
is_real_interaction = hasattr(interaction.response, 'is_done')
if is_real_interaction and not interaction.response.is_done():
await interaction.response.defer(ephemeral=True)
async with interaction.client.clear_lock:
if not hasattr(clear_restricted, "last_run"):
clear_restricted.last_run = None
current_time = datetime.datetime.now(pytz.UTC)
if clear_restricted.last_run is not None:
time_diff = (current_time - clear_restricted.last_run).total_seconds() / 60
if time_diff < 10:
wait_time = round(10 - time_diff)
if is_real_interaction:
await interaction.followup.send(
f"In order to combat abuse, this command can only be used once every 10 minutes. Please wait `{wait_time}` minutes before invoking it again."
)
return
if not table_data.get("chunk_message_ids"):
if is_real_interaction:
await interaction.followup.send("No table exists to clear.")
return
if table_data["is_locked"]:
if is_real_interaction:
await interaction.followup.send("Table is locked. Cannot clear entries.")
return
if is_real_interaction:
await interaction.followup.send("Clear-restricted has been invoked successfully.")
progress_message = await interaction.channel.send(
"Starting table clear of entries that expired 30 minutes ago..."
)
else:
progress_message = None
clear_restricted.last_run = current_time
new_entries = []
for entry in table_data["entries"]:
if entry["game_time_full"]:
try:
entry_time = datetime.datetime.fromisoformat(entry["game_time_full"])
if entry_time + datetime.timedelta(minutes=30) > current_time:
new_entries.append(entry)
continue
except ValueError:
pass
new_entries.append({
"world": entry["world"],
"region": "",
"size": "",
"game_time": "",
"game_time_full": ""
})
table_data["entries"] = new_entries
table_rows = []
for entry in table_data["entries"]:
world_name = entry["world"]
if entry["world"] in special_worlds:
world_name = f"\u001b[36m{world_name}\u001b[0m"
elif entry["world"] in local_worlds:
world_name = f"\u001b[30m{world_name}\u001b[0m"
elif entry["world"] in free_to_play_worlds:
world_name = f"\u001b[33m{world_name}\u001b[0m"
else:
world_name = f"\u001b[37m{world_name}\u001b[0m"
table_rows.append(
f"{world_name:<1} {entry['region']:<17} {entry['size']:<4} {entry['game_time']:<4}"
)
chunk_size = 32
chunks = [table_rows[i:i + chunk_size] for i in range(0, len(table_rows), chunk_size)]
channel = interaction.client.get_channel(table_data["channel_id"])
total_chunks = len(chunks)
async def process_chunk(chunk_index: int, chunk: list) -> None:
max_retries = 3
base_delay = 24.0
for attempt in range(max_retries):
try:
message_id = table_data["chunk_message_ids"][chunk_index]
message = await channel.fetch_message(message_id)
updated_chunk = "```ansi\n" + "\n".join(chunk) + "```"
await message.edit(content=updated_chunk)
return
except discord.RateLimited as e:
retry_after = e.retry_after
print(f"Rate limited on chunk {chunk_index}, waiting {retry_after} seconds.")
await asyncio.sleep(retry_after + 1)
continue
except discord.HTTPException as e:
print(f"HTTP Error on chunk {chunk_index} (attempt {attempt + 1}): {e}")
if e.code == 429:
retry_after = float(e.response.headers.get('Retry-After', base_delay))
print(f"Rate limited (HTTP 429) on chunk {chunk_index}, waiting {retry_after} seconds.")
await asyncio.sleep(retry_after + 1)
base_delay = max(base_delay, retry_after + 2)
else:
await asyncio.sleep(base_delay)
except Exception as e:
print(f"Unexpected error on chunk {chunk_index} (attempt {attempt + 1}): {str(e)}")
await asyncio.sleep(base_delay)
print(f"Failed to process chunk {chunk_index} after {max_retries} attempts")
last_progress_update = time.time()
chunks_processed = 0
total_chunks = len(chunks)
chunk_delay = 24.0
for i, chunk in enumerate(chunks):
if i > 0:
await asyncio.sleep(chunk_delay)
await process_chunk(i, chunk)
chunks_processed += 1
if is_real_interaction:
current_time = time.time()
if current_time - last_progress_update >= 5:
progress = f"Clearing expired entries... ({chunks_processed}/{total_chunks} chunks)"
try:
await progress_message.edit(content=progress)
last_progress_update = current_time
except discord.HTTPException as e:
print(f"Failed to update progress message: {e}")
save_table_data(table_data)
if is_real_interaction:
try:
await progress_message.edit(content=f"Entries that expired over 30 minutes ago have been cleared!")
except discord.HTTPException as e:
print(f"Failed to send completion message: {e}")
@client.tree.command(name="prune", description="Clear data for a specific world.")
@app_commands.describe(world="What world do you plan to prune entries for?")
@app_commands.default_permissions(manage_events=True)
@check_authorized_server()
async def prune(interaction: discord.Interaction, world: int):
if not table_data.get("chunk_message_ids"):
await interaction.response.send_message("No table exists to prune.", ephemeral=True)
return
if table_data["is_locked"] and not interaction.user.guild_permissions.manage_events:
await interaction.response.send_message("Table is locked. Cannot prune entries.", ephemeral=True)
return
world_index = None
for i, entry in enumerate(table_data["entries"]):
if entry["world"] == world:
world_index = i
break
if world_index is None:
await interaction.response.send_message(f"World {world} not found.", ephemeral=True)
return
table_data["entries"][world_index] = {
"world": world, "region": "", "size": "", "game_time": "", "game_time_full": ""
}
table_rows = []
for entry in table_data["entries"]:
world_name = entry["world"]
if entry["world"] in special_worlds:
world_name = f"\u001b[36m{world_name}\u001b[0m"
elif entry["world"] in local_worlds:
world_name = f"\u001b[30m{world_name}\u001b[0m"
elif entry["world"] in free_to_play_worlds:
world_name = f"\u001b[33m{world_name}\u001b[0m"
else:
world_name = f"\u001b[37m{world_name}\u001b[0m"
table_rows.append(
f"{world_name:<1} {entry['region']:<17} {entry['size']:<4} {entry['game_time']:<4}"
)
chunk_size = 32
chunks = [table_rows[i:i + chunk_size] for i in range(0, len(table_rows), chunk_size)]
chunk_index = world_index // chunk_size
channel = interaction.client.get_channel(table_data["channel_id"])
message_id = table_data["chunk_message_ids"][chunk_index]
message = await channel.fetch_message(message_id)
updated_chunk = "```ansi\n" + "\n".join(chunks[chunk_index]) + "```"
await message.edit(content=updated_chunk)
save_table_data(table_data)
await interaction.response.send_message(f"Pruned data for world {world}.")
@client.tree.command(name="create", description="Create a star call table.")
@app_commands.default_permissions(administrator=True)
@check_authorized_server()
async def create(interaction: discord.Interaction):
if table_data["message_id"]:
try:
channel = interaction.client.get_channel(table_data["channel_id"])
if channel:
try:
existing_message = await channel.fetch_message(table_data["message_id"])
await channel.fetch_message(table_data["message_id"])
await interaction.response.send_message(
f"A table already exists. Click [`here`]({existing_message.jump_url}) to view it or use `/clear` to reset its data.\nIf you wish to generate a new table, delete any part of the original.", ephemeral=True
)
return
except discord.NotFound:
pass
except Exception:
pass
await interaction.response.defer(ephemeral=True)
table_rows = []
for world in all_worlds:
world_name = world
if world in special_worlds:
world_name = f"\u001b[36m{world_name}\u001b[0m"
elif world in local_worlds:
world_name = f"\u001b[30m{world_name}\u001b[0m"
elif world in free_to_play_worlds:
world_name = f"\u001b[33m{world_name}\u001b[0m"
else:
world_name = f"\u001b[37m{world_name}\u001b[0m"
table_rows.append(
f"{world_name:<1} {'':<17} {'':<4} {'':<4}"
)
chunk_size = 32
chunks = [table_rows[i:i + chunk_size] for i in range(0, len(table_rows), chunk_size)]
table_data["chunk_message_ids"] = []
for i, chunk in enumerate(chunks):
formatted_table = "\n".join([f"{row}" for row in chunk])
try:
table_message = await interaction.channel.send(
content=f"```ansi\n{formatted_table}\n```"
)
table_data["chunk_message_ids"].append(table_message.id)
if i == 0:
table_data["message_id"] = table_message.id
table_data["channel_id"] = interaction.channel.id
if not table_data["entries"]:
table_data["entries"] = [
{"world": world, "region": "", "size": "", "game_time": "", "game_time_full": ""}
for world in all_worlds
]
except Exception as e:
await interaction.followup.send(
f"Failed to create the table. Error: {str(e)}",
ephemeral=True
)
return
save_table_data(table_data)
await interaction.followup.send("Table(s) created successfully!", ephemeral=True)
@client.tree.command(name="call", description="Call-out shooting stars.")
@app_commands.describe(
world = "What world will this star fall on?",
region = "What's the general location of the falling star?",
size = "What size will the falling star be?",
game_time = "How many minutes from now will the star fall? (Use the lower bound)",
)
@app_commands.rename(game_time="relative-time")
@app_commands.choices(
region=[
app_commands.Choice(name="Anachronia", value="Anachronia"),
app_commands.Choice(name="Asgarnia", value="Asgarnia"),
app_commands.Choice(name="Ashdale", value="Ashdale"),
app_commands.Choice(name="Crandor/Karamja", value="Crandor/Karamja"),
app_commands.Choice(name="Daemonheim", value="Daemonheim"),
app_commands.Choice(name="Feldip Hills", value="Feldip Hills"),
app_commands.Choice(name="Fremennik/Lunar Isle", value="Frem/Lunar"),
app_commands.Choice(name="Kandarin", value="Kandarin"),
app_commands.Choice(name="Kharidian Desert", value="Kharidian Desert"),
app_commands.Choice(name="Lost Grove", value="Lost Grove"),
app_commands.Choice(name="Menaphos", value="Menaphos"),
app_commands.Choice(name="Misthalin", value="Misthalin"),
app_commands.Choice(name="Morytania/Mos Le'Harmless", value="Morytania/Mos"),
app_commands.Choice(name="Piscatoris/Gnome/Tirannwn", value="Pisc/Gnome/Tir"),
app_commands.Choice(name="Tuska", value="Tuska"),
app_commands.Choice(name="Wilderness", value="Wilderness"),
],
size=[
app_commands.Choice(name="Small", value="sm"),
app_commands.Choice(name="Average", value="avg"),
app_commands.Choice(name="Big", value="big"),
app_commands.Choice(name="1", value="s1"),
app_commands.Choice(name="2", value="s2"),
app_commands.Choice(name="3", value="s3"),
app_commands.Choice(name="4", value="s4"),
app_commands.Choice(name="5", value="s5"),
app_commands.Choice(name="6", value="s6"),
app_commands.Choice(name="7", value="s7"),
app_commands.Choice(name="8", value="s8"),
app_commands.Choice(name="9", value="s9"),
app_commands.Choice(name="10", value="s10"),
]
)
@check_authorized_server()
async def call(interaction: discord.Interaction, world: int, region: str, size: str, game_time: app_commands.Range[int, 1, 128]):
try:
await interaction.response.defer()
except (discord.NotFound, discord.HTTPException):
pass
async with interaction.client.clear_lock:
if not hasattr(interaction.client, 'message_queue'):
interaction.client.message_queue = MessageQueue()
interaction.client.message_queue.start()
if not table_data.get("message_id"):
await interaction.followup.send("No table exists. Use `/create` first.", ephemeral=True)
return
if table_data["is_locked"] and not interaction.user.guild_permissions.manage_events:
await interaction.followup.send("Table is locked. Invoke `/unlock` to modify entries.", ephemeral=True)
return
world_index = None
for i, entry in enumerate(table_data["entries"]):
if entry["world"] == world:
world_index = i
break
if world_index is None:
await interaction.followup.send(f"World `{world}` not found.", ephemeral=True)
return
try:
entry_updates = {}
if region is not None:
entry_updates["region"] = region
if size is not None:
entry_updates["size"] = size
if game_time is not None:
current_utc = datetime.datetime.now(pytz.UTC)
game_end_time = current_utc + datetime.timedelta(minutes=game_time)
entry_updates["game_time"] = game_end_time.strftime("%H:%M")
entry_updates["game_time_full"] = game_end_time.isoformat()
game_time_unix = int(game_end_time.timestamp())
table_data["entries"][world_index].update(entry_updates)
channel = interaction.client.get_channel(table_data["channel_id"])
if not channel:
await interaction.followup.send("Error: Could not find the channel.", ephemeral=True)
return
table_rows = []
for entry in table_data["entries"]:
world_name = entry["world"]
if entry["world"] in special_worlds:
world_name = f"\u001b[36m{world_name}\u001b[0m"
elif entry["world"] in local_worlds:
world_name = f"\u001b[30m{world_name}\u001b[0m"
elif entry["world"] in free_to_play_worlds:
world_name = f"\u001b[33m{world_name}\u001b[0m"
else:
world_name = f"\u001b[37m{world_name}\u001b[0m"
table_rows.append(
f"{world_name:<1} {entry['region']:<17} {entry['size']:<4} {entry['game_time']:<4}"
)
chunk_size = 32
chunks = [table_rows[i:i + chunk_size] for i in range(0, len(table_rows), chunk_size)]
chunk_index = world_index // chunk_size
message_id = table_data["chunk_message_ids"][chunk_index]
message = await channel.fetch_message(message_id)
updated_chunk = "```ansi\n" + "\n".join(chunks[chunk_index]) + "```"