-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmodmail.py
More file actions
1538 lines (1271 loc) · 52.6 KB
/
modmail.py
File metadata and controls
1538 lines (1271 loc) · 52.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Runs a bot that can be messaged to create modmail threads
Unit tests: False
Config:
File: enable_modmail, disable_thread_creation, modmail_auth_token, modmail_prefix,
modmail_guild, modmail_forum_channel, modmail_log_channel
Command: aliases, automatic_responses, modmail_roles, roles_to_ping, thread_creation_message
API: None
Postgresql: True
Models: ModmailBan
Commands: contact, modmail commands, modmail ban, modmail unban
"""
from __future__ import annotations
import asyncio
import re
from datetime import datetime
from typing import TYPE_CHECKING, Self
import discord
import expiringdict
import munch
import ui
from core import auxiliary, cogs, extensionconfig
from discord.ext import commands
if TYPE_CHECKING:
import bot
async def has_modmail_management_role(
ctx: commands.Context, config: munch.Munch = None
) -> bool:
"""-COMMAND CHECK-
Checks if the invoker has a modmail management role
Args:
ctx (commands.Context): Context used for getting the config file
config (munch.Munch): Can be defined manually to run this without providing actual ctx
Raises:
CommandError: No modmail management roles were assigned in the config
MissingAnyRole: Invoker doesn't have a modmail role
Returns:
bool: Whether the invoker has a modmail management role
"""
# Only running this line of code if config isn't manually defined allows the use of
# a discord.Message object in place of ctx
if not config:
config = ctx.bot.guild_configs[str(ctx.guild.id)]
user_roles = getattr(ctx.author, "roles", [])
unparsed_roles = config.extensions.modmail.modmail_roles.value
modmail_roles = []
if not unparsed_roles:
raise commands.CommandError("No modmail roles were assigned in the config file")
# Deduplicates the list
unparsed_roles = list(dict.fromkeys(unparsed_roles))
# Two for loops are needed, because an array containing all modmail roles is needed for
# the error thrown when the user doesn't have any relevant roles.
for role_id in config.extensions.modmail.modmail_roles.value:
role = discord.utils.get(ctx.guild.roles, id=int(role_id))
if not role:
continue
modmail_roles.append(role)
if not any(role in user_roles for role in modmail_roles):
raise commands.MissingAnyRole(modmail_roles)
return True
class Modmail_bot(discord.Client):
"""The bot used to send and receive DM messages"""
@commands.Cog.listener()
async def on_message(self: Self, message: discord.Message) -> None:
"""Listen to DMs, send them to handle_dm for proper handling when applicable
Args:
message (discord.Message): Every sent message, gets filtered to only dms
"""
if isinstance(message.channel, discord.DMChannel) and not message.author.bot:
# Log all DMs regardless of what happens to them
await Ts_client.log_DM(
message.author,
"Modmail",
message.content,
)
# User is banned from creating modmail threads
if await Ts_client.models.ModmailBan.query.where(
Ts_client.models.ModmailBan.user_id == str(message.author.id)
).gino.first():
await message.add_reaction("❌")
return
# Makes sure existing threads can still be responded to
if message.author.id not in active_threads and DISABLE_THREAD_CREATION:
await message.add_reaction("❌")
await auxiliary.send_deny_embed(
message="Modmail isn't accepting messages right now. "
+ "Please try again later.",
channel=message.channel,
)
return
# Spam protection
if message.author.id in delayed_people:
await message.add_reaction("🕒")
await auxiliary.send_deny_embed(
message="To restrict spam, you are timed out from creating new threads. "
+ "You are welcome to create a new thread after 24 hours since your previous"
+ " thread's closing.",
channel=message.channel,
)
return
# Everything looks good - handle dm properly
await handle_dm(message)
@commands.Cog.listener()
async def on_typing(
self: Self, channel: discord.DMChannel, user: discord.User, _: datetime
) -> None:
"""When someone starts typing in modmails dms, start typing in the corresponding thread
Args:
channel (discord.DMChannel): The channel where someone started typing
user (discord.User): The user who started typing
"""
if isinstance(channel, discord.DMChannel) and user.id in active_threads:
await self.get_channel(active_threads[user.id]).typing()
@commands.Cog.listener()
async def on_message_edit(
self: Self, before: discord.Message, after: discord.Message
) -> None:
"""When someone edits a message, send the event to the appropriate thread
Args:
before (discord.Message): The message prior to editing
after (discord.Message): The message after editing
"""
if (
isinstance(before.channel, discord.DMChannel)
and before.author.id in active_threads
):
if await Ts_client.models.ModmailBan.query.where(
Ts_client.models.ModmailBan.user_id == str(before.author.id)
).gino.first():
return
thread = self.get_channel(active_threads[before.author.id])
embed = discord.Embed(
color=discord.Color.blue(),
title="Message edit",
description=f"Message ID: {before.id}",
)
embed.timestamp = datetime.now()
# This is here to save space if this listener is triggered by something other than
# a content modification, i.e. a message being pinned
if before.content == after.content:
return
# Length handling has to be here, 1024 is the limit for inividual fields
if len(before.content) > 1016 or len(after.content) > 1016:
embed.set_footer(
text="Edit was too long to send! Sending just the result instead..."
)
embed.description += (
f"\n\n**New contents:**\n```{after.content[:5975]}```"
)
# Length is fine, send as usual
else:
embed.add_field(
name="Before", value=f"```\n{before.content}```"
).add_field(name="After", value=f"```\n{after.content}```")
await thread.send(embed=embed)
@commands.Cog.listener()
async def on_member_remove(self: Self, member: discord.Member) -> None:
"""Sends a message into a thread if the addressee left
Args:
member (discord.Member): The member who left
"""
if member.id in active_threads:
thread = self.get_channel(active_threads[member.id])
embed = discord.Embed(
color=discord.Color.red(),
title="Member left",
description=f"{member.mention} has left the guild.",
)
await thread.send(embed=embed)
@commands.Cog.listener()
async def on_member_join(self: Self, member: discord.Member) -> None:
"""Sends a message into a thread if the addressee joined the guild with an active thread
Args:
member (discord.Member): The member who joined
"""
if member.id in active_threads:
thread = self.get_channel(active_threads[member.id])
embed = discord.Embed(
color=discord.Color.blue(),
title="Member joined",
description=f"{member.mention} has rejoined the guild.",
)
await thread.send(embed=embed)
# These get assigned in the __init__, are needed for inter-bot comm
# It is a goofy solution but given that this extension is only used in ONE guild, it's good enough
Ts_client = None
DISABLE_THREAD_CREATION = None
MODMAIL_FORUM_ID = None
MODMAIL_LOG_CHANNEL_ID = None
AUTOMATIC_RESPONSES = None
AUTOMATIC_REJECTIONS = None
ROLES_TO_PING = None
THREAD_CREATION_MESSAGE = None
active_threads = {} # User id: Thread id
closure_jobs = {} # Used in timed closes
# Is a dict because expiringDict only has dictionaries... go figure
delayed_people = expiringdict.ExpiringDict(
max_age_seconds=93600, max_len=1000 # max_len has to be set for some reason
)
# This is needed to prevent being able to open more than one thread by sending several messages
# and then clicking the confirmations really quickly
awaiting_confirmation = []
# Prepares the Modmail client with the Members intent used for lookups
# Is started in __init__ of the modmail exntension, the client is defined here
# since it is used elsewhere
intents = discord.Intents.default()
intents.members = True
Modmail_client = Modmail_bot(intents=intents)
async def build_attachments(
thread: discord.Thread, message: discord.Message
) -> list[discord.File]:
"""Returns a list of as many files from a message as the bot can send to the given channel
Args:
thread (discord.Thread): The thread the attachments are going to be sent to
(To get the maximum file size)
message (discord.Message): The message to get the attachments from
Returns:
list[discord.File]: The list of file objects ready to be sent
"""
attachments: list[discord.File] = []
total_attachment_size = 0
for attachment in message.attachments:
# Add attachments until the max file size is reached
if (
total_attachment_size := total_attachment_size + attachment.size
) <= thread.guild.filesize_limit:
attachments.append(await attachment.to_file())
# The attachments were too big
if (failed_amount := len(message.attachments) - len(attachments)) != 0:
await thread.send(
f"{failed_amount} additional attachments were detected, but were too big to send!"
)
return attachments
async def handle_dm(message: discord.Message) -> None:
"""Sends a message to the corresponding thread, creates one if needed
Args:
message (discord.Message): The incoming message
"""
# The bot is not ready to handle dms yet, this should only take a few seconds after startup
if not Ts_client or not MODMAIL_FORUM_ID:
await message.channel.send(
embed=auxiliary.generate_basic_embed(
color=discord.Color.light_gray(),
description="Bot is still starting, please wait...",
)
)
return
# The user already has an open thread
if message.author.id in active_threads:
thread = Ts_client.get_channel(active_threads[message.author.id])
# If thread was going to be closed, cancel the task
if thread.id in closure_jobs:
closure_jobs[thread.id].cancel()
del closure_jobs[thread.id]
await thread.send(
embed=discord.Embed(
color=discord.Color.red(),
description="Scheduled close has been cancelled.",
)
)
embed = discord.Embed(color=discord.Color.blue(), description=message.content)
embed.set_footer(text=f"Message ID: {message.id}")
embed.timestamp = datetime.now()
if message.author.avatar:
embed.set_author(name=message.author, icon_url=message.author.avatar.url)
else:
embed.set_author(
name=message.author, icon_url=message.author.default_avatar.url
)
attachments = None
if message.attachments:
if not message.content:
embed.description = "*<Attachment>*"
attachments = await build_attachments(thread=thread, message=message)
# This should only happen if a sticker was sent, is here so an empty message isn't sent
if not embed.description:
return
await thread.send(embed=embed, files=attachments)
await message.add_reaction("📨")
return
# - No thread was found, create one -
for regex in AUTOMATIC_REJECTIONS:
if re.match(regex, message.content):
await auxiliary.send_deny_embed(
message="This message cannot be used to start a "
+ f"thread: {AUTOMATIC_REJECTIONS[regex]}",
channel=message.channel,
)
return
if message.author.id in awaiting_confirmation:
await auxiliary.send_deny_embed(
message="Please respond to the existing prompt before trying to open a new modmail"
+ " thread!",
channel=message.channel,
)
return
confirmation = ui.Confirm()
await confirmation.send(
message=THREAD_CREATION_MESSAGE,
channel=message.channel,
author=message.author,
)
awaiting_confirmation.append(message.author.id)
await confirmation.wait()
if confirmation.value == ui.ConfirmResponse.DENIED:
awaiting_confirmation.remove(message.author.id)
await auxiliary.send_deny_embed(
message="Thread creation cancelled.",
channel=message.channel,
)
return
if confirmation.value == ui.ConfirmResponse.TIMEOUT:
awaiting_confirmation.remove(message.author.id)
await auxiliary.send_deny_embed(
message="Thread confirmation prompt timed out, please hit `Confirm` or `Cancel` when "
+ "creating a new thread. You are welcome to send another message.",
channel=message.channel,
)
return
if not await create_thread(
channel=Ts_client.get_channel(MODMAIL_FORUM_ID),
user=message.author,
source_channel=message.channel,
message=message,
):
return
await message.add_reaction("📨")
awaiting_confirmation.remove(message.author.id)
async def create_thread(
channel: discord.TextChannel,
user: discord.User,
source_channel: discord.TextChannel,
message: discord.Message = None,
) -> bool:
"""Creates a thread from a DM message.
The message is left blank when invoked by the contact command
Args:
channel (discord.TextChannel): The forum channel to create the thread in
user (discord.User): The user who sent the DM or is being contacted
source_channel (discord.TextChannel): Used for error handling
message (discord.Message, optional): The incoming message
Returns:
bool: Whether the thread was created succesfully
"""
# --> CHECKS <--
# These checks can be triggered on both the users and server side using .contact
# The code adjusts the error for formatting purposes
if user.id in active_threads:
# Ran from a DM
if message:
await auxiliary.send_deny_embed(
message="You already have an open thread!",
channel=source_channel,
)
else:
await auxiliary.send_deny_embed(
message=f"User already has an open thread! <#{active_threads[user.id]}>",
channel=source_channel,
)
return False
# --> WELCOME MESSAGE <--
embed = discord.Embed(color=discord.Color.blue())
# Formatting the description of the initial message
description = (
f"{user.mention} was created {discord.utils.format_dt(user.created_at, 'R')}"
)
past_thread_count = 0
async for thread in channel.archived_threads():
if not thread.name.startswith("[OPEN]") and thread.name.split("|")[
-1
].strip() == str(user.id):
past_thread_count += 1
if past_thread_count == 0:
description += ", has **no** past threads"
else:
description += f", has **{past_thread_count}** past threads"
# If the user is a member, do member specific things
member = channel.guild.get_member(user.id)
if member:
description += f", joined {discord.utils.format_dt(member.joined_at, 'R')}"
embed.add_field(name="Nickname", value=member.nick)
role_string = "None"
roles = []
for role in sorted(member.roles, key=lambda x: x.position, reverse=True):
if role.is_default():
continue
roles.append(role.mention)
if roles:
role_string = ", ".join(roles)
embed.add_field(name="Roles", value=role_string)
# This shouldn't be possible because to dm the bot you need to share a server
# Is still here for safety
else:
description += ", is not in this server"
# Only adds the avatar if the user has one
if user.avatar:
url = user.avatar.url
else:
url = user.default_avatar.url
# has to be done like this because of member handling
embed.description = description
embed.set_author(name=user, icon_url=url)
embed.timestamp = datetime.now()
embed.set_footer(text=f"User ID: {user.id}")
# Handling for roles to ping, not performed if the func was invoked by the contact command
role_string = ""
if message and ROLES_TO_PING:
for role_id in ROLES_TO_PING:
role_string += f"<@&{role_id}> "
# --> THREAD CREATION <--
# All threads in the modmail forum channel HAVE to follow this scheme as long as they start
# with [CLOSED] or [OPEN]:
# [STATUS] | Username | Date of creation | User id
thread = await channel.create_thread(
name=f"[OPEN] | {user} | {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} | {user.id}",
embed=embed,
content=role_string.rstrip()[:2000],
allowed_mentions=discord.AllowedMentions(roles=True),
)
active_threads[user.id] = thread[0].id
# The thread creation was invoked from an incoming message
if message:
# - Server side -
embed = discord.Embed(color=discord.Color.blue(), description=message.content)
embed.set_author(name=user, icon_url=url)
embed.set_footer(text=f"Message ID: {message.id}")
embed.timestamp = datetime.now()
attachments = None
if message.attachments:
if not message.content:
embed.description = "*<Attachment>*"
attachments = await build_attachments(thread=thread[0], message=message)
await thread[0].send(embed=embed, files=attachments)
# - User side -
embed = discord.Embed(
color=discord.Color.green(),
description="The staff will get back to you as soon as possible.",
)
embed.set_author(name="Thread Created")
embed.set_footer(text="Your message has been sent.")
embed.timestamp = datetime.now()
await message.author.send(embed=embed)
# - Auto responses -
for regex in AUTOMATIC_RESPONSES:
if re.match(regex, message.content):
await reply_to_thread(
raw_contents=AUTOMATIC_RESPONSES[regex],
message=message,
thread=thread[0],
anonymous=True,
automatic=True,
)
return True
return True
async def reply_to_thread(
raw_contents: str,
message: discord.Message,
thread: discord.Thread,
anonymous: bool,
automatic: bool = False,
) -> None:
"""Replies to a modmail thread on both the dm side and the modmail thread side
Args:
raw_contents (str): The raw content string
message (discord.Message): The outgoing message, used for attachments and author handling
thread (discord.Thread): The thread to reply to
anonymous (bool): Whether to reply anonymously
automatic (bool, optional): Whether this response was automatic
"""
# If thread was going to be closed, cancel the task
if thread.id in closure_jobs:
closure_jobs[thread.id].cancel()
del closure_jobs[thread.id]
await thread.send(
embed=discord.Embed(
color=discord.Color.red(),
description="Scheduled close has been cancelled.",
)
)
# Gets the user from the guild instead of just looking for them by the id, because modmail
# can't contact users it doesn't share a guild with. Acts as protection for people who left.
target_member = discord.utils.get(
thread.guild.members, id=int(thread.name.split("|")[-1].strip())
)
if not target_member:
await auxiliary.send_deny_embed(
message="This user isn't in the guild, so the message cannot be sent",
channel=thread,
)
return
# - Modmail thread side -
embed = discord.Embed(color=discord.Color.green())
# if there are any attachments sent, this will be changed to a list of files
attachments = None
# The attachments that will be sent to the user, has to be remade since they become invlaid
# after being sent to the thread
user_attachments = None
if raw_contents:
embed.description = raw_contents
# Makes sure an empty message won't be sent
elif not message.attachments:
await auxiliary.send_deny_embed(
message="You need to include message contents!", channel=thread
)
return
# Properly handles any attachments
if message.attachments:
if not raw_contents:
embed.description = "*<Attachment>*"
attachments = await build_attachments(thread=thread, message=message)
if not attachments:
await auxiliary.send_deny_embed(
message="Failed to build any attachments!", channel=thread
)
# No need to reconfirm
user_attachments = await build_attachments(thread=thread, message=message)
embed.timestamp = datetime.now()
embed.set_footer(text="Response")
if automatic:
embed.set_author(name=thread.guild, icon_url=thread.guild.icon.url)
elif message.author.avatar:
embed.set_author(name=message.author, icon_url=message.author.avatar.url)
else:
embed.set_author(
name=message.author, icon_url=message.author.default_avatar.url
)
if automatic:
embed.set_footer(text="[Automatic] Response")
elif message.author == Ts_client.user:
embed.set_footer(text="[Automatic] Response")
elif anonymous:
embed.set_footer(text="[Anonymous] Response")
# Attachments is either None or a list of files, discord can handle either
await thread.send(embed=embed, files=attachments)
# - User side -
embed.set_footer(text="Response")
if anonymous:
embed.set_author(
name=f"{thread.guild.name} Moderator", icon_url=thread.guild.icon.url
)
# Refetches the user from modmails client so it can reply to it instead of TS
user = Modmail_client.get_user(target_member.id)
# Attachments is either None or a list of files, discord can handle either
await user.send(embed=embed, files=user_attachments)
async def close_thread(
thread: discord.Thread,
silent: bool,
timed: bool,
log_channel: discord.TextChannel,
closed_by: discord.User,
) -> None:
"""Closes a thread instantly or with a delay
Args:
thread (discord.Thread): The thread to close
silent (bool): Whether to send a closure message to the user
timed (bool): Whether to wait 5 minutes before closing
log_channel (discord.TextChannel): The channel to send the closure message to
closed_by (discord.User): The person who closed the thread
"""
user_id = int(thread.name.split("|")[-1].strip())
user = Modmail_client.get_user(user_id)
# Waits 5 minutes before closing, below is only executed when func was run as an asyncio job
if timed:
embed = discord.Embed(
color=discord.Color.red(),
description="This thread will close in 5 minutes.",
)
embed.set_author(name="Scheduled close")
embed.set_footer(
text="Closing will be cancelled if a message is sent, or if the command is run again."
)
embed.timestamp = datetime.now()
await thread.send(embed=embed)
await asyncio.sleep(300)
# - Actually starts closing the thread -
# Removes closure job from queue if it's there
if thread.id in closure_jobs:
# Makes sure the close job doesn't kill itself
if not timed:
closure_jobs[thread.id].cancel()
del closure_jobs[thread.id]
# Archives and locks the thread
if silent:
await thread.send(
embed=auxiliary.generate_basic_embed(
color=discord.Color.red(),
title="Thread Silently Closed.",
description="",
)
)
else:
await thread.send(
embed=auxiliary.generate_basic_embed(
color=discord.Color.red(),
title="Thread Closed.",
description="",
)
)
await thread.edit(
name=f"[CLOSED] {thread.name[7:]}",
archived=True,
locked=True,
)
await log_closure(thread, user_id, log_channel, closed_by, silent)
# User has left the guild
if not user:
del active_threads[user_id]
# No value needed, just has to exist in the dictionary
delayed_people[user_id] = ""
return
# User can't be None anymore
del active_threads[user.id]
# No value needed, just has to exist in the dictionary
delayed_people[user.id] = ""
if silent:
return
# Sends the closure message to the user
embed = discord.Embed(
color=discord.Color.light_gray(),
description="Please wait 24 hours before creating a new one.",
)
embed.set_author(name="Thread Closed")
embed.timestamp = datetime.now()
await user.send(embed=embed)
async def log_closure(
thread: discord.Thread,
user_id: int,
log_channel: discord.TextChannel,
closed_by: discord.User,
silent: bool,
) -> None:
"""Sends a closure message to the log channel
Args:
thread (discord.Thread): The thread that got closed
user_id (int): The id of the person who created the thread, not an user object to
be able to include the ID even if the user leaves the guild
log_channel (discord.TextChannel): The log channel to send the closure message to
closed_by (discord.User): The person who closed the thread
silent (bool): Whether the thread was closed silently
"""
user = Modmail_client.get_user(user_id)
if not user:
embed = discord.Embed(
color=discord.Color.red(),
description=f"<#{thread.id}>",
title=f"<User has left the guild> `{user_id}`",
)
else:
embed = discord.Embed(
color=discord.Color.red(),
description=f"<#{thread.id}>",
title=f"{user.name} `{user.id}`",
)
if silent:
embed.set_footer(
icon_url=closed_by.avatar.url,
text=f"Thread silently closed by {closed_by.name}",
)
else:
embed.set_footer(
icon_url=closed_by.avatar.url,
text=f"Thread closed by {closed_by.name}",
)
embed.timestamp = datetime.now()
await log_channel.send(embed=embed)
async def setup(bot: bot.TechSupportBot) -> None:
"""Loading the Modmail plugin into the bot
Args:
bot (bot.TechSupportBot): The bot object to register the cogs to
Raises:
AttributeError: Raised if modmail is disabled
"""
# Only runs if modmail is enabled
if not bot.file_config.modmail_config.enable_modmail:
# Raising an exception makes the extension loading mark as failed, this is surprisingly
# the most reliable way to ensure the modmail bot or code doesn't run
raise AttributeError("Modmail was not loaded because it's disabled")
config = extensionconfig.ExtensionConfig()
config.add(
key="aliases",
datatype="dict",
title="Aliases for modmail messages",
description="Custom modmail commands to send message slices",
default={},
)
config.add(
key="automatic_responses",
datatype="dict",
title="Modmail autoresponses",
description="If someone sends a message containing a key, sends its value",
default={},
)
config.add(
key="automatic_rejections",
datatype="dict",
title="Modmail auto-rejections",
description="If someone sends a message matching regex, blocks thread creation",
default={},
)
config.add(
key="modmail_roles",
datatype="list",
title="Roles that can access modmail and its commands",
description="Roles that can access modmail and its commands",
default=[],
)
config.add(
key="roles_to_ping",
datatype="list",
title="Roles to ping on thread creation",
description="Roles to ping on thread creation",
default=[],
)
config.add(
key="thread_creation_message",
datatype="str",
title="Thread creation message",
description="The message sent to the user when confirming a thread creation.",
default="Create modmail thread?",
)
await bot.add_cog(Modmail(bot=bot))
bot.add_extension_config("modmail", config)
class Modmail(cogs.BaseCog):
"""The modmail cog class
Args:
bot (bot.TechSupportBot): The main TS bot object to be stored in modmail
"""
def __init__(self: Self, bot: bot.TechSupportBot) -> None:
# Init is used to make variables global so they can be used on the modmail side
super().__init__(bot=bot)
# Makes the TS client available globally for creating threads and populating them with info
# pylint: disable=W0603
global Ts_client
Ts_client = bot
Ts_client.loop.create_task(
Modmail_client.start(bot.file_config.modmail_config.modmail_auth_token)
)
# -> This makes the configs available from the whole file, this can only be done here
# -> thanks to modmail only being available in one guild. It is NEEDED for inter-bot comms
# -> Pylint disables present because it bitches about using globals
# pylint: disable=W0603
global DISABLE_THREAD_CREATION
DISABLE_THREAD_CREATION = bot.file_config.modmail_config.disable_thread_creation
# pylint: disable=W0603
global MODMAIL_FORUM_ID
MODMAIL_FORUM_ID = int(bot.file_config.modmail_config.modmail_forum_channel)
# pylint: disable=W0603
global MODMAIL_LOG_CHANNEL_ID
MODMAIL_LOG_CHANNEL_ID = int(bot.file_config.modmail_config.modmail_log_channel)
config = bot.guild_configs[str(bot.file_config.modmail_config.modmail_guild)]
# pylint: disable=W0603
global AUTOMATIC_RESPONSES
AUTOMATIC_RESPONSES = config.extensions.modmail.automatic_responses.value
# pylint: disable=W0603
global AUTOMATIC_REJECTIONS
AUTOMATIC_REJECTIONS = config.extensions.modmail.automatic_rejections.value
# pylint: disable=W0603
global ROLES_TO_PING
# dict.fromkeys() to deduplicate the list
ROLES_TO_PING = list(
dict.fromkeys(config.extensions.modmail.roles_to_ping.value)
)
# pylint: disable=W0603
global THREAD_CREATION_MESSAGE
THREAD_CREATION_MESSAGE = (
config.extensions.modmail.thread_creation_message.value
)
# Finally, makes the TS client available from within the Modmail extension class once again
self.prefix = bot.file_config.modmail_config.modmail_prefix
self.bot = bot
async def handle_reboot(self: Self) -> None:
"""Ran when the bot is restarted"""
await Modmail_client.close()
async def preconfig(self: Self) -> None:
"""Fetches modmail threads once ready"""
self.modmail_forum = await self.bot.fetch_channel(MODMAIL_FORUM_ID)
# Populates the currently active threads
for thread in self.modmail_forum.threads:
if thread.name.startswith("[OPEN]"):
# [status, username, date, id]
active_threads[int(thread.name.split(" | ")[3])] = thread.id
@commands.Cog.listener()
async def on_message(self: Self, message: discord.Message) -> None:
"""Processes messages sent in a modmail thread, basically a manual command handler
Args:
message (discord.Message): The sent message
"""
if (
not message.content.startswith(self.prefix)
or not isinstance(message.channel, discord.Thread)
or message.channel.parent_id != self.modmail_forum.id
or message.channel.name.startswith("[CLOSED]")
or message.author.bot
):
return
# Makes sure the person is actually allowed to run modmail commands
config = self.bot.guild_configs[str(message.guild.id)]
try:
await has_modmail_management_role(message, config)
except commands.MissingAnyRole as e:
await auxiliary.send_deny_embed(message=f"{e}", channel=message.channel)
return
# Gets the content without the prefix
content = message.content.partition(self.prefix)[2]
# Checks if the message had a command
match content.split()[0]:
# - Normal closes -
case "close":
await close_thread(
thread=message.channel,
silent=False,
timed=False,
log_channel=self.bot.get_channel(MODMAIL_LOG_CHANNEL_ID),