-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
executable file
·889 lines (789 loc) · 42.1 KB
/
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
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
import json
import discord
import traceback
import logging
import os
from dotenv import load_dotenv
from datetime import datetime
from pytz import timezone
from contest import Contest
from team import Team
from question import Question
from contestperiod import ContestPeriod
from exceptions import (
AnswersAlreadySubmittedException,
MemberInAnotherTeamException,
MemberNotInTeamException,
MemberNotInvitedException,
OwnerLeaveTeamException,
WrongPeriodException,
TeamSizeExceededException,
TeamNameException,
)
load_dotenv("secrets.env")
intents = discord.Intents.default()
intents.members = True
client: discord.Client = discord.Client(intents=intents)
tree: discord.app_commands.CommandTree = discord.app_commands.CommandTree(client)
# constants
GUILD = discord.Object(id=624314920158232616)
DANIEL_USER_ID = 614549755342880778
assert GUILD is not None
async def contest_name_autocompletion(interaction, current: str) -> list:
with open('data/generalContestInfo.json', 'r') as file:
contest_names = json.load(file)['allContestNames']
data = []
for contest_name in contest_names:
data.append(discord.app_commands.Choice(name=contest_name.lower(), value=contest_name))
return data
async def user_team_name_autocompletion(interaction, current: str):
contest_name = interaction.namespace.contest_name
contest = Contest.from_json(contest_name)
team_name_choices = []
for team in contest.teams:
if interaction.user.id in team.invited_member_ids or interaction.user.id == team.owner_id:
team_name_choices.append(discord.app_commands.Choice(name=team.name.lower(), value=team.name))
return team_name_choices
async def all_team_names_autocompletion(interaction, current: str):
contest = Contest.from_json(interaction.namespace.contest_name)
team_name_choices: list[discord.app_commands.Choice] = []
for team in contest.teams:
team_name_choices.append(discord.app_commands.Choice(name=team.name.lower(), value=team.name))
return team_name_choices
def member_id_to_name(interaction, member_id: int) -> str:
member: discord.Member = interaction.guild.get_member(member_id)
if member is None:
return "(Member not found)"
else:
return member.display_name
@client.event
async def on_ready():
print("Ready! - " + datetime.now(timezone('US/Eastern')).strftime("%m/%d/%Y %H:%M:%S"))
@tree.error
async def on_app_command_error(interaction, error):
await interaction.response.send_message(
"Sorry, there was a problem with the bot, so an uncaught error has occurred. "
"Please consult @DanielRocksUrMom for help.")
daniel = client.get_user(DANIEL_USER_ID)
if daniel:
channel = await daniel.create_dm()
stack_trace = traceback.format_exc()
date_est = datetime.now(timezone('US/Eastern'))
error_text = f"Date: {date_est} \n {stack_trace}"
with open('latest_error.log', 'w+') as file:
file.write(error_text)
await channel.send(f"Hey Daniel, the user '{interaction.user.display_name}' just caused an error in your code.")
await channel.send("This is the error file: ", file=discord.File("latest_error.log"))
logging.warning(error_text)
else:
print("Failed to write to disk.")
logging.warning(error)
@tree.command(name="help",
description="The starting point for the bot.",
guild=GUILD)
async def help_command(interaction):
await interaction.response.send_message(
"""
The DSMC contest bot is a bot that manages DSMC competitions.
See the bot page for descriptions on each of it's commands.
Basic Commands for Test-Takers:
/register_team
/join_team - needs an invitation
/answer_question
/submit_all_answers - Cannot be undone!
/leave_current_team - Owners must /transfer_ownership first
/show_answers - Shows your team's answers(not the correct ones)
/invite_member
/transfer_ownership - if you are the team's owner
/change_team_name
Commands that show the contest status:
/all_contest_competitors
/show_questions
/show_teams
/contest_period
/team_rankings
/link - Gets PDF/overleaf link
"""
)
@tree.command(name="create_contest",
description="[Mod Only] Creates a new Contest.",
guild=GUILD)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def create_contest(interaction, name: str, pdf_link: str, team_size_limit: int | None = None):
with open("data/generalContestInfo.json", "r") as file:
data_dict = json.load(file)
if name in data_dict['allContestNames']:
await interaction.response.send_message("A contest with name " + name + " already exists.", ephemeral=True)
return
Contest(name.lower(), pdf_link, team_size_limit).update_json()
await interaction.response.send_message("Contest successfully created!")
@tree.command(name="all_contest_competitors",
description="Displays all contest competitors.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def all_contest_competitors(interaction, contest_name: str):
contest = Contest.from_json(contest_name)
all_participants = [member_id_to_name(interaction, member_id) for member_id in contest.registered_member_ids]
all_invited_participants = [member_id_to_name(interaction, member_id) for member_id in contest.invited_member_ids]
await interaction.response.send_message("These people are currently in a team: \n" + str(
all_participants) + "\n These people are currently invited to a team: " + str(all_invited_participants))
@tree.command(name="register_team",
description="Registers a team into a contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def register_team(interaction, contest_name: str, team_name: str, member_two: discord.Member | None = None,
member_three: discord.Member | None = None, member_four: discord.Member | None = None):
contest = Contest.from_json(contest_name)
potential_current_team: Team | None = contest.get_team_of_user(interaction.user.id)
if potential_current_team:
await interaction.response.send_message(f"You seem to already be in team {potential_current_team.name}.")
return
invite_list: list[discord.Member] = []
if member_two:
invite_list.append(member_two)
if member_three:
invite_list.append(member_three)
if member_four:
invite_list.append(member_four)
try:
new_team = Team(
contest_instance=contest,
name=team_name,
owner_id=interaction.user.id
)
contest.add_team(new_team)
await interaction.response.send_message(
f"Team '{team_name}' has been added to the contest with {[member.display_name for member in invite_list]}"
f" invited. In order for users to join your team, they must use /join_team.")
for member in invite_list:
new_team.invite_member(member.id)
channel = await member.create_dm()
await channel.send(
f"The user {interaction.user} has invited you to join the team '{team_name}'"
f"for the contest '{contest_name}'. In order to join, "
f"use /join_team in the Mathematics Server (not in the DMs)."
f"If you don't want to join, or if you're already in another team, ignore this message.")
contest.update_json()
except WrongPeriodException:
await interaction.response.send_message(
"You can only create a team when this contest is in it's signup phase. Sorry!")
except TeamNameException:
await interaction.response.send_message("There is already a team with the name " + team_name)
@tree.command(name="create_team",
description="[Mod Only] Creates a team with another owner.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def create_team(interaction, contest_name: str, team_name: str,
owner: discord.Member, member_two: discord.Member | None = None,
member_three: discord.Member | None = None, member_four: discord.Member | None = None):
contest = Contest.from_json(contest_name)
potential_current_team: Team | None = contest.get_team_of_user(owner.id)
if potential_current_team:
await interaction.response.send_message(f"The owner is already in team {potential_current_team.name}.")
return
if team_name in map(lambda t: t.name, contest.teams):
await interaction.response.send_message(f"The team with name {team_name} already exists.")
return
try:
new_team = Team(
contest_instance=contest,
name=team_name,
owner_id=owner.id
)
contest.add_team(new_team)
for member in [member_two, member_three, member_four]:
if member is None:
continue
new_team.register_member(member.id, ignore_invite=True)
await interaction.response.send_message(f"Team {team_name} has been created by admin.", ephemeral=True)
contest.update_json()
except WrongPeriodException:
await interaction.response.send_message(
"You can only create a team when this contest is in it's signup phase. Sorry!")
except TeamNameException:
await interaction.response.send_message("There is already a team with the name " + team_name)
@tree.command(name="invite_members",
description="Invites more members to your team.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def invite_more_members(interaction, contest_name: str, member_one: discord.Member,
member_two: discord.Member | None = None, member_three: discord.Member | None = None):
contest = Contest.from_json(contest_name)
success_messages: list[str] = []
user_team = contest.get_team_of_user(interaction.user.id)
if user_team is None:
await interaction.response.send_message("Hmmm... It looks like you are not in a team yet.")
return
if member_one:
user_team.invite_member(member_one.id)
success_messages.append(f"{member_one.display_name} has been successfully invited.")
if member_two:
user_team.invite_member(member_two.id)
success_messages.append(f"{member_two.display_name} has been successfully invited.")
if member_three:
user_team.invite_member(member_three.id)
success_messages.append(f"{member_three.display_name} has been successfully invited.")
contest.update_json()
await interaction.response.send_message("\n".join(success_messages))
@tree.command(name="join_team",
description="Join one of the teams you were invited to.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion, team_name=user_team_name_autocompletion)
async def join_team(interaction, contest_name: str, team_name: str):
contest = Contest.from_json(contest_name)
new_team = contest.get_team(team_name)
if interaction.user.id in new_team.member_ids + [new_team.owner_id]:
await interaction.response.send_message(
"Looks like you are already in this team. To leave, use /leave_current_team.",
ephemeral=True)
return
try:
new_team.register_member(interaction.user.id)
await interaction.response.send_message(
f"Hooray! You have officially joined team {team_name}! to leave, use /leave_current_team.")
contest.update_json()
except MemberNotInvitedException:
await interaction.response.send_message(
"Hmmm.... It seems that you haven't been invited to this team.",
ephemeral=True)
except MemberInAnotherTeamException:
await interaction.response.send_message(
"You've already joined another team! Use /leave_current_team to leave your current team, "
"then use /join_team to join this one.",
ephemeral=True)
except TeamSizeExceededException:
await interaction.response.send_message(f"The team size limit of {contest.team_size_limit} has been exceeded.")
@tree.command(name="change_team_name",
description="Change the name of the team you are in.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def change_team_name(interaction, contest_name: str, new_team_name: str):
contest = Contest.from_json(contest_name)
team: Team | None = contest.get_team_of_user(interaction.user.id)
if team:
previous_name = team.name
team.name = new_team_name
contest.update_json()
await interaction.response.send_message(
"The team that was previously referred to as '" +
previous_name + "' now has the name '" + team.name + "'."
)
else:
await interaction.response.send_message("It seems that you are not in a team currently.", ephemeral=True)
@tree.command(name="change_team_size_limit",
description="[Mod Only] Modifies the team size limit for a contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def modify_team_size_limit(interaction, contest_name: str, size_limit: int):
contest = Contest.from_json(contest_name)
contest.team_size_limit = size_limit
contest.update_json()
await interaction.response.send_message("Team size limit has been updated to " + str(size_limit) + ".")
@tree.command(name="leave_current_team",
description="Leaves your current team in the respective contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def leave_current_team(interaction, contest_name: str):
contest = Contest.from_json(contest_name)
user_team = contest.get_team_of_user(interaction.user.id)
if user_team is None:
await interaction.response.send_message("Hmmm... You don't seem to be in a team as of now.", ephemeral=True)
else:
try:
user_team.remove_member(interaction.user.id)
contest.update_json()
await interaction.response.send_message("You have officially left your current team.")
except OwnerLeaveTeamException:
await interaction.response.send_message(
"As the owner of this team, you cannot leave. You must either delete the team or transfer ownership "
"to another person(via the /transfer_ownership command)")
@tree.command(name="transfer_ownership",
description="If you are the owner of a team, transfers ownership to another person within your team.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def transfer_ownership(interaction, contest_name: str, new_owner: discord.Member):
try:
contest = Contest.from_json(contest_name)
player_team = contest.get_team_of_user(interaction.user.id)
if player_team is None:
await interaction.response.send_message("it looks like you are not in a team currently.")
elif interaction.user.id == player_team.owner_id:
player_team.transfer_ownership(new_owner.id)
contest.update_json()
await interaction.response.send_message(f"Ownership has been successfully transferred to {new_owner}!")
else:
await interaction.response.send_message(
"Sorry, you're not the owner of the team you're in, so you cannot transfer ownership.", ephemeral=True)
except MemberNotInTeamException:
await interaction.response.send_message(
"The member that you tried to transfer ownership in is not in the team(or hasn't accepted the invite yet).")
@tree.command(name="unregister_team",
description="Unregisters your team.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def unregister_team(interaction, contest_name: str):
contest = Contest.from_json(contest_name)
user_team = contest.get_team_of_user(interaction.user.id)
if user_team is None:
await interaction.response.send_message("Hmm.... You don't seem to be in a team as of now.", ephemeral=True)
elif user_team.owner_id != interaction.user.id:
await interaction.response.send_message(
"Holdup! You can't delete this team, as it was created by someone else. "
"Ask the creator to delete the team. If you want to leave, use /leave_current_team.",
ephemeral=True)
else:
contest.remove_team(user_team)
for member_id in user_team.member_ids:
channel = await interaction.guild.get_member(member_id).create_dm()
await channel.send(
f"The original owner of team {user_team.name} has deleted this team. "
f"To sign up for another team, ask another team owner to invite you, then use /join.")
await interaction.response.send_message("Success!")
contest.update_json()
@tree.command(name="add_question",
description="[Mod Only] Adds a question into a contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def add_question(interaction, contest_name: str, answer: float, points: int, problem_number: int | None = None):
contest = Contest.from_json(contest_name)
try:
if problem_number is None:
contest.add_question(Question(contest, answer, points))
else:
contest.add_question(Question(contest, answer, points), problem_number)
contest.update_json()
await interaction.response.send_message("Success!")
except WrongPeriodException:
await interaction.response.send_message(
"currently, the contest is underway. You cannot add questions at this time.", ephemeral=True)
except IndexError:
await interaction.response.send_message("Hmm.... your question index is out of bounds", ephemeral=True)
@tree.command(name="remove_question", description="[Mod Only] Removes a question from a specified contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def remove_question(interaction, contest_name: str, question_number: int):
contest = Contest.from_json(contest_name)
try:
contest.remove_question(question_number)
contest.update_json()
await interaction.response.send_message("Question with number " + str(question_number) + " has been removed.")
except WrongPeriodException:
await interaction.response.send_message("The competition is underway, so you cannot add or remove questions.")
@tree.command(name="change_question",
description="[Mod Only] Changes a question's properties.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def change_answer(interaction, contest_name: str, question_num: int,
new_answer: float | None = None, new_point_value: float | None = None):
contest = Contest.from_json(contest_name)
try:
question = contest.get_question(question_num)
if new_answer:
question.correct_answer = new_answer
if new_point_value:
question.point_value = new_point_value
contest.update_json()
except KeyError:
await interaction.response.send_message(f"The contest only has a total of {len(contest.questions)} questions.")
@tree.command(name="change_contest_period",
description="[Mod Only] Changes the period of a contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
@discord.app_commands.choices(period_name=[
discord.app_commands.Choice(name='pre-signup', value='pre-signup'),
discord.app_commands.Choice(name='signup', value='signup'),
discord.app_commands.Choice(name='competition', value='competition'),
discord.app_commands.Choice(name='post-competition', value='post-competition')
])
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def change_contest_period(interaction, contest_name: str, period_name: str):
match period_name.lower():
case 'pre-signup':
period = ContestPeriod.preSignup
case 'signup':
period = ContestPeriod.signup
case 'competition':
period = ContestPeriod.competition
case 'post-competition':
period = ContestPeriod.postCompetition
case _:
await interaction.response.send_message("An invalid period name has been entered.", ephemeral=True)
return
contest = Contest.from_json(contest_name)
contest.period = period
contest.update_json()
await interaction.response.send_message(f"Success! The contest period has been changed to {period_name}.")
@tree.command(name="contest_period",
description="Gives info about the current phase of the contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def contest_period(interaction, contest_name: str):
contest = Contest.from_json(contest_name)
match contest.period:
case ContestPeriod.preSignup:
message = "The current period of the contest is pre-signup. In this phase, you cannot register teams."
case ContestPeriod.signup:
message = ("The current period of the contest is signup. Now, you can create and join teams; but you "
"cannot answer questions yet.")
case ContestPeriod.competition:
message = ("The current period of the contest is competition. Here, you can answer questions ande join "
"existing teams; but no new teams can be created.")
case ContestPeriod.postCompetition:
message = ("The current period of the contest is post-competition. At this point, the contest is already "
"finished; and results are out. No answers can be submitted.")
case _:
message = "Unknown period...Ask Daniel to update the contest_period command."
await interaction.response.send_message(message)
@tree.command(name="start_competition",
description="[Mod Only] Starts the competition and creates private team channels.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def start_competition(interaction, contest_name: str, channel_category: discord.CategoryChannel):
contest = Contest.from_json(contest_name)
# sets the contest period
contest.period = ContestPeriod.competition
# then, creates the appropriate contest channels
manager_role = discord.utils.get(interaction.guild.roles, name="Olympiad Team")
admin_role = discord.utils.get(interaction.guild.roles, name="Olympiad Manager")
contestant_role = discord.utils.get(interaction.guild.roles, name="DSMC Contestant")
for team in contest.teams:
overwrites = {
interaction.guild.default_role: discord.PermissionOverwrite(read_messages=False),
manager_role: discord.PermissionOverwrite(read_messages=True),
admin_role: discord.PermissionOverwrite(read_messages=True),
interaction.guild.get_member(team.owner_id): discord.PermissionOverwrite(read_messages=True)
}
for member_id in team.member_ids:
member = interaction.guild.get_member(member_id)
if member:
overwrites[member] = discord.PermissionOverwrite(read_messages=True)
await member.add_roles(contestant_role)
else:
await interaction.response.send_message("Member with ID {member_id} was not found.", ephemeral=True)
channel = await interaction.guild.create_text_channel(
team.name + '-contest-channel',
overwrites=overwrites,
category=channel_category)
team.channel_id = channel.id
contest.update_json()
await interaction.response.send_message("Channels have been opened!.")
@tree.command(name="assign_roles",
description="[Mod Only] Assigns the DSMC Contestant role if it hasn't already been assigned.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def start_competition(interaction, contest_name: str):
contestant_role = discord.utils.get(interaction.guild.roles, name="DSMC Contestant")
for member_id in Contest.from_json(contest_name).registered_member_ids:
member = interaction.guild.get_member(member_id)
if member:
await member.add_roles(contestant_role)
await interaction.response.send_message("Success!")
# untested.
@tree.command(name="end_competition",
description="[Mod Only; DANGER ZONE] Ends the competition and deletes the contest channels.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def end_competition(interaction, contest_name: str):
contest = Contest.from_json(contest_name)
contest.period = ContestPeriod.postCompetition
contest.update_json()
for team in contest.teams:
if team.channel_id:
await interaction.guild.get_channel(team.channel_id).delete()
await interaction.response.send_message("All contest channels deleted!.")
@tree.command(name="answer_question",
description="Answer a question within a specific contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def answer_question(interaction, contest_name: str, question_number: int, answer: float):
try:
contest = Contest.from_json(contest_name)
user_team = contest.get_team_of_user(interaction.user.id)
if user_team:
user_team.answer(contest.get_question(question_number), answer)
else:
await interaction.response.send_message(
"Hmmm..... your team doesn't seem to be found in the contest. Maybe you haven't signed up yet?",
ephemeral=True)
contest.update_json()
await interaction.response.send_message(
str(interaction.user) + f" has answered question {question_number}!")
except AnswersAlreadySubmittedException:
await interaction.response.send_message(
"Sorry, you have already submitted your answers. Once you submit your answers, you cannot answer anything "
"else.",
ephemeral=True)
except WrongPeriodException:
await interaction.response.send_message(
"Sorry, you can't submit any answers right now, as the contest period is not the competition period.",
ephemeral=True)
except IndexError:
await interaction.response.send_message(
"Sorry, but the contest doesn't have a problem with number " + str(question_number))
@tree.command(name="submit_all_answers",
description="Submits your teams' answers. Once you submit, you CANNOT unsubmit!",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def submit_team_answers(interaction, contest_name: str):
try:
contest = Contest.from_json(contest_name)
player_team = contest.get_team_of_user(interaction.user.id)
if player_team and player_team.owner_id == interaction.user.id:
player_team.submit_answers()
contest.update_json()
await interaction.response.send_message("The owner has officially submitted all of their teams' answers!")
else:
await interaction.response.send_message(
"Sorry, you are not the owner, and thus you cannot submit any answers.")
except WrongPeriodException:
await interaction.response.send_message(
"Sorry, but you cannot submit any answers right now, as the contest is not in the competition period.")
@tree.command(name="show_questions_with_answers",
description="[Mod Only] Gets the answer and point value of all questions within the specified contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def show_questions_with_answers(interaction, contest_name: str):
contest = Contest.from_json(contest_name)
question_string = ""
for question in contest.questions:
question_string += (f"Question {question.number}: answer = {question.correct_answer}, "
f"points = {question.point_value} \n")
if question_string == "":
await interaction.response.send_message(
"Hmm... There doesn't seem to be any questions in the contest currently. To add one, use /add_question.")
else:
await interaction.response.send_message(question_string)
@tree.command(name="link",
description="Gets the link of a contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def link(interaction, contest_name: str):
contest = Contest.from_json(contest_name)
if contest.period == ContestPeriod.competition or contest.period == ContestPeriod.postCompetition:
await interaction.response.send_message(contest.link, ephemeral=True)
else:
await interaction.response.send_message("Sorry, you cannot access this right now.", ephemeral=True)
@tree.command(name="change_link",
description="[Mod Only] Changes the link of a contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def change_link(interaction, contest_name: str, new_link: str):
contest = Contest.from_json(contest_name)
contest.link = new_link
contest.update_json()
await interaction.response.send_message("Link has been changed!")
@tree.command(name="team_rankings",
description="Gets the team rankings for the specified contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def team_rankings(interaction, contest_name: str):
try:
contest = Contest.from_json(contest_name)
rankings: list[str] = []
rank = 1
for team in contest.team_rankings:
rankings.append(f"Rank {rank}: {team.name}, total points = {team.total_points}.")
rank += 1
await interaction.response.send_message("\n".join(rankings))
except WrongPeriodException:
await interaction.response.send_message(
"The competition hasn't started yet, and thus there aren't any rankings. "
"Use /all_teams instead to get a list of every team.")
@tree.command(name="show_teams",
description="Shows all teams, and their members, within a contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def show_teams(interaction, contest_name: str):
contest = Contest.from_json(contest_name)
team_blurbs: list[str] = []
for team in contest.teams:
team_blurbs.append(
f"Team '{team.name}', with owner '{member_id_to_name(interaction, team.owner_id)}' and members"
f" {[member_id_to_name(interaction, member_id) for member_id in team.member_ids]},")
await interaction.response.send_message("All teams: \n" + "\n".join(team_blurbs))
@tree.command(name="show_questions",
description="Shows the questions of a contest with their respective point value. Does not show answers.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def show_questions(interaction, contest_name: str):
contest = Contest.from_json(contest_name)
questions_strings: list[str] = []
for question in contest.questions:
questions_strings.append(f"Question {question.number}: points = {question.point_value}")
if len(questions_strings) == 0:
await interaction.response.send_message(
"There are no questions at the moment. The contest might still be in it's signup phase.", ephemeral=True)
else:
await interaction.response.send_message("All questions: \n" + "\n".join(questions_strings))
@tree.command(name="delete_contest",
description="[Mod Only; DANGER ZONE] Deletes a contest.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def delete_contest(interaction, contest_name: str):
Contest.delete_json(contest_name)
await interaction.response.send_message("Contest has been deleted!")
@tree.command(name="remove_member_from_team",
description="[Mod Only] Removes a member from a team.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion, team_name=all_team_names_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def remove_member_from_team(interaction, contest_name: str, team_name: str, member: discord.Member):
contest: Contest = Contest.from_json(contest_name)
team = contest.get_team(team_name)
try:
team.remove_member(member.id)
except OwnerLeaveTeamException:
await interaction.user.send_message(
"You cannot remove an owner. Use /transfer_ownership to transfer ownership to someone else.",
ephemeral=True)
except MemberNotInTeamException:
await interaction.user.send_message("This member is not currently in the team.", ephemeral=True)
finally:
contest.update_json()
@tree.command(name="unsubmit_answers_of_team",
description="[Mod Only] Unsubmits a team's answers.",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion, team_name=all_team_names_autocompletion)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def unsubmit_answers_of_team(interaction, contest_name: str, team_name: str):
contest: Contest = Contest.from_json(contest_name)
team: Team = contest.get_team(team_name)
team.answers_submitted = False
team.submit_ranking = 0
contest.update_json()
await interaction.response.send_message("Success!")
@tree.command(name="transfer_ownership_of_team",
description="[Mod Only] Forces team ownership transfer.",
guild=GUILD)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion, team_name=all_team_names_autocompletion)
async def transfer_ownership_of_team(interaction, contest_name: str, team_name: str, new_owner: discord.Member):
try:
contest = Contest.from_json(contest_name)
player_team = contest.get_team(team_name)
if player_team is None:
await interaction.response.send_message("Hmmm... this team cannot be found", ephemeral=True)
else:
player_team.transfer_ownership(new_owner.id)
contest.update_json()
await interaction.response.send_message(f"Ownership has been successfully transferred to {new_owner}!")
except MemberNotInTeamException:
await interaction.response.send_message(
"The member that you tried to transfer ownership in is not in the team(or hasn't accepted the invite yet).",
ephemeral=True)
@tree.command(name="add_member_to_team",
description="[Mod Only] Forcefully adds a member to a team.",
guild=GUILD)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion, team_name=all_team_names_autocompletion)
async def add_member_to_team(interaction, contest_name: str, team_name: str, new_member: discord.Member):
contest = Contest.from_json(contest_name)
team = contest.get_team(team_name)
try:
team.register_member(new_member.id, ignore_invite=True)
contest.update_json()
await interaction.response.send_message("Success!")
except MemberInAnotherTeamException:
await interaction.response.send_message("This member is already in another team.", ephemeral=True)
except TeamSizeExceededException:
await interaction.response.send_message(
f"The team size limit of {contest.team_size_limit} has been exceeded.", ephemeral=True)
@tree.command(name="submit_answers_for_team",
description="[Mod Only] Forcefully submits answers for teams who haven't submitted yet.",
guild=GUILD)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def submit_answers_for_team(interaction, contest_name: str):
try:
contest = Contest.from_json(contest_name)
for team in contest.teams:
if not team.answers_submitted:
team.submit_answers()
contest.update_json()
await interaction.response.send_message("Success!")
except WrongPeriodException:
await interaction.response.send_message(
"The period must be ContestPeriod.Competition for this to work.", ephemeral=True)
@tree.command(name="answer_question_for_team",
description="[Mod Only] Forcefully answers a question for a team.",
guild=GUILD)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion, team_name=all_team_names_autocompletion)
async def answer_question_for_team(interaction, contest_name: str, team_name: str, question_number: int, answer: float):
try:
contest = Contest.from_json(contest_name)
team = contest.get_team(team_name)
team.answer(contest.get_question(question_number), answer)
contest.update_json()
await interaction.response.send_message(f"Question {question_number} for team {team_name} was answered.")
except AnswersAlreadySubmittedException:
await interaction.response.send_message("Hmm... this team has already submitted their answers.", ephemeral=True)
except WrongPeriodException:
await interaction.response.send_message(
"Sorry, you can't submit any answers right now, as the contest period is not the competition period.",
ephemeral=True)
except IndexError:
await interaction.response.send_message(
"Sorry, but the contest doesn't have a problem with number " + str(question_number), ephemeral=True)
@tree.command(name="show_answers",
description="Your team's current answers(not necessarily the correct ones!)",
guild=GUILD)
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def answered_questions(interaction, contest_name: str):
contest = Contest.from_json(contest_name)
team = contest.get_team_of_user(interaction.user.id)
if team:
await interaction.response.send_message(team.answering_status())
else:
await interaction.response.send_message("Hmmm... your team could not be found.")
@tree.command(name="show_answers_of_team",
description="[Mod Only] What a certain team has answered; includes grading.",
guild=GUILD)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion, team_name=all_team_names_autocompletion)
async def answered_questions_admin(interaction, contest_name: str, team_name: str):
contest = Contest.from_json(contest_name)
team = contest.get_team(team_name)
if team:
await interaction.response.send_message(team.answering_status(display_correct_answer=True))
else:
await interaction.response.send_message("Hmmm... the team could not be found.")
@tree.command(name="change_contest_name",
description="[Mod Only] Changes the name of an existing contest.",
guild=GUILD)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
@discord.app_commands.autocomplete(contest_name=contest_name_autocompletion)
async def change_contest_name(interaction, contest_name: str, new_name: str):
contest = Contest.from_json(contest_name)
contest.name = new_name
contest.update_json()
Contest.delete_json(contest_name)
await interaction.response.send_message(f"Success! The name has been changed to {contest_name}")
@tree.command(name="sync",
description="[Mod Only] Syncs the current slash commands.",
guild=GUILD)
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def sync_bot_commands(interaction):
await interaction.response.defer(thinking=True)
await tree.sync(guild=GUILD)
await interaction.followup.send("Commands synced.")
@tree.command(name="sync_global",
description="[Mod Only] Syncs slash command on all servers.")
@discord.app_commands.checks.has_any_role('Olympiad Team', 'Olympiad Manager')
async def sync_commands_globally(interaction):
await interaction.response.defer(thinking=True)
await tree.sync()
await interaction.followup.send("Commands synced across all guilds.")
# DO NOT DELETE THESE LINES OF CODE:
# tree.add_command(db_group)
client.run(os.environ['token'])