-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping_manager.py
1022 lines (836 loc) · 36.9 KB
/
ping_manager.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 discord
import asyncio
from discord.ext import commands
import json
from json.decoder import JSONDecodeError
import sys
import traceback
DESCRIPTION = """This bot is used to ping helpers while also preventing spamming of pings in the APStudents discord.
It was created by @jjam912#2180 and @ACT Inc.#2590
https://github.com/APStudentsReddit/ping_manager/
This was written using discord.py rewrite.
"""
# This bot is only meant to be run on one server, so hardcoding this id seems fine
GUILD_ID = 420053499707916288 # Currently set to JJam912's Bot Army
# For the settings file
KEY_PREFIX = "prefix"
KEY_TIMEOUT = "timeout_length"
bot = commands.Bot(description=DESCRIPTION, command_prefix="!")
bot.remove_command("help") # We implement our own
helper_roles = {} # Drawn from aliases.json
blacklisted_users = []
users_on_confirmation = []
pending_pings = {}
users_on_timeout = {}
users_to_remind = []
ping_frequency = {} # Keeps track of stats
AMBIGUOUS = "Ambiguous role"
DISABLED = "Disabled role"
HELPER_SUFFIX = " Helper"
TIMEOUT_TIME = 3600
DISABLED_ROLES = {
"Calculus": ["calculus", "ap calc", "calc ab", "calc bc", "calc", "ab calc",
"bc calc", "calculus bc", "calculus ab"],
"Computer Science": ["ap computer science", "ap computer science helper",
"ap csa", "computer science a", "comp sci a", "csa"],
"Home Economics": ["home econ", "home economics", "ap home econ", "ap home economics"]
}
AMBIGUOUS_ROLES = {
"physics": ["Physics 1", "Physics 2", "Physics C Mech", "Physics C E/M"],
# "spanish": ["Spanish Language", "Spanish Literature"],
# "comp sci": ["Computer Science", "Computer Science Principles"]
"economics": ["Macroeconomics", "Microeconomics"],
"econ": ["Macroeconomics", "Microeconomics"],
"art": ["Art History", "Studio Art"],
"gov": ["U.S Government", "Comp. Government"],
"government": ["U.S Government", "Comp. Government"],
}
# See alias command for alias message
HELP_MESSAGE = """
{0}
**NOTE:** At the request of bork, Computer Science A, Home Economics, and Calculus Helpers will not receive any pings.
```
_________________________________________________________________________
| Command | Description | Access Level |
|----------------------+----------------------------+-------------------|
| ping <alias> | Pings a helper | Everyone |
| pending <alias> | Links to active pings | Everyone |
| time | Sends cooldown time | Everyone |
| remind | DM's when can ping | Everyone |
|----------------------+----------------------------+-------------------|
| help | Shows this | Everyone |
| aliases | Shows all aliases | Everyone |
|----------------------+----------------------------+-------------------|
| blacklist <@user> | Bans user from pinging | Moderator |
| unblacklist <@user>| Unbans user | Moderator |
| getblacklist | Lists names of banned | Moderator |
| setprefix <prefix> | Sets bot prefix | Moderator |
| settimeout <time> | Sets bot timeout (sec) | Moderator |
| resetuser <@user> | Resets user's cooldown | Moderator |
| addalias | Adds an alias | Moderator |
| removealias | Removes an alias | Moderator |
| stats | DM's ping frequencies | Moderator |
|______________________|____________________________|___________________|
Cooldown Time: {1} minutes and {2} seconds
Current prefix: {3}```"""
def convert_alias(alias):
"""
Converts an alias name to the proper Helper name.
Parameters
----------
alias : str
An alias for the wanted helper name.
Returns
-------
str
The helper name, "ambiguous role", "disabled role", or "" (if none are found).
"""
for role in helper_roles:
if alias in helper_roles[role]:
return role + HELPER_SUFFIX
if alias in AMBIGUOUS_ROLES:
return AMBIGUOUS
for role in DISABLED_ROLES:
if alias in DISABLED_ROLES[role]:
return DISABLED
return None
def split_message(message, embedded=False, language: str=""):
"""
Splits a message into more messages of size less than 2000 characters.
This is to bypass the Discord 2000 character limit.
Parameters
----------
message : str
A long message to split up.
embedded : bool
Whether to embed the message in code format (surrounded by ```)
language : str
Name of the language of the code.
Returns
-------
list of str
All messages to send.
"""
lines = message.split("\n")
curr_message = ""
messages = []
if embedded:
curr_message += "```" + language + "\n"
for line in lines:
if len(line) + len(curr_message) + 5 > 2000:
messages.append(curr_message + "```")
curr_message = "```" + language + line + "\n"
else:
curr_message += line + "\n"
messages.append(curr_message + "```")
else:
for line in lines:
if len(line) + len(curr_message) + 2 > 2000:
messages.append(curr_message)
curr_message = line + "\n"
else:
curr_message += line + "\n"
messages.append(curr_message)
return messages
@bot.event
async def on_message(message):
"""
Handles and interprets all messages sent by the discord and determines if they are commands.
Only accepts messages from a specific guild id because it's meant to only run on one server.
This is hopefully to prevent any manipulation of the blacklist from other servers.
Also makes commands case insensitive.
"""
if message.guild:
if message.guild.id != GUILD_ID: # Prevent anyone from using the bot in another server
return
if message.author in blacklisted_users or message.author.id in blacklisted_users:
# Disable blacklisted from interacting with bot
return
if bot.user.mention in message.content:
await message.channel.send("My current prefix is {0}".format(bot.command_prefix), delete_after=60)
return
message.content = message.content.lower()
await bot.process_commands(message)
@bot.event
async def on_command_error(ctx, error):
"""Responds when some command error is made"""
if hasattr(ctx.command, 'on_error'):
return
# These two are commonly triggered by trying to delete messages.
ignored = (commands.CommandNotFound, discord.Forbidden, discord.NotFound)
error = getattr(error, 'original', error)
if isinstance(error, ignored):
return
elif isinstance(error, commands.NoPrivateMessage):
return await ctx.send("{0}, {1} can not be used in Private Messages.".format(
ctx.author.name, ctx.command), delete_after=60)
elif isinstance(error, commands.MissingPermissions):
await ctx.send("{0}, {1} can not be used because you do not have permission.".format(
ctx.author.name, ctx.command), delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
try:
await ctx.message.delete()
except discord.Forbidden:
pass
return
elif isinstance(error, commands.MissingRequiredArgument):
if ctx.command.qualified_name in ["ping", "pending"]:
await ctx.send("{0}, please enter a valid alias. You can find aliases with {1}alias.".format(
ctx.author.name, bot.command_prefix), delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
try:
await ctx.message.delete()
except discord.Forbidden:
pass
return
elif ctx.command.qualified_name == "settimeout":
await ctx.send("{0}, please include a time in seconds.".format(ctx.author.name), delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
try:
await ctx.message.delete()
except discord.Forbidden:
pass
return
elif ctx.command.qualified_name == "setprefix":
await ctx.send("{0}, please include a prefix to set.".format(ctx.author.name), delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
try:
await ctx.message.delete()
except discord.Forbidden:
pass
return
elif ctx.command.qualified_name in ["blacklist", "unblacklist", "resetuser"]:
await ctx.send("{0}, please include a valid member name. ".format(ctx.author.name), delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
try:
await ctx.message.delete()
except discord.Forbidden:
pass
return
elif isinstance(error, commands.BadArgument):
if ctx.command.qualified_name == "settimeout":
await ctx.send("{0}, please enter a time in seconds.".format(ctx.author.name), delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
try:
await ctx.message.delete()
except discord.Forbidden:
pass
return
elif ctx.command.qualified_name in ["blacklist", "unblacklist", "resetuser"]:
await ctx.send("{0}, please enter a valid member.".format(ctx.author.name), delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
try:
await ctx.message.delete()
except discord.Forbidden:
pass
return
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
@bot.command()
async def help(ctx):
"""DM's bot description and help for the requester."""
await ctx.author.send(HELP_MESSAGE.format(DESCRIPTION, TIMEOUT_TIME//60, TIMEOUT_TIME % 60, bot.command_prefix))
await ctx.message.delete()
@bot.command(aliases=["alias"])
async def aliases(ctx):
"""DM's helper aliases to the requester."""
alias_message = "Alias for helpers:" + "\n" # See alias command
for subject in helper_roles.keys():
alias_message += "* {0:<27}: {1}\n".format(subject, ", ".join(helper_roles[subject]))
alias_message += "\n"
for message in split_message(alias_message, embedded=True):
await ctx.author.send(message)
await ctx.message.delete()
@commands.guild_only()
@bot.command()
async def ping(ctx, *, alias: str):
"""
Pings a helper role after some confirmation and puts the user on cooldown.
In order for a member to ping, they must:
1. Not be on the blacklist.
2. Not be on timeout.
3. Refer clearly to an alias.
4. Confirm that they are going to ping all helpers.
Also, the bot will delete several messages if the ping is confirmed to reduce channel spam.
Parameters
----------
ctx : Context
Context of the message.
alias : str
An alias for a helper role.
"""
if ctx.author in users_on_confirmation:
await ctx.send("{0}, please confirm or cancel your current request before pinging again.".format(
ctx.author.name), delete_after=30)
await ctx.message.delete()
return
if ctx.author in users_on_timeout and not ctx.author.guild_permissions.manage_guild:
time_left = users_on_timeout[ctx.author] # In seconds
await ctx.send("Sorry {0}, but you still have to wait {1} minutes and {2} seconds"
.format(ctx.author.name, time_left // 60, time_left % 60), delete_after=60)
await ctx.message.delete()
return
helper_role = convert_alias(alias)
if helper_role is None:
await ctx.send("Sorry {0}, invalid alias.".format(ctx.author.name), delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
await ctx.message.delete()
return
if helper_role == AMBIGUOUS:
ambiguous_role_response = "There are multiple helper roles that you could be referring to with." \
" Please specify by using one of the below roles and try again.\n```\n"
for r in AMBIGUOUS_ROLES[alias]:
ambiguous_role_response += "\n* " + r + ": " + ", ".join(helper_roles[r]) + "\n"
ambiguous_role_response += "```"
await ctx.send(ctx.author.name + ", " + ambiguous_role_response, delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
await ctx.message.delete()
return
if helper_role == DISABLED:
disabled_role_response = "Sorry, but that helper role has been disabled at the request of the moderators."
await ctx.send(ctx.author.name + ", " + disabled_role_response, delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
await ctx.message.delete()
return
# We may now assume helper_role is verified.
confirm_ping = await ctx.send("{0}, You are about to ping all the {1}s on this server. "
"Please make sure you have clearly elaborated your question and/or shown all work. \n"
"If you have done so, react with ✅ in the next 30 seconds. \n"
"To cancel, react with ❌.\n"
"After 30 seconds, this message will be deleted and your request will be canceled."
.format(ctx.author.name, helper_role))
# Add the new request to the list of requests.
users_on_confirmation.append(ctx.author)
# Add the two options for reacts to make it easier for the user to click their choice.
await confirm_ping.add_reaction("✅")
await confirm_ping.add_reaction("❌")
def ping_check(react, member):
"""Determines if the confirmation is a valid response."""
if member == ctx.author:
if str(react.emoji) == "✅" or str(react.emoji) == "❌":
return True
return False
try:
reaction, user = await bot.wait_for('reaction_add', timeout=30, check=ping_check)
except asyncio.TimeoutError:
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
await ctx.send("Timed out!", delete_after=5)
await confirm_ping.delete()
await ctx.message.delete()
return
if str(reaction.emoji) == "❌":
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
await ctx.send("Canceling request...", delete_after=5)
await confirm_ping.delete()
await ctx.message.delete()
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
return
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
# Member answered Yes
actual_role = discord.utils.get(ctx.guild.roles, name=helper_role)
await actual_role.edit(mentionable=True)
actual_ping = await ctx.send("Ping requested by {0} for {1}\nReact with ✅ when the problem is solved.\n"
"Note: Anyone can react with ✅ and it cannot be revoked.".format(
ctx.author.mention, actual_role.mention))
await actual_role.edit(mentionable=False)
if not ctx.author.guild_permissions.manage_guild:
users_on_timeout[ctx.author] = TIMEOUT_TIME
await confirm_ping.delete()
await ctx.message.delete()
pending_pings[helper_role].append(actual_ping)
await actual_ping.add_reaction("✅")
try:
ping_frequency[helper_role] += 1
except KeyError:
ping_frequency[helper_role] = 1
def done_check(react, ruser):
"""Determines if someone says the problem is solved."""
if ruser == bot.user:
return False
if str(react.emoji) == "✅":
return True
return False
# We do not delete the actual ping message since that gives people safety of deleting their ping.
await bot.wait_for("reaction_add", check=done_check)
del pending_pings[helper_role][pending_pings[helper_role].index(actual_ping)]
@bot.command()
async def time(ctx):
"""Tells the member how much time they have left before being able to ping again."""
try:
time_left = users_on_timeout[ctx.author] # In seconds
await ctx.send("{0}, you still have to wait {1} minutes and {2} seconds"
.format(ctx.author.name, time_left // 60, time_left % 60), delete_after=60)
except KeyError:
await ctx.send("{0}, you are currently allowed to ping a helper.".format(ctx.author.name), delete_after=60)
await ctx.message.delete()
@bot.command(aliases=["notify"])
async def remind(ctx):
"""Informs the member that they will be DM'ed when they are allowed to ping again."""
try:
_ = users_on_timeout[ctx.author]
except KeyError:
await ctx.send("{0}, you are currently allowed to ping a helper.".format(ctx.author.name), delete_after=60)
return
users_to_remind.append(ctx.author)
await ctx.send("{0}, you will receive a DM's when you are allowed to ping again.".format(ctx.author.name),
delete_after=60)
await ctx.message.delete()
@commands.guild_only()
@bot.command(alieses=["pings"])
async def pending(ctx, *, alias: str):
"""Prints all links to pending pings."""
helper_role = convert_alias(alias)
if helper_role is None:
await ctx.send("Sorry {0}, invalid alias.".format(ctx.author.name), delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
await ctx.message.delete()
return
if helper_role == AMBIGUOUS:
ambiguous_role_response = "There are multiple helper roles that you could be referring to with." \
" Please specify by using one of the below roles and try again.\n```\n"
for r in AMBIGUOUS_ROLES[alias]:
ambiguous_role_response += "\n* " + r + ": " + ", ".join(helper_roles[r]) + "\n"
ambiguous_role_response += "```"
await ctx.send(ctx.author.name + ", " + ambiguous_role_response, delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
await ctx.message.delete()
return
if helper_role == DISABLED:
disabled_role_response = "Sorry, but that helper role has been disabled at the request of the moderators."
await ctx.send(ctx.author.name + ", " + disabled_role_response, delete_after=60)
for _ in range(60):
await asyncio.sleep(1)
await ctx.message.delete()
return
if pending_pings[helper_role]:
await ctx.send("{0}, active pings for {1}s are:\n {2}".
format(ctx.author.name, helper_role,
"\n".join(list(map(lambda x: x.jump_url, pending_pings[helper_role])))), delete_after=300)
else:
await ctx.send("{0}, there are no active pings for {1}s.".format(ctx.author.name, helper_role), delete_after=30)
await ctx.message.delete()
@commands.has_permissions(manage_guild=True)
@commands.guild_only()
@bot.command()
async def blacklist(ctx, member: discord.Member):
"""Adds a member to the blacklist."""
if member not in blacklisted_users and member.id not in blacklisted_users:
blacklisted_users.append(member)
await ctx.send("{0} has been blacklisted by {1}.".format(member.mention, ctx.author.name))
else:
await ctx.send("{0}, {1} was already blacklisted".format(ctx.author.name, member.name))
await ctx.message.delete()
@commands.has_permissions(manage_guild=True)
@commands.guild_only()
@bot.command()
async def unblacklist(ctx, member: discord.Member):
"""Removes a member from the blacklist."""
if member in blacklisted_users or member.id in blacklisted_users:
try:
del blacklisted_users[blacklisted_users.index(member)]
except ValueError:
pass
try:
del blacklisted_users[blacklisted_users.index(member.id)]
except ValueError:
pass
await ctx.send("{0} was unblacklisted by {1}.".format(member.mention, ctx.author.name))
else:
await ctx.send("{0}, {1} was already unblacklisted.".format(ctx.author.name, member.name))
await ctx.message.delete()
@commands.has_permissions(manage_guild=True)
@commands.guild_only()
@bot.command()
async def getblacklist(ctx):
"""Sends the blacklist by converting the members to their names."""
if blacklisted_users:
for message in split_message(ctx.author.name + ", blacklisted members are: " + ", ".join(
list(map(lambda x: x.name if not isinstance(x, int) else str(ctx.guild.get_member(x)),
blacklisted_users)))):
await ctx.send(message)
else:
await ctx.send("{0}, no members are blacklisted.".format(ctx.author.name))
await ctx.message.delete()
@commands.has_permissions(manage_guild=True)
@commands.guild_only()
@bot.command()
async def addalias(ctx):
"""Adds an alias to a helper role after some selections."""
if ctx.author in users_on_confirmation:
await ctx.send("{0}, please confirm or cancel your current request before trying again.".format(
ctx.author.name), delete_after=30)
await ctx.message.delete()
return
helper_message = "Please choose the number of the helper role you want to add to:\n" \
"To cancel, type 'cancel'\n\n"
for i, alias in enumerate(helper_roles.keys()):
helper_message += "{0}: {1}\n".format(i+1, alias)
users_on_confirmation.append(ctx.author)
helper_prompt = await ctx.send(helper_message)
cancels = ["exit", "cancel", "quit", "stop"]
def helper_check(message):
"""Checks whether the user quit or selected a valid number in the helper role index."""
if message.author == ctx.author and message.channel == ctx.channel:
if message.content in cancels:
return True
try:
select = int(message.content)
except ValueError:
return False
if select in range(1, len(helper_roles.keys()) + 1):
return True
return False
try:
helper_choice = await bot.wait_for("message", check=helper_check, timeout=60)
except asyncio.TimeoutError:
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
await ctx.send("Timed out!", delete_after=10)
await ctx.message.delete()
await helper_prompt.delete()
return
try:
helper_number = int(helper_choice.content) - 1
except ValueError:
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
await ctx.send("Canceling request...", delete_after=10)
await ctx.message.delete()
await helper_prompt.delete()
await helper_choice.delete()
return
helper_name = list(helper_roles.keys())[helper_number]
alias_prompt = await ctx.send("Now enter your alias name:\n"
"To cancel, type 'cancel'")
def author_check(message):
"""Checks whether the message is by the original requester."""
if message.author == ctx.author and message.channel == ctx.channel:
return True
return False
try:
new_alias = await bot.wait_for('message', check=author_check, timeout=60)
except asyncio.TimeoutError:
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
await ctx.send("Timed out!", delete_after=5)
await ctx.message.delete()
await helper_prompt.delete()
await helper_choice.delete()
await alias_prompt.delete()
return
if new_alias.content in cancels:
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
await ctx.send("Canceling request...", delete_after=5)
await ctx.message.delete()
await helper_prompt.delete()
await helper_choice.delete()
await alias_prompt.delete()
await new_alias.delete()
return
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
if new_alias.content.lower() not in helper_roles[helper_name]:
helper_roles[helper_name].append(new_alias.content.lower())
await ctx.send("The alias {0} has been added to the {1} Helper aliases by {2}.".format(
new_alias.content.lower(), helper_name, ctx.author.name))
else:
await ctx.send("{0}, the alias {1} was already in {2} Helper aliases".format(
ctx.author.name, new_alias.content.lower(), helper_name))
await ctx.message.delete()
await helper_prompt.delete()
await helper_choice.delete()
await alias_prompt.delete()
await new_alias.delete()
@commands.has_permissions(manage_guild=True)
@commands.guild_only()
@bot.command(aliases=["deletealias"])
async def removealias(ctx):
"""Removes an alias from the helper roles after some selections."""
if ctx.author in users_on_confirmation:
await ctx.send("{0}, please confirm or cancel your current request before trying again.".format(
ctx.author.name), delete_after=30)
await ctx.message.delete()
return
helper_message = "Please choose the number of the helper role you want to remove from:\n" \
"To cancel, type 'cancel'\n\n"
for i, alias in enumerate(helper_roles.keys()):
helper_message += "{0}: {1}\n".format(i + 1, alias)
users_on_confirmation.append(ctx.author)
helper_prompt = await ctx.send(helper_message)
cancels = ["exit", "cancel", "quit", "stop"]
def helper_check(message):
"""Checks whether the user quit or selected a valid number in the helper role index."""
if message.author == ctx.author and message.channel == ctx.channel:
if message.content in cancels:
return True
try:
select = int(message.content)
except ValueError:
return False
if select in range(1, len(helper_roles.keys()) + 1):
return True
return False
try:
helper_choice = await bot.wait_for("message", check=helper_check, timeout=60)
except asyncio.TimeoutError:
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
await ctx.send("Timed out!", delete_after=5)
await ctx.message.delete()
await helper_prompt.delete()
return
try:
helper_number = int(helper_choice.content) - 1
except ValueError:
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
await ctx.send("Canceling request...", delete_after=5)
await ctx.message.delete()
await helper_prompt.delete()
await helper_choice.delete()
return
helper_name = list(helper_roles.keys())[helper_number]
alias_message = "Now select the number of the alias you want to remove:\n" \
"To cancel, type 'cancel'\n\n"
for i, alias in enumerate(helper_roles[helper_name]):
alias_message += "{0}: {1}\n".format(i + 1, alias)
alias_prompt = await ctx.send(alias_message)
def alias_check(message):
"""Checks whether the author quit or selected a valid number in the helper alias index."""
if message.author == ctx.author and message.channel == ctx.channel:
if message.content in cancels:
return True
try:
select = int(message.content)
except ValueError:
return False
if select in range(1, len(helper_roles[helper_name]) + 1):
return True
return False
try:
alias_choice = await bot.wait_for('message', check=alias_check, timeout=60)
except asyncio.TimeoutError:
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
await ctx.send("Timed out!", delete_after=5)
await ctx.message.delete()
await helper_prompt.delete()
await helper_choice.delete()
await alias_prompt.delete()
return
try:
alias_number = int(alias_choice.content) - 1
except ValueError:
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
await ctx.send("Canceling request...", delete_after=5)
await ctx.message.delete()
await helper_prompt.delete()
await helper_choice.delete()
await alias_prompt.delete()
await alias_choice.delete()
return
alias_name = helper_roles[helper_name][alias_number]
del users_on_confirmation[users_on_confirmation.index(ctx.author)]
if alias_name in helper_roles[helper_name]:
del helper_roles[helper_name][alias_number]
await ctx.send("The alias {0} has been removed from the {1} Helper aliases by {2}.".format(
alias_name, helper_name, ctx.author.name))
else:
await ctx.send("{0}, the alias {1} was not in the {2} Helper aliases.".format(
ctx.author.name, alias_name, helper_name))
await ctx.message.delete()
await helper_prompt.delete()
await helper_choice.delete()
await alias_prompt.delete()
await alias_choice.delete()
@commands.has_permissions(manage_guild=True)
@commands.guild_only()
@bot.command(aliases=["reset"])
async def resetuser(ctx, member: discord.Member):
"""Reset a user's current timeout, allowing them to ping a helper."""
if not ctx.author.guild_permissions.manage_guild:
return
if member in users_on_timeout:
del users_on_timeout[member]
await ctx.send("{0} can now ping a helper (reset by {1}).".format(member.name, ctx.author.name))
else:
await ctx.send("{0} can already ping helpers (reset by {1}).".format(member.name, ctx.author.name))
if member in users_to_remind: # The ping should be sufficient.
del users_to_remind[users_to_remind.index(member)]
if member in users_on_confirmation: # Just in case.
del users_on_confirmation[users_to_remind.index(member)]
await ctx.message.delete()
@commands.has_permissions(manage_guild=True)
@commands.guild_only()
@bot.command(aliases=["prefix"])
async def setprefix(ctx, prefix: str):
"""Changes the prefix of the bot."""
if not ctx.author.guild_permissions.manage_guild:
return
bot.command_prefix = prefix
await ctx.send("{0} set the bot prefix set to {1}".format(ctx.author.name, prefix))
await bot.change_presence(activity=discord.Game(name="{0}help for commands!".format(bot.command_prefix)))
await ctx.message.delete()
@commands.has_permissions(manage_guild=True)
@commands.guild_only()
@bot.command(aliases=["settime"])
async def settimeout(ctx, seconds: int):
"""Set the length of the timeout in seconds."""
global TIMEOUT_TIME
if not ctx.author.guild_permissions.manage_guild:
return
TIMEOUT_TIME = int(seconds)
await ctx.send("{0} set the timeout length to {1} minutes and {2} seconds.".format(
ctx.author.name, seconds // 60, seconds % 60))
await ctx.message.delete()
@commands.has_permissions(manage_guild=True)
@commands.guild_only()
@bot.command()
async def stats(ctx):
"""Prints ping frequency of all helper roles that have been pinged"""
if not ctx.author.guild_permissions.manage_guild:
return
roles = sorted(ping_frequency, key=lambda x: x)
total_pings = sum([ping_frequency[k] for k in roles])
stats_message = "Percentages of pings out of {0} pings\n\n".format(total_pings)
for role in roles:
stats_message += "{0:<34}: ".format(role)
stats_message += ("{:04." + str(len(str(total_pings)) + 1) + "f}\n").format(
ping_frequency[role] / total_pings * 100)
stats_message += "\nTotal number of pings out of {0} pings\n\n".format(total_pings)
for role in roles:
stats_message += "{0:<34}: ".format(role)
stats_message += "{0}\n".format(ping_frequency[role])
for message in split_message(stats_message, embedded=True):
await ctx.author.send(message)
await ctx.message.delete()
@commands.is_owner()
@bot.command()
async def logout(ctx):
await ctx.send("Logging out.")
print("Logging out")
print("------")
await bot.logout()
@bot.event
async def on_ready():
"""Prints important bot information and sets up background tasks. Converts ids."""
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
await bot.change_presence(activity=discord.Game(name="{0}help for commands!".format(bot.command_prefix)))
convert_ids()
async def update_timer():
"""Updates all students' cooldown periods every second."""
while True:
await asyncio.sleep(1)
names_to_remove = []
for member in users_on_timeout:
users_on_timeout[member] -= 1
if users_on_timeout[member] <= 0:
names_to_remove.append(member)
for member in names_to_remove:
if member in users_to_remind:
await member.send("This is your reminder that you are now allowed to ping helpers.")
del users_on_timeout[member]
def write_data():
"""Writes the blacklist to a JSON file using the member's ids."""
with open("aliases.json", mode="w", encoding="UTF-8") as f:
f.write(json.dumps(helper_roles))
print("Aliases saved!")
with open("blacklist.json", mode="w") as f:
for i in range(len(blacklisted_users)):
try:
blacklisted_users[i] = blacklisted_users[i].id
except AttributeError:
pass
f.write(json.dumps(blacklisted_users))
print("Blacklist saved!")
with open("settings.json", mode="w") as f:
var_dict = dict()
var_dict[KEY_PREFIX] = bot.command_prefix
var_dict[KEY_TIMEOUT] = TIMEOUT_TIME
f.write(json.dumps(var_dict))
print("Settings saved!")
with open("stats.json", mode="w") as f:
f.write(json.dumps(ping_frequency))
print("Ping frequency saved!")
def load_data():
"""Reads the ids from the blacklist from the JSON file"""
global blacklisted_users
global helper_roles
global TIMEOUT_TIME
global ping_frequency
global pending_pings
with open("aliases.json", mode="r", encoding="UTF-8") as f:
try:
helper_roles = json.loads(f.read())
except JSONDecodeError:
print("Aliases failed to load.")
raise
else:
print("Aliases loaded successfully.")
for alias_list in helper_roles.values():
alias_list.sort(key=len)
for role in helper_roles:
pending_pings[role + HELPER_SUFFIX] = []
with open("blacklist.json", mode="r") as f:
try:
blacklisted_users = json.loads(f.read())
except JSONDecodeError:
print("Invalid blacklist found.")
print("File contents: " + f.read())
else:
print("Blacklist loaded successfully.")
with open("settings.json", mode="r") as f:
try:
settings = json.loads(f.read())
bot.command_prefix = settings[KEY_PREFIX]
TIMEOUT_TIME = settings[KEY_TIMEOUT]
except JSONDecodeError:
print("No settings found.")
print("File contents: " + f.read())
else:
print("Settings loaded successfully.")
with open("stats.json", mode="r") as f:
try:
ping_frequency = json.loads(f.read())
except JSONDecodeError:
print("Invalid statistics found.")
print("File contents: " + f.read())
else:
print("Statistics loaded successfully.")
def convert_ids():
"""Converts all ids of the server from the blacklist by using the GUILD_ID constant."""
global blacklisted_users
print("Converting ids")
for i in range(len(blacklisted_users) - 1, -1, -1):
member = bot.get_guild(GUILD_ID).get_member(blacklisted_users[i]) # Can be None
if member:
blacklisted_users[i] = member
# If it is None, we can just keep the id.
else:
print("Members converted from ids successfully.")