-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
ModerationCommands.kt
1386 lines (1227 loc) · 42.8 KB
/
ModerationCommands.kt
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
package org.hyacinthbots.lilybot.extensions.moderation
import com.kotlindiscord.kord.extensions.DISCORD_BLACK
import com.kotlindiscord.kord.extensions.DISCORD_GREEN
import com.kotlindiscord.kord.extensions.DISCORD_RED
import com.kotlindiscord.kord.extensions.annotations.DoNotChain
import com.kotlindiscord.kord.extensions.checks.hasPermissions
import com.kotlindiscord.kord.extensions.commands.Arguments
import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand
import com.kotlindiscord.kord.extensions.commands.converters.impl.coalescingDuration
import com.kotlindiscord.kord.extensions.commands.converters.impl.coalescingOptionalDuration
import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingBoolean
import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingString
import com.kotlindiscord.kord.extensions.commands.converters.impl.int
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalAttachment
import com.kotlindiscord.kord.extensions.commands.converters.impl.user
import com.kotlindiscord.kord.extensions.components.components
import com.kotlindiscord.kord.extensions.components.ephemeralStringSelectMenu
import com.kotlindiscord.kord.extensions.extensions.Extension
import com.kotlindiscord.kord.extensions.extensions.ephemeralMessageCommand
import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand
import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.api.PluralKit
import com.kotlindiscord.kord.extensions.pagination.EphemeralResponsePaginator
import com.kotlindiscord.kord.extensions.pagination.pages.Page
import com.kotlindiscord.kord.extensions.pagination.pages.Pages
import com.kotlindiscord.kord.extensions.time.TimestampType
import com.kotlindiscord.kord.extensions.time.toDiscord
import com.kotlindiscord.kord.extensions.utils.dm
import com.kotlindiscord.kord.extensions.utils.isNullOrBot
import com.kotlindiscord.kord.extensions.utils.kordExUserAgent
import com.kotlindiscord.kord.extensions.utils.scheduling.Scheduler
import com.kotlindiscord.kord.extensions.utils.scheduling.Task
import com.kotlindiscord.kord.extensions.utils.timeout
import com.kotlindiscord.kord.extensions.utils.timeoutUntil
import com.kotlindiscord.kord.extensions.utils.toDuration
import dev.kord.common.entity.Permission
import dev.kord.common.entity.Permissions
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.ban
import dev.kord.core.behavior.channel.createEmbed
import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.behavior.edit
import dev.kord.core.behavior.getChannelOfOrNull
import dev.kord.core.behavior.interaction.followup.edit
import dev.kord.core.behavior.reply
import dev.kord.core.entity.Guild
import dev.kord.core.entity.Message
import dev.kord.core.entity.User
import dev.kord.core.entity.channel.GuildMessageChannel
import dev.kord.core.entity.interaction.followup.EphemeralFollowupMessage
import dev.kord.rest.builder.message.EmbedBuilder
import dev.kord.rest.builder.message.embed
import dev.kord.rest.request.KtorRequestException
import kotlinx.datetime.Clock
import kotlinx.datetime.DateTimePeriod
import kotlinx.datetime.TimeZone
import kotlinx.datetime.plus
import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection
import org.hyacinthbots.lilybot.database.collections.TemporaryBanCollection
import org.hyacinthbots.lilybot.database.collections.WarnCollection
import org.hyacinthbots.lilybot.database.entities.TemporaryBanData
import org.hyacinthbots.lilybot.extensions.config.ConfigOptions
import org.hyacinthbots.lilybot.utils.HYACINTH_GITHUB
import org.hyacinthbots.lilybot.utils.baseModerationEmbed
import org.hyacinthbots.lilybot.utils.dmNotificationStatusEmbedField
import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms
import org.hyacinthbots.lilybot.utils.interval
import org.hyacinthbots.lilybot.utils.isBotOrModerator
import org.hyacinthbots.lilybot.utils.modCommandChecks
import kotlin.time.Duration
import kotlin.time.toDuration
class ModerationCommands : Extension() {
override val name = "moderation"
private val warnSuffix = "Please consider your actions carefully.\n\n" +
"For more information about the warn system, please see [this document]" +
"($HYACINTH_GITHUB/LilyBot/blob/main/docs/commands.md#name-warn)"
/** The scheduler that will track the time for un-banning in temp bans. */
private val tempBanScheduler = Scheduler()
/** The task that will run the [tempBanScheduler]. */
private lateinit var tempBanTask: Task
@OptIn(DoNotChain::class)
override suspend fun setup() {
tempBanTask = tempBanScheduler.schedule(120, repeat = true, callback = ::removeTempBans)
ephemeralMessageCommand {
name = "Moderate"
locking = true
requirePermission(Permission.BanMembers, Permission.KickMembers, Permission.ModerateMembers)
check {
hasPermissions(Permissions(Permission.BanMembers, Permission.KickMembers, Permission.ModerateMembers))
requireBotPermissions(Permission.BanMembers, Permission.KickMembers, Permission.ModerateMembers)
}
action {
val messageEvent = event
val loggingChannel = getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!) ?: return@action
var menuMessage: EphemeralFollowupMessage? = null
val targetMessage = messageEvent.interaction.getTargetOrNull()
if (targetMessage == null) {
respond {
content = "The message this command was run on cannot be found! It may have been deleted."
}
return@action
}
val senderId: Snowflake
if (targetMessage.author.isNullOrBot()) {
val proxiedMessage =
PluralKit(userAgent = this@ephemeralMessageCommand.kord.kordExUserAgent()).getMessageOrNull(
targetMessage.id
)
proxiedMessage ?: run {
respond { content = "Unable to find user" }
return@action
}
senderId = proxiedMessage.sender
} else {
senderId = targetMessage.author!!.id
}
val sender = guild!!.getMemberOrNull(senderId)
?: run {
respond { content = "Unable to find user" }
return@action
}
isBotOrModerator(event.kord, sender.asUserOrNull(), guild, "moderate") ?: return@action
menuMessage = respond {
content = "How would you like to moderate this message?"
components {
ephemeralStringSelectMenu {
placeholder = "Select action..."
maximumChoices = 1 // Prevent selecting multiple options at once
option(label = "Ban user", ModerationActions.BAN.name) {
description = "Ban the user that sent this message"
}
option("Soft-ban", ModerationActions.SOFT_BAN.name) {
description = "Soft-ban the user that sent this message"
}
option("Kick", ModerationActions.KICK.name) {
description = "Kick the user that sent this message"
}
option("Timeout", ModerationActions.TIMEOUT.name) {
description = "Timeout the user that sent this message"
}
option("Warn", ModerationActions.WARN.name) {
description = "Warn the user that sent this message"
}
action SelectMenu@{
// Get the first because there can only be one
val option = event.interaction.values.firstOrNull()
if (option == null) {
respond { content = "You did not select an option!" }
return@SelectMenu
}
val reasonSuffix = "for sending the following message: `${targetMessage.content}`"
val modConfig = ModerationConfigCollection().getConfig(guild!!.id)
when (option) {
ModerationActions.BAN.name -> {
val dm = sender.dm {
embed {
title = "You have been banned from ${guild?.asGuildOrNull()?.name}"
description = modConfig?.banDmMessage ?: "Quick banned $reasonSuffix"
color = DISCORD_GREEN
}
}
sender.ban {
reason =
"Quick banned $reasonSuffix"
}
if (modConfig?.publicLogging != null && modConfig.publicLogging == true) {
try {
targetMessage.reply {
embed {
title = "Banned."
description = "${sender.mention} user was banned " +
"for sending this message."
}
}
} catch (e: KtorRequestException) {
channel.createEmbed {
title = "Banned."
description = "${sender.mention} user was banned " +
"for sending a deleted message."
}
}
}
loggingChannel.createEmbed {
title = "Banned a user"
description = "${
sender.mention
} has been banned!"
baseModerationEmbed(
"Quick banned via moderate menu $reasonSuffix",
sender,
user
)
dmNotificationStatusEmbedField(dm, true)
timestamp = Clock.System.now()
}
menuMessage?.edit {
content = "Banned a user."
components { removeAll() }
}
}
ModerationActions.SOFT_BAN.name -> {
val dm = sender.dm {
embed {
title = "You have been soft-banned from ${guild?.asGuildOrNull()?.name}"
description =
"Quick soft-banned $reasonSuffix. This is a soft-ban, you are " +
"free to rejoin at any time"
}
}
sender.ban {
reason =
"Quick soft-banned $reasonSuffix"
}
guild!!.unban(senderId, "Quick soft-ban unban")
if (modConfig?.publicLogging != null && modConfig.publicLogging == true) {
try {
targetMessage.reply {
embed {
title = "Soft-banned."
description = "${sender.mention} user was soft-banned " +
"for sending this message."
}
}
} catch (e: KtorRequestException) {
channel.createEmbed {
title = "Soft-Banned."
description = "${sender.mention} user was soft-banned " +
"for sending a deleted message."
}
}
}
loggingChannel.createEmbed {
title = "Soft-Banned a user"
description = "${
sender.mention
} has been soft-banned!"
baseModerationEmbed(
"Quick soft-banned via moderate menu $reasonSuffix",
sender,
user
)
dmNotificationStatusEmbedField(dm, true)
timestamp = Clock.System.now()
}
menuMessage?.edit {
content = "Soft-Banned a user."
components { removeAll() }
}
}
ModerationActions.KICK.name -> {
val dm = sender.dm {
embed {
title = "You have been kicked from ${guild?.asGuildOrNull()?.name}"
description = "Quick kicked $reasonSuffix."
}
}
guild!!.kick(senderId, "Quick kicked ")
if (modConfig?.publicLogging != null && modConfig.publicLogging == true) {
try {
targetMessage.reply {
embed {
title = "Kicked."
description = "${sender.mention} user was kicked " +
"for sending this message."
}
}
} catch (e: KtorRequestException) {
channel.createEmbed {
title = "Kicked."
description = "${sender.mention} user was kicked " +
"for sending a deleted message."
}
}
}
loggingChannel.createEmbed {
title = "Kicked a user"
description = "${
sender.mention
} has been kicked!"
baseModerationEmbed(
"Quick kicked via moderate menu $reasonSuffix",
sender,
user
)
dmNotificationStatusEmbedField(dm, true)
timestamp = Clock.System.now()
}
menuMessage?.edit {
content = "Kicked a user."
components { removeAll() }
}
}
ModerationActions.TIMEOUT.name -> {
val timeoutTime =
ModerationConfigCollection().getConfig(guild!!.id)?.quickTimeoutLength
if (timeoutTime == null) {
menuMessage?.edit {
content =
"No timeout length has been set in the moderation config, please set a length."
components { removeAll() }
}
return@SelectMenu
}
val dm = sender.dm {
embed {
title = "You have been timed-out in ${guild?.asGuildOrNull()?.name}"
description =
"Quick timed out for ${timeoutTime.interval()} $reasonSuffix."
}
}
sender.timeout(timeoutTime, reason = "Quick timed-out $reasonSuffix")
if (modConfig?.publicLogging != null && modConfig.publicLogging == true) {
try {
targetMessage.reply {
embed {
title = "Timed-out."
description = "${sender.mention} user was timed-out for " +
"${timeoutTime.interval()} for sending this message."
}
}
} catch (e: KtorRequestException) {
channel.createEmbed {
title = "Timed-out."
description = "${sender.mention} user was timed-out for " +
"${timeoutTime.interval()} for sending a deleted message."
}
}
}
loggingChannel.createEmbed {
title = "Timed-out a user"
description = "${
sender.mention
} has be timed-out!"
baseModerationEmbed(
"Quick timed-out via moderate menu $reasonSuffix",
sender,
user
)
dmNotificationStatusEmbedField(dm, true)
field {
name = "Length"
value = modConfig?.quickTimeoutLength.interval()
?: "Failed to load timeout length"
}
timestamp = Clock.System.now()
}
menuMessage?.edit {
content = "Timed-out a user."
components { removeAll() }
}
}
ModerationActions.WARN.name -> {
WarnCollection().setWarn(senderId, guild!!.id, false)
val strikes = WarnCollection().getWarn(senderId, guild!!.id)?.strikes
val dm = sender.dm {
embed {
title = "Warning $strikes in ${guild?.asGuildOrNull()?.name}"
description =
"Quick warned $reasonSuffix\n $warnSuffix"
}
}
if (modConfig?.autoPunishOnWarn == true && strikes!! > 1) {
val duration = when (strikes) {
2 -> "PT3H"
3 -> "PT12H"
else -> "P3D"
}
guild?.getMemberOrNull(senderId)?.edit {
timeoutUntil = Clock.System.now().plus(Duration.parse(duration))
}
}
loggingChannel.createMessage {
embed {
title = "Warning"
baseModerationEmbed(
"Quick warned via moderate menu $reasonSuffix",
sender,
user
)
dmNotificationStatusEmbedField(dm, true)
timestamp = Clock.System.now()
field {
name = "Total strikes"
value = strikes.toString()
}
}
if (modConfig?.autoPunishOnWarn == true && strikes != 1) {
embed {
warnTimeoutLog(
strikes!!,
event.interaction.user.asUserOrNull(),
sender.asUserOrNull(),
"Quick warned via moderate menu $reasonSuffix"
)
}
}
}
menuMessage?.edit {
content = "Warned a user."
components { removeAll() }
}
if (modConfig?.publicLogging == true) {
try {
targetMessage.reply {
embed {
title = "Warned."
description = "${sender.mention} user was warned " +
"for sending this message."
}
}
} catch (e: KtorRequestException) {
channel.createEmbed {
title = "Warned."
description = "${sender.mention} user was warned " +
"for sending a deleted message."
}
}
}
}
}
}
}
}
}
}
}
ephemeralSlashCommand(::BanArgs) {
name = "ban"
description = "Bans a user."
requirePermission(Permission.BanMembers)
check {
modCommandChecks(Permission.BanMembers)
requireBotPermissions(Permission.BanMembers)
}
action {
isBotOrModerator(event.kord, arguments.userArgument, guild, "ban") ?: return@action
// The discord limit for deleting days of messages in a ban is 7, so we should catch invalid inputs.
if (arguments.messages > 7 || arguments.messages < 0) {
respond { content = "Invalid `messages` parameter! This number must be between 0 and 7!" }
return@action
}
val modConfig = ModerationConfigCollection().getConfig(guild!!.id)
var dmStatus: Message? = null
if (arguments.dm) {
dmStatus = arguments.userArgument.dm {
embed {
title = "You have been banned from ${guild?.asGuildOrNull()?.name}"
description = "**Reason:**\n${
if (modConfig?.banDmMessage != null && arguments.reason == "No reason provided") {
modConfig.banDmMessage
} else if (modConfig?.banDmMessage != null && arguments.reason != "No reason provided") {
"${arguments.reason}\n${modConfig.banDmMessage}"
} else {
arguments.reason
}
}\n${if (arguments.softBan) {
"You were soft-banned. You are free to rejoin without the need to be unbanned"
} else {
""
}
}"
}
}
}
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage {
embed {
if (arguments.softBan) {
title = "Soft-Banned a User"
description = "${arguments.userArgument.mention} has been soft-banned!"
} else {
title = "Banned a User"
description = "${arguments.userArgument.mention} has been banned!"
}
baseModerationEmbed(arguments.reason, arguments.userArgument, user)
image = arguments.image?.url
dmNotificationStatusEmbedField(dmStatus, arguments.dm)
timestamp = Clock.System.now()
field {
name = "Days of messages deleted:"
value = if (arguments.softBan && arguments.messages == 0) "3" else arguments.messages.toString()
inline = false
}
}
}
if (modConfig?.publicLogging == true) {
event.interaction.channel.createEmbed {
if (arguments.softBan) {
title = "Soft-Banned a user"
description = "${arguments.userArgument.mention} has been soft-banned!"
} else {
title = "Banned a user"
description = "${arguments.userArgument.mention} has been banned!"
}
color = DISCORD_BLACK
}
}
guild?.ban(arguments.userArgument.id) {
reason = arguments.reason + if (arguments.softBan) " **SOFT-BAN**" else ""
deleteMessageDuration = if (arguments.softBan && arguments.messages == 0) {
DateTimePeriod(days = 3).toDuration(TimeZone.UTC)
} else {
DateTimePeriod(days = arguments.messages).toDuration(TimeZone.UTC)
}
}
if (arguments.softBan) guild?.unban(arguments.userArgument.id, "User was soft-banned")
respond {
content = if (arguments.softBan) { "Soft-banned " } else { "Banned " } + arguments.userArgument.mention
}
}
}
ephemeralSlashCommand {
name = "temp-ban"
description = "The parent command for temporary ban commands"
ephemeralSubCommand(::TempBanArgs) {
name = "add"
description = "Temporarily bans a user"
requirePermission(Permission.BanMembers)
check {
modCommandChecks(Permission.BanMembers)
requireBotPermissions(Permission.BanMembers)
}
action {
isBotOrModerator(event.kord, arguments.userArgument, guild, "temp-ban add")
val now = Clock.System.now()
val duration = now.plus(arguments.duration, TimeZone.UTC)
val modConfig = ModerationConfigCollection().getConfig(guild!!.id)
var dmStatus: Message? = null
if (arguments.dm) {
dmStatus = arguments.userArgument.dm {
embed {
title = "You have been temporarily banned from ${guild?.fetchGuild()?.name}"
description = "**Reason:**\n${arguments.reason}\n\n" +
"You are banned until $duration"
}
}
}
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage {
embed {
title = "Temporarily banned a user"
description =
"${arguments.userArgument.mention} has been temporarily banned!"
image = arguments.image?.url
baseModerationEmbed(arguments.reason, arguments.userArgument, user)
dmNotificationStatusEmbedField(dmStatus, arguments.dm)
field {
name = "Duration:"
value =
duration.toDiscord(TimestampType.Default) + " (${arguments.duration.interval()})"
}
timestamp = now
}
}
if (modConfig?.publicLogging == true) {
event.interaction.channel.createEmbed {
title = "Temp Banned a user"
description = "${arguments.userArgument.mention} has been Temp Banned!"
color = DISCORD_BLACK
}
}
TemporaryBanCollection().setTempBan(
TemporaryBanData(guild!!.id, arguments.userArgument.id, user.id, now, duration)
)
guild?.ban(arguments.userArgument.id) {
reason = arguments.reason
deleteMessageDuration = DateTimePeriod(days = arguments.messages).toDuration(TimeZone.UTC)
}
respond {
content = "Temporarily banned ${arguments.userArgument.mention}"
}
}
}
ephemeralSubCommand {
name = "view-all"
description = "View all temporary bans for this guild"
requirePermission(Permission.BanMembers)
check {
modCommandChecks(Permission.BanMembers)
requireBotPermissions(Permission.BanMembers)
}
action {
val pagesObj = Pages()
val tempBans = TemporaryBanCollection().getTempBansForGuild(guild!!.id)
if (tempBans.isEmpty()) {
pagesObj.addPage(
Page {
description = "There are no temporary bans in this guild."
}
)
} else {
tempBans.chunked(4).forEach { tempBan ->
var content = ""
tempBan.forEach {
content = """
User: ${this@ephemeralSubCommand.kord.getUser(it.bannedUserId)?.username}
Moderator: ${guild?.getMemberOrNull(it.moderatorUserId)?.username}
Start Time: ${it.startTime.toDiscord(TimestampType.ShortDateTime)} (${
it.startTime.toDiscord(TimestampType.RelativeTime)
})
End Time: ${it.endTime.toDiscord(TimestampType.ShortDateTime)} (${
it.endTime.toDiscord(TimestampType.RelativeTime)
})
---
""".trimIndent()
}
pagesObj.addPage(
Page {
title = "Temporary bans for ${guild?.asGuildOrNull()?.name ?: "this guild"}"
description = content
}
)
}
}
val paginator = EphemeralResponsePaginator(
pages = pagesObj,
owner = event.interaction.user,
timeoutSeconds = 300,
interaction = interactionResponse
)
paginator.send()
}
}
}
ephemeralSlashCommand(::UnbanArgs) {
name = "unban"
description = "Unbans a user."
requirePermission(Permission.BanMembers)
check {
modCommandChecks(Permission.BanMembers)
requireBotPermissions(Permission.BanMembers)
}
action {
val tempBan = TemporaryBanCollection().getUserTempBan(this.getGuild()!!.id, arguments.userArgument.id)
if (tempBan == null) {
guild?.unban(arguments.userArgument.id)
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage {
embed {
title = "Unbanned a user"
description = "${arguments.userArgument.mention} has been unbanned!\n${
arguments.userArgument.id
} (${arguments.userArgument.username})"
field {
name = "Reason:"
value = arguments.reason
}
footer {
text = user.asUserOrNull()?.username ?: "Unable to get user username"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
timestamp = Clock.System.now()
color = DISCORD_GREEN
}
}
} else {
guild?.unban(arguments.userArgument.id)
TemporaryBanCollection().removeTempBan(guild!!.id, arguments.userArgument.id)
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage {
embed {
title = "Temporary Ban Removed"
description = "${arguments.userArgument.mention} has had their temporary ban removed!\n${
arguments.userArgument.id
} (${arguments.userArgument.username})"
field {
name = "Reason:"
value = arguments.reason
}
field {
name = "Original Action taker:"
value = guild?.getMemberOrNull(tempBan.moderatorUserId)?.username
?: this@ephemeralSlashCommand.kord.getUser(tempBan.moderatorUserId)?.username
?: "Unable to get username"
}
footer {
text = user.asUserOrNull()?.username ?: "Unable to get user username"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
timestamp = Clock.System.now()
color = DISCORD_GREEN
}
}
}
respond {
content = "Unbanned user."
}
}
}
ephemeralSlashCommand(::KickArgs) {
name = "kick"
description = "Kicks a user."
requirePermission(Permission.KickMembers)
check {
modCommandChecks(Permission.KickMembers)
requireBotPermissions(Permission.KickMembers)
}
action {
isBotOrModerator(event.kord, arguments.userArgument, guild, "kick") ?: return@action
val modConfig = ModerationConfigCollection().getConfig(guild!!.id)
var dmStatus: Message? = null
if (arguments.dm) {
dmStatus = arguments.userArgument.dm {
embed {
title = "You have been kicked from ${guild?.fetchGuild()?.name}"
description = "**Reason:**\n${arguments.reason}"
}
}
}
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage {
embed {
title = "Kicked a user"
description = "${arguments.userArgument.mention} has been kicked!"
image = arguments.image?.url
baseModerationEmbed(arguments.reason, arguments.userArgument, user)
dmNotificationStatusEmbedField(dmStatus, arguments.dm)
timestamp = Clock.System.now()
}
}
if (modConfig?.publicLogging == true) {
event.interaction.channel.createEmbed {
title = "Kicked a user"
description = "${arguments.userArgument.mention} has been kicked!"
color = DISCORD_BLACK
}
}
guild?.kick(arguments.userArgument.id, arguments.reason)
respond {
content = "Kicked ${arguments.userArgument}"
}
}
}
ephemeralSlashCommand(::TimeoutArgs) {
name = "timeout"
description = "Times out a user."
requirePermission(Permission.ModerateMembers)
check {
modCommandChecks(Permission.ModerateMembers)
requireBotPermissions(Permission.ModerateMembers)
}
action {
val modConfig = ModerationConfigCollection().getConfig(guild!!.id)
val durationArg = arguments.duration ?: modConfig?.quickTimeoutLength ?: DateTimePeriod(hours = 6)
val duration = Clock.System.now().plus(durationArg, TimeZone.UTC)
isBotOrModerator(event.kord, arguments.userArgument, guild, "timeout") ?: return@action
var dmStatus: Message? = null
if (arguments.dm) {
dmStatus = arguments.userArgument.dm {
embed {
title = "You have been timed out in ${guild?.fetchGuild()?.name}"
description = "**Duration:**\n${
duration.toDiscord(TimestampType.Default) + " (${durationArg.interval()})"
}\n**Reason:**\n${arguments.reason}"
}
}
}
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage {
embed {
title = "Timeout"
image = arguments.image?.url
baseModerationEmbed(arguments.reason, arguments.userArgument, user)
dmNotificationStatusEmbedField(dmStatus, arguments.dm)
timestamp = Clock.System.now()
field {
name = "Duration:"
value = duration.toDiscord(TimestampType.Default) + " (${durationArg.interval()})"
inline = false
}
}
}
if (modConfig?.publicLogging == true) {
event.interaction.channel.createEmbed {
title = "Timeout"
description = "${arguments.userArgument.mention} was timed out by a moderator"
color = DISCORD_BLACK
field {
name = "Duration:"
value = duration.toDiscord(TimestampType.Default) + " (${durationArg.interval()})"
inline = false
}
}
}
respond {
content = "Timed-out ${arguments.userArgument.mention}."
}
}
}
ephemeralSlashCommand(::RemoveTimeoutArgs) {
name = "remove-timeout"
description = "Removes a timeout from a user"
requirePermission(Permission.ModerateMembers)
check {
modCommandChecks(Permission.ModerateMembers)
requireBotPermissions(Permission.ModerateMembers)
}
action {
var dmStatus: Message? = null
if (arguments.dm) {
dmStatus = arguments.userArgument.dm {
embed {
title = "Timeout removed in ${guild!!.asGuildOrNull()?.name}"
description = "Your timeout has been manually removed in this guild."
}
}
}
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage {
embed {
title = "Timeout Removed"
dmNotificationStatusEmbedField(dmStatus, arguments.dm)
field {
name = "User:"
value = "${arguments.userArgument.username} \n${arguments.userArgument.id}"
inline = false
}
footer {
text = "Requested by ${user.asUserOrNull()?.username}"
icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl()
}
timestamp = Clock.System.now()
color = DISCORD_BLACK
}
}
respond {
content = "Removed timeout from user."
}
}
}
ephemeralSlashCommand(::WarnArgs) {
name = "warn"
description = "Warns a user."
requirePermission(Permission.ModerateMembers)
check {
modCommandChecks(Permission.ModerateMembers)
requireBotPermissions(Permission.ModerateMembers)
}
action {
val config = ModerationConfigCollection().getConfig(guild!!.id)!!
val actionLog =
getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, this.getGuild()!!) ?: return@action
val guildName = guild?.asGuildOrNull()?.name
isBotOrModerator(event.kord, arguments.userArgument, guild, "warn") ?: return@action
WarnCollection().setWarn(arguments.userArgument.id, guild!!.id, false)
val strikes = WarnCollection().getWarn(arguments.userArgument.id, guild!!.id)?.strikes
respond {
content = "Warned user."
}
var dmStatus: Message? = null
if (arguments.dm) {
val warnText = if (config.autoPunishOnWarn == false) {
"No moderation action has been taken.\n $warnSuffix"
} else {
when (strikes) {
1 -> "No moderation action has been taken.\n $warnSuffix"
2 -> "You have been timed out for 3 hours.\n $warnSuffix"
3 -> "You have been timed out for 12 hours.\n $warnSuffix"
else -> "You have been timed out for 3 days.\n $warnSuffix"
}
}
dmStatus = arguments.userArgument.dm {
embed {
title = "Warning $strikes in $guildName"
description = "**Reason:** ${arguments.reason}\n\n$warnText"
}
}
}
if (config.autoPunishOnWarn == true && strikes!! > 1) {
val duration = when (strikes) {
2 -> "PT3H"
3 -> "PT12H"
else -> "P3D"
}
guild?.getMemberOrNull(arguments.userArgument.id)?.edit {
timeoutUntil = Clock.System.now().plus(Duration.parse(duration))
}
}
actionLog.createMessage {
embed {
title = "Warning"
image = arguments.image?.url
baseModerationEmbed(arguments.reason, arguments.userArgument, user)
dmNotificationStatusEmbedField(dmStatus, arguments.dm)
timestamp = Clock.System.now()
field {
name = "Total strikes"
value = strikes.toString()
}
color = DISCORD_RED
}
if (config.autoPunishOnWarn == true && strikes != 1) {
embed {
warnTimeoutLog(
strikes!!,
event.interaction.user.asUserOrNull(),
arguments.userArgument,
arguments.reason
)