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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
|
/* eslint-disable @typescript-eslint/no-unused-vars */
import {
BushClientEvents,
Moderation,
ModLogType,
type BushClient,
type BushGuild,
type BushGuildTextBasedChannel,
type BushGuildTextChannelResolvable,
type BushRole,
type BushThreadChannelResolvable,
type BushUser
} from '#lib';
import { GuildMember, type Partialize, type Role } from 'discord.js';
import type { RawGuildMemberData } from 'discord.js/typings/rawDataTypes';
/* eslint-enable @typescript-eslint/no-unused-vars */
/**
* Represents a member of a guild on Discord.
*/
export class BushGuildMember extends GuildMember {
public declare readonly client: BushClient;
public declare guild: BushGuild;
public declare user: BushUser;
public constructor(client: BushClient, data: RawGuildMemberData, guild: BushGuild) {
super(client, data, guild);
}
/**
* Send a punishment dm to the user.
* @param punishment The punishment that the user has received.
* @param reason The reason for the user's punishment.
* @param duration The duration of the punishment.
* @param sendFooter Whether or not to send the guild's punishment footer with the dm.
* @returns Whether or not the dm was sent successfully.
*/
public async bushPunishDM(punishment: string, reason?: string | null, duration?: number, sendFooter = true): Promise<boolean> {
return Moderation.punishDM({ guild: this.guild, user: this, punishment, reason: reason ?? undefined, duration, sendFooter });
}
/**
* Warn the user, create a modlog entry, and send a dm to the user.
* @param options Options for warning the user.
* @returns An object with the result of the warning, and the case number of the warn.
* @emits {@link BushClientEvents.bushWarn}
*/
public async bushWarn(options: BushPunishmentOptions): Promise<{ result: WarnResponse; caseNum: number | null }> {
let caseID: string | undefined = undefined;
let dmSuccessEvent: boolean | undefined = undefined;
const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!;
const ret = await (async (): Promise<{ result: WarnResponse; caseNum: number | null }> => {
// add modlog entry
const result = await Moderation.createModLogEntry(
{
type: ModLogType.WARN,
user: this,
moderator: moderator.id,
reason: options.reason,
guild: this.guild,
evidence: options.evidence,
hidden: options.silent ?? false
},
true
);
caseID = result.log?.id;
if (!result || !result.log) return { result: warnResponse.MODLOG_ERROR, caseNum: null };
if (!options.silent) {
// dm user
const dmSuccess = await this.bushPunishDM('warned', options.reason);
dmSuccessEvent = dmSuccess;
if (!dmSuccess) return { result: warnResponse.DM_ERROR, caseNum: result.caseNum };
}
return { result: warnResponse.SUCCESS, caseNum: result.caseNum };
})();
if (!([warnResponse.MODLOG_ERROR] as const).includes(ret.result) && !options.silent)
client.emit('bushWarn', this, moderator, this.guild, options.reason ?? undefined, caseID!, dmSuccessEvent!);
return ret;
}
/**
* Add a role to the user, if it is a punishment create a modlog entry, and create a punishment entry if it is temporary or a punishment.
* @param options Options for adding a role to the user.
* @returns A status message for adding the add.
* @emits {@link BushClientEvents.bushPunishRole}
*/
public async bushAddRole(options: AddRoleOptions): Promise<AddRoleResponse> {
// checks
if (!this.guild.me!.permissions.has('MANAGE_ROLES')) return addRoleResponse.MISSING_PERMISSIONS;
const ifShouldAddRole = this.#checkIfShouldAddRole(options.role, options.moderator);
if (ifShouldAddRole !== true) return ifShouldAddRole;
let caseID: string | undefined = undefined;
const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!;
const ret = await (async () => {
if (options.addToModlog || options.duration) {
const { log: modlog } = await Moderation.createModLogEntry({
type: options.duration ? ModLogType.TEMP_PUNISHMENT_ROLE : ModLogType.PERM_PUNISHMENT_ROLE,
guild: this.guild,
moderator: moderator.id,
user: this,
reason: 'N/A',
pseudo: !options.addToModlog,
evidence: options.evidence,
hidden: options.silent ?? false
});
if (!modlog) return addRoleResponse.MODLOG_ERROR;
caseID = modlog.id;
if (options.addToModlog || options.duration) {
const punishmentEntrySuccess = await Moderation.createPunishmentEntry({
type: 'role',
user: this,
guild: this.guild,
modlog: modlog.id,
duration: options.duration,
extraInfo: options.role.id
});
if (!punishmentEntrySuccess) return addRoleResponse.PUNISHMENT_ENTRY_ADD_ERROR;
}
}
const removeRoleSuccess = await this.roles.add(options.role, `${moderator.tag}`);
if (!removeRoleSuccess) return addRoleResponse.ACTION_ERROR;
return addRoleResponse.SUCCESS;
})();
if (
!(
[addRoleResponse.ACTION_ERROR, addRoleResponse.MODLOG_ERROR, addRoleResponse.PUNISHMENT_ENTRY_ADD_ERROR] as const
).includes(ret) &&
options.addToModlog &&
!options.silent
)
client.emit(
'bushPunishRole',
this,
moderator,
this.guild,
options.reason ?? undefined,
caseID!,
options.duration ?? 0,
options.role as BushRole,
options.evidence
);
return ret;
}
/**
* Remove a role from the user, if it is a punishment create a modlog entry, and destroy a punishment entry if it was temporary or a punishment.
* @param options Options for removing a role from the user.
* @returns A status message for removing the role.
* @emits {@link BushClientEvents.bushPunishRoleRemove}
*/
public async bushRemoveRole(options: RemoveRoleOptions): Promise<RemoveRoleResponse> {
// checks
if (!this.guild.me!.permissions.has('MANAGE_ROLES')) return removeRoleResponse.MISSING_PERMISSIONS;
const ifShouldAddRole = this.#checkIfShouldAddRole(options.role, options.moderator);
if (ifShouldAddRole !== true) return ifShouldAddRole;
let caseID: string | undefined = undefined;
const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!;
const ret = await (async () => {
if (options.addToModlog) {
const { log: modlog } = await Moderation.createModLogEntry({
type: ModLogType.REMOVE_PUNISHMENT_ROLE,
guild: this.guild,
moderator: moderator.id,
user: this,
reason: 'N/A',
evidence: options.evidence,
hidden: options.silent ?? false
});
if (!modlog) return removeRoleResponse.MODLOG_ERROR;
caseID = modlog.id;
const punishmentEntrySuccess = await Moderation.removePunishmentEntry({
type: 'role',
user: this,
guild: this.guild,
extraInfo: options.role.id
});
if (!punishmentEntrySuccess) return removeRoleResponse.PUNISHMENT_ENTRY_REMOVE_ERROR;
}
const removeRoleSuccess = await this.roles.remove(options.role, `${moderator.tag}`);
if (!removeRoleSuccess) return removeRoleResponse.ACTION_ERROR;
return removeRoleResponse.SUCCESS;
})();
if (
!(
[
removeRoleResponse.ACTION_ERROR,
removeRoleResponse.MODLOG_ERROR,
removeRoleResponse.PUNISHMENT_ENTRY_REMOVE_ERROR
] as const
).includes(ret) &&
options.addToModlog &&
!options.silent
)
client.emit(
'bushPunishRoleRemove',
this,
moderator,
this.guild,
options.reason ?? undefined,
caseID!,
options.role as BushRole,
options.evidence
);
return ret;
}
/**
* Check whether or not a role should be added/removed from the user based on hierarchy.
* @param role The role to check if can be modified.
* @param moderator The moderator that is trying to add/remove the role.
* @returns `true` if the role should be added/removed or a string for the reason why it shouldn't.
*/
#checkIfShouldAddRole(
role: BushRole | Role,
moderator?: BushGuildMember
): true | 'user hierarchy' | 'role managed' | 'client hierarchy' {
if (moderator && moderator.roles.highest.position <= role.position && this.guild.ownerId !== this.user.id) {
return shouldAddRoleResponse.USER_HIERARCHY;
} else if (role.managed) {
return shouldAddRoleResponse.ROLE_MANAGED;
} else if (this.guild.me!.roles.highest.position <= role.position) {
return shouldAddRoleResponse.CLIENT_HIERARCHY;
}
return true;
}
/**
* Mute the user, create a modlog entry, creates a punishment entry, and dms the user.
* @param options Options for muting the user.
* @returns A status message for muting the user.
* @emits {@link BushClientEvents.bushMute}
*/
public async bushMute(options: BushTimedPunishmentOptions): Promise<MuteResponse> {
// checks
if (!this.guild.me!.permissions.has('MANAGE_ROLES')) return muteResponse.MISSING_PERMISSIONS;
const muteRoleID = await this.guild.getSetting('muteRole');
if (!muteRoleID) return muteResponse.NO_MUTE_ROLE;
const muteRole = this.guild.roles.cache.get(muteRoleID);
if (!muteRole) return muteResponse.MUTE_ROLE_INVALID;
if (muteRole.position >= this.guild.me!.roles.highest.position || muteRole.managed)
return muteResponse.MUTE_ROLE_NOT_MANAGEABLE;
let caseID: string | undefined = undefined;
let dmSuccessEvent: boolean | undefined = undefined;
const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!;
const ret = await (async () => {
// add role
const muteSuccess = await this.roles
.add(muteRole, `[Mute] ${moderator.tag} | ${options.reason ?? 'No reason provided.'}`)
.catch(async (e) => {
await client.console.warn('muteRoleAddError', e);
client.console.debug(e);
return false;
});
if (!muteSuccess) return muteResponse.ACTION_ERROR;
// add modlog entry
const { log: modlog } = await Moderation.createModLogEntry({
type: options.duration ? ModLogType.TEMP_MUTE : ModLogType.PERM_MUTE,
user: this,
moderator: moderator.id,
reason: options.reason,
duration: options.duration,
guild: this.guild,
evidence: options.evidence,
hidden: options.silent ?? false
});
if (!modlog) return muteResponse.MODLOG_ERROR;
caseID = modlog.id;
// add punishment entry so they can be unmuted later
const punishmentEntrySuccess = await Moderation.createPunishmentEntry({
type: 'mute',
user: this,
guild: this.guild,
duration: options.duration,
modlog: modlog.id
});
if (!punishmentEntrySuccess) return muteResponse.PUNISHMENT_ENTRY_ADD_ERROR;
if (!options.silent) {
// dm user
const dmSuccess = await this.bushPunishDM('muted', options.reason, options.duration ?? 0);
dmSuccessEvent = dmSuccess;
if (!dmSuccess) return muteResponse.DM_ERROR;
}
return muteResponse.SUCCESS;
})();
if (
!([muteResponse.ACTION_ERROR, muteResponse.MODLOG_ERROR, muteResponse.PUNISHMENT_ENTRY_ADD_ERROR] as const).includes(ret) &&
!options.silent
)
client.emit(
'bushMute',
this,
moderator,
this.guild,
options.reason ?? undefined,
caseID!,
options.duration ?? 0,
dmSuccessEvent!,
options.evidence
);
return ret;
}
/**
* Unmute the user, create a modlog entry, remove the punishment entry, and dm the user.
* @param options Options for unmuting the user.
* @returns A status message for unmuting the user.
* @emits {@link BushClientEvents.bushUnmute}
*/
public async bushUnmute(options: BushPunishmentOptions): Promise<UnmuteResponse> {
// checks
if (!this.guild.me!.permissions.has('MANAGE_ROLES')) return unmuteResponse.MISSING_PERMISSIONS;
const muteRoleID = await this.guild.getSetting('muteRole');
if (!muteRoleID) return unmuteResponse.NO_MUTE_ROLE;
const muteRole = this.guild.roles.cache.get(muteRoleID);
if (!muteRole) return unmuteResponse.MUTE_ROLE_INVALID;
if (muteRole.position >= this.guild.me!.roles.highest.position || muteRole.managed)
return unmuteResponse.MUTE_ROLE_NOT_MANAGEABLE;
let caseID: string | undefined = undefined;
let dmSuccessEvent: boolean | undefined = undefined;
const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!;
const ret = await (async () => {
// remove role
const muteSuccess = await this.roles
.remove(muteRole, `[Unmute] ${moderator.tag} | ${options.reason ?? 'No reason provided.'}`)
.catch(async (e) => {
await client.console.warn('muteRoleAddError', e?.stack || e);
return false;
});
if (!muteSuccess) return unmuteResponse.ACTION_ERROR;
// add modlog entry
const { log: modlog } = await Moderation.createModLogEntry({
type: ModLogType.UNMUTE,
user: this,
moderator: moderator.id,
reason: options.reason,
guild: this.guild,
evidence: options.evidence,
hidden: options.silent ?? false
});
if (!modlog) return unmuteResponse.MODLOG_ERROR;
caseID = modlog.id;
// remove mute entry
const removePunishmentEntrySuccess = await Moderation.removePunishmentEntry({
type: 'mute',
user: this,
guild: this.guild
});
if (!removePunishmentEntrySuccess) return unmuteResponse.PUNISHMENT_ENTRY_REMOVE_ERROR;
if (!options.silent) {
// dm user
const dmSuccess = await this.bushPunishDM('unmuted', options.reason, undefined, false);
dmSuccessEvent = dmSuccess;
if (!dmSuccess) return unmuteResponse.DM_ERROR;
}
return unmuteResponse.SUCCESS;
})();
if (
!(
[unmuteResponse.ACTION_ERROR, unmuteResponse.MODLOG_ERROR, unmuteResponse.PUNISHMENT_ENTRY_REMOVE_ERROR] as const
).includes(ret) &&
!options.silent
)
client.emit(
'bushUnmute',
this,
moderator,
this.guild,
options.reason ?? undefined,
caseID!,
dmSuccessEvent!,
options.evidence
);
return ret;
}
/**
* Kick the user, create a modlog entry, and dm the user.
* @param options Options for kicking the user.
* @returns A status message for kicking the user.
* @emits {@link BushClientEvents.bushKick}
*/
public async bushKick(options: BushPunishmentOptions): Promise<KickResponse> {
// checks
if (!this.guild.me?.permissions.has('KICK_MEMBERS') || !this.kickable) return kickResponse.MISSING_PERMISSIONS;
let caseID: string | undefined = undefined;
let dmSuccessEvent: boolean | undefined = undefined;
const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!;
const ret = await (async () => {
// dm user
const dmSuccess = options.silent ? null : await this.bushPunishDM('kicked', options.reason);
dmSuccessEvent = dmSuccess ?? undefined;
// kick
const kickSuccess = await this.kick(`${moderator?.tag} | ${options.reason ?? 'No reason provided.'}`).catch(() => false);
if (!kickSuccess) return kickResponse.ACTION_ERROR;
// add modlog entry
const { log: modlog } = await Moderation.createModLogEntry({
type: ModLogType.KICK,
user: this,
moderator: moderator.id,
reason: options.reason,
guild: this.guild,
evidence: options.evidence,
hidden: options.silent ?? false
});
if (!modlog) return kickResponse.MODLOG_ERROR;
caseID = modlog.id;
if (dmSuccess === false) return kickResponse.DM_ERROR;
return kickResponse.SUCCESS;
})();
if (!([kickResponse.ACTION_ERROR, kickResponse.MODLOG_ERROR] as const).includes(ret) && !options.silent)
client.emit(
'bushKick',
this,
moderator,
this.guild,
options.reason ?? undefined,
caseID!,
dmSuccessEvent!,
options.evidence
);
return ret;
}
/**
* Ban the user, create a modlog entry, create a punishment entry, and dm the user.
* @param options Options for banning the user.
* @returns A status message for banning the user.
* @emits {@link BushClientEvents.bushBan}
*/
public async bushBan(options: BushBanOptions): Promise<BanResponse> {
// checks
if (!this.guild.me!.permissions.has('BAN_MEMBERS') || !this.bannable) return banResponse.MISSING_PERMISSIONS;
let caseID: string | undefined = undefined;
let dmSuccessEvent: boolean | undefined = undefined;
const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!;
// ignore result, they should still be banned even if their mute cannot be removed
await this.bushUnmute({
reason: 'User is about to be banned, a mute is no longer necessary.',
moderator: this.guild.me!,
silent: true
});
const ret = await (async () => {
// dm user
const dmSuccess = options.silent ? null : await this.bushPunishDM('banned', options.reason, options.duration ?? 0);
dmSuccessEvent = dmSuccess ?? undefined;
// ban
const banSuccess = await this.ban({
reason: `${moderator.tag} | ${options.reason ?? 'No reason provided.'}`,
days: options.deleteDays
}).catch(() => false);
if (!banSuccess) return banResponse.ACTION_ERROR;
// add modlog entry
const { log: modlog } = await Moderation.createModLogEntry({
type: options.duration ? ModLogType.TEMP_BAN : ModLogType.PERM_BAN,
user: this,
moderator: moderator.id,
reason: options.reason,
duration: options.duration,
guild: this.guild,
evidence: options.evidence,
hidden: options.silent ?? false
});
if (!modlog) return banResponse.MODLOG_ERROR;
caseID = modlog.id;
// add punishment entry so they can be unbanned later
const punishmentEntrySuccess = await Moderation.createPunishmentEntry({
type: 'ban',
user: this,
guild: this.guild,
duration: options.duration,
modlog: modlog.id
});
if (!punishmentEntrySuccess) return banResponse.PUNISHMENT_ENTRY_ADD_ERROR;
if (!dmSuccess) return banResponse.DM_ERROR;
return banResponse.SUCCESS;
})();
if (
!([banResponse.ACTION_ERROR, banResponse.MODLOG_ERROR, banResponse.PUNISHMENT_ENTRY_ADD_ERROR] as const).includes(ret) &&
!options.silent
)
client.emit(
'bushBan',
this,
moderator,
this.guild,
options.reason ?? undefined,
caseID!,
options.duration ?? 0,
dmSuccessEvent!,
options.evidence
);
return ret;
}
/**
* Prevents a user from speaking in a channel.
* @param options Options for blocking the user.
*/
public async bushBlock(options: BlockOptions): Promise<BlockResponse> {
const _channel = this.guild.channels.resolve(options.channel);
if (!_channel || (!_channel.isText() && !_channel.isThread())) return blockResponse.INVALID_CHANNEL;
const channel = _channel as BushGuildTextBasedChannel;
// checks
if (!channel.permissionsFor(this.guild.me!)!.has('MANAGE_CHANNELS')) return blockResponse.MISSING_PERMISSIONS;
let caseID: string | undefined = undefined;
let dmSuccessEvent: boolean | undefined = undefined;
const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!;
const ret = await (async () => {
// change channel permissions
const channelToUse = channel.isThread() ? channel.parent! : channel;
const perm = channel.isThread() ? { SEND_MESSAGES_IN_THREADS: false } : { SEND_MESSAGES: false };
const blockSuccess = await channelToUse.permissionOverwrites
.edit(this, perm, { reason: `[Block] ${moderator.tag} | ${options.reason ?? 'No reason provided.'}` })
.catch(() => false);
if (!blockSuccess) return blockResponse.ACTION_ERROR;
// add modlog entry
const { log: modlog } = await Moderation.createModLogEntry({
type: options.duration ? ModLogType.TEMP_CHANNEL_BLOCK : ModLogType.PERM_CHANNEL_BLOCK,
user: this,
moderator: moderator.id,
reason: options.reason,
guild: this.guild,
evidence: options.evidence,
hidden: options.silent ?? false
});
if (!modlog) return blockResponse.MODLOG_ERROR;
caseID = modlog.id;
// add punishment entry so they can be unblocked later
const punishmentEntrySuccess = await Moderation.createPunishmentEntry({
type: 'block',
user: this,
guild: this.guild,
duration: options.duration,
modlog: modlog.id,
extraInfo: channel.id
});
if (!punishmentEntrySuccess) return blockResponse.PUNISHMENT_ENTRY_ADD_ERROR;
if (!options.silent) {
// dm user
const dmSuccess = await this.send({
content: `You have been blocked from <#${channel.id}> in **${this.guild.name}** ${
options.duration !== null && options.duration !== undefined
? options.duration
? `for ${util.humanizeDuration(options.duration)} `
: 'permanently '
: ''
}for **${options.reason?.trim() ? options.reason?.trim() : 'No reason provided'}**.`
}).catch(() => false);
dmSuccessEvent = !!dmSuccess;
if (!dmSuccess) return blockResponse.DM_ERROR;
}
return blockResponse.SUCCESS;
})();
if (
!([blockResponse.ACTION_ERROR, blockResponse.MODLOG_ERROR, blockResponse.PUNISHMENT_ENTRY_ADD_ERROR] as const).includes(
ret
) &&
!options.silent
)
client.emit(
'bushBlock',
this,
moderator,
this.guild,
options.reason ?? undefined,
caseID!,
options.duration ?? 0,
dmSuccessEvent!,
channel,
options.evidence
);
return ret;
}
/**
* Allows a user to speak in a channel.
* @param options Options for unblocking the user.
*/
public async bushUnblock(options: UnblockOptions): Promise<UnblockResponse> {
const _channel = this.guild.channels.resolve(options.channel);
if (!_channel || (!_channel.isText() && !_channel.isThread())) return unblockResponse.INVALID_CHANNEL;
const channel = _channel as BushGuildTextBasedChannel;
// checks
if (!channel.permissionsFor(this.guild.me!)!.has('MANAGE_CHANNELS')) return unblockResponse.MISSING_PERMISSIONS;
let caseID: string | undefined = undefined;
let dmSuccessEvent: boolean | undefined = undefined;
const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!;
const ret = await (async () => {
// change channel permissions
const channelToUse = channel.isThread() ? channel.parent! : channel;
const perm = channel.isThread() ? { SEND_MESSAGES_IN_THREADS: null } : { SEND_MESSAGES: null };
const blockSuccess = await channelToUse.permissionOverwrites
.edit(this, perm, { reason: `[Unblock] ${moderator.tag} | ${options.reason ?? 'No reason provided.'}` })
.catch(() => false);
if (!blockSuccess) return unblockResponse.ACTION_ERROR;
// add modlog entry
const { log: modlog } = await Moderation.createModLogEntry({
type: ModLogType.CHANNEL_UNBLOCK,
user: this,
moderator: moderator.id,
reason: options.reason,
guild: this.guild,
evidence: options.evidence,
hidden: options.silent ?? false
});
if (!modlog) return unblockResponse.MODLOG_ERROR;
caseID = modlog.id;
// remove punishment entry
const punishmentEntrySuccess = await Moderation.removePunishmentEntry({
type: 'block',
user: this,
guild: this.guild,
extraInfo: channel.id
});
if (!punishmentEntrySuccess) return unblockResponse.ACTION_ERROR;
if (!options.silent) {
// dm user
const dmSuccess = await this.send({
content: `You have been unblocked from <#${channel.id}> in **${this.guild.name}** for **${
options.reason?.trim() ? options.reason?.trim() : 'No reason provided'
}**.`
}).catch(() => false);
dmSuccessEvent = !!dmSuccess;
if (!dmSuccess) return unblockResponse.DM_ERROR;
}
return unblockResponse.SUCCESS;
})();
if (
!([unblockResponse.ACTION_ERROR, unblockResponse.MODLOG_ERROR, unblockResponse.ACTION_ERROR] as const).includes(ret) &&
!options.silent
)
client.emit(
'bushUnblock',
this,
moderator,
this.guild,
options.reason ?? undefined,
caseID!,
dmSuccessEvent!,
channel,
options.evidence
);
return ret;
}
/**
* Mutes a user using discord's timeout feature.
* @param options Options for timing out the user.
*/
public async bushTimeout(options: BushTimeoutOptions): Promise<TimeoutResponse> {
// checks
if (!this.guild.me!.permissions.has('MODERATE_MEMBERS')) return timeoutResponse.MISSING_PERMISSIONS;
const twentyEightDays = client.consts.timeUnits.days.value * 28;
if (options.duration > twentyEightDays) return timeoutResponse.INVALID_DURATION;
let caseID: string | undefined = undefined;
let dmSuccessEvent: boolean | undefined = undefined;
const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!;
const ret = await (async () => {
// timeout
const timeoutSuccess = await this.timeout(
options.duration,
`${moderator.tag} | ${options.reason ?? 'No reason provided.'}`
).catch(() => false);
if (!timeoutSuccess) return timeoutResponse.ACTION_ERROR;
// add modlog entry
const { log: modlog } = await Moderation.createModLogEntry({
type: ModLogType.TIMEOUT,
user: this,
moderator: moderator.id,
reason: options.reason,
duration: options.duration,
guild: this.guild,
evidence: options.evidence,
hidden: options.silent ?? false
});
if (!modlog) return timeoutResponse.MODLOG_ERROR;
caseID = modlog.id;
if (!options.silent) {
// dm user
const dmSuccess = await this.bushPunishDM('timed out', options.reason, options.duration);
dmSuccessEvent = dmSuccess;
if (!dmSuccess) return timeoutResponse.DM_ERROR;
}
return timeoutResponse.SUCCESS;
})();
if (!([timeoutResponse.ACTION_ERROR, timeoutResponse.MODLOG_ERROR] as const).includes(ret) && !options.silent)
client.emit(
'bushTimeout',
this,
moderator,
this.guild,
options.reason ?? undefined,
caseID!,
options.duration ?? 0,
dmSuccessEvent!,
options.evidence
);
return ret;
}
/**
* Removes a timeout from a user.
* @param options Options for removing the timeout.
*/
public async bushRemoveTimeout(options: BushPunishmentOptions): Promise<RemoveTimeoutResponse> {
// checks
if (!this.guild.me!.permissions.has('MODERATE_MEMBERS')) return removeTimeoutResponse.MISSING_PERMISSIONS;
let caseID: string | undefined = undefined;
let dmSuccessEvent: boolean | undefined = undefined;
const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!;
const ret = await (async () => {
// remove timeout
const timeoutSuccess = await this.timeout(null, `${moderator.tag} | ${options.reason ?? 'No reason provided.'}`).catch(
() => false
);
if (!timeoutSuccess) return removeTimeoutResponse.ACTION_ERROR;
// add modlog entry
const { log: modlog } = await Moderation.createModLogEntry({
type: ModLogType.REMOVE_TIMEOUT,
user: this,
moderator: moderator.id,
reason: options.reason,
guild: this.guild,
evidence: options.evidence,
hidden: options.silent ?? false
});
if (!modlog) return removeTimeoutResponse.MODLOG_ERROR;
caseID = modlog.id;
if (!options.silent) {
// dm user
const dmSuccess = await this.bushPunishDM('untimedout', options.reason);
dmSuccessEvent = dmSuccess;
if (!dmSuccess) return removeTimeoutResponse.DM_ERROR;
}
return removeTimeoutResponse.SUCCESS;
})();
if (!([removeTimeoutResponse.ACTION_ERROR, removeTimeoutResponse.MODLOG_ERROR] as const).includes(ret) && !options.silent)
client.emit(
'bushRemoveTimeout',
this,
moderator,
this.guild,
options.reason ?? undefined,
caseID!,
dmSuccessEvent!,
options.evidence
);
return ret;
}
/**
* Whether or not the user is an owner of the bot.
*/
public isOwner(): boolean {
return client.isOwner(this);
}
/**
* Whether or not the user is a super user of the bot.
*/
public isSuperUser(): boolean {
return client.isSuperUser(this);
}
}
/**
* Options for punishing a user.
*/
export interface BushPunishmentOptions {
/**
* The reason for the punishment.
*/
reason?: string | null;
/**
* The moderator who punished the user.
*/
moderator?: BushGuildMember;
/**
* Evidence for the punishment.
*/
evidence?: string;
/**
* Makes the punishment silent by not sending the user a punishment dm and not broadcasting the event to be logged.
*/
silent?: boolean;
}
/**
* Punishment options for punishments that can be temporary.
*/
export interface BushTimedPunishmentOptions extends BushPunishmentOptions {
/**
* The duration of the punishment.
*/
duration?: number;
}
/**
* Options for a role add punishment.
*/
export interface AddRoleOptions extends BushTimedPunishmentOptions {
/**
* The role to add to the user.
*/
role: BushRole | Role;
/**
* Whether to create a modlog entry for this punishment.
*/
addToModlog: boolean;
}
/**
* Options for a role remove punishment.
*/
export interface RemoveRoleOptions extends BushTimedPunishmentOptions {
/**
* The role to remove from the user.
*/
role: BushRole | Role;
/**
* Whether to create a modlog entry for this punishment.
*/
addToModlog: boolean;
}
/**
* Options for banning a user.
*/
export interface BushBanOptions extends BushTimedPunishmentOptions {
/**
* The number of days to delete the user's messages for.
*/
deleteDays?: number;
}
/**
* Options for blocking a user from a channel.
*/
export interface BlockOptions extends BushTimedPunishmentOptions {
/**
* The channel to block the user from.
*/
channel: BushGuildTextChannelResolvable | BushThreadChannelResolvable;
}
/**
* Options for unblocking a user from a channel.
*/
export interface UnblockOptions extends BushPunishmentOptions {
/**
* The channel to unblock the user from.
*/
channel: BushGuildTextChannelResolvable | BushThreadChannelResolvable;
}
/**
* Punishment options for punishments that can be temporary.
*/
export interface BushTimeoutOptions extends BushPunishmentOptions {
/**
* The duration of the punishment.
*/
duration: number;
}
type ValueOf<T> = T[keyof T];
export const basePunishmentResponse = Object.freeze({
SUCCESS: 'success',
MODLOG_ERROR: 'error creating modlog entry',
ACTION_ERROR: 'error performing action'
} as const);
export const dmResponse = Object.freeze({
...basePunishmentResponse,
DM_ERROR: 'failed to dm'
} as const);
export const permissionsResponse = Object.freeze({
MISSING_PERMISSIONS: 'missing permissions'
} as const);
export const punishmentEntryAdd = Object.freeze({
PUNISHMENT_ENTRY_ADD_ERROR: 'error creating punishment entry'
} as const);
export const punishmentEntryRemove = Object.freeze({
PUNISHMENT_ENTRY_REMOVE_ERROR: 'error removing punishment entry'
} as const);
export const shouldAddRoleResponse = Object.freeze({
USER_HIERARCHY: 'user hierarchy',
CLIENT_HIERARCHY: 'client hierarchy',
ROLE_MANAGED: 'role managed'
} as const);
export const baseBlockResponse = Object.freeze({
INVALID_CHANNEL: 'invalid channel'
} as const);
export const baseMuteResponse = Object.freeze({
NO_MUTE_ROLE: 'no mute role',
MUTE_ROLE_INVALID: 'invalid mute role',
MUTE_ROLE_NOT_MANAGEABLE: 'mute role not manageable'
} as const);
export const warnResponse = Object.freeze({
...dmResponse
} as const);
export const addRoleResponse = Object.freeze({
...basePunishmentResponse,
...permissionsResponse,
...shouldAddRoleResponse,
...punishmentEntryAdd
} as const);
export const removeRoleResponse = Object.freeze({
...basePunishmentResponse,
...permissionsResponse,
...shouldAddRoleResponse,
...punishmentEntryRemove
} as const);
export const muteResponse = Object.freeze({
...dmResponse,
...permissionsResponse,
...baseMuteResponse,
...punishmentEntryAdd
} as const);
export const unmuteResponse = Object.freeze({
...dmResponse,
...permissionsResponse,
...baseMuteResponse,
...punishmentEntryRemove
} as const);
export const kickResponse = Object.freeze({
...dmResponse,
...permissionsResponse
} as const);
export const banResponse = Object.freeze({
...dmResponse,
...permissionsResponse,
...punishmentEntryAdd
} as const);
export const blockResponse = Object.freeze({
...dmResponse,
...permissionsResponse,
...baseBlockResponse,
...punishmentEntryAdd
});
export const unblockResponse = Object.freeze({
...dmResponse,
...permissionsResponse,
...baseBlockResponse,
...punishmentEntryRemove
});
export const timeoutResponse = Object.freeze({
...dmResponse,
...permissionsResponse,
INVALID_DURATION: 'duration too long'
} as const);
export const removeTimeoutResponse = Object.freeze({
...dmResponse,
...permissionsResponse
} as const);
/**
* Response returned when warning a user.
*/
export type WarnResponse = ValueOf<typeof warnResponse>;
/**
* Response returned when adding a role to a user.
*/
export type AddRoleResponse = ValueOf<typeof addRoleResponse>;
/**
* Response returned when removing a role from a user.
*/
export type RemoveRoleResponse = ValueOf<typeof removeRoleResponse>;
/**
* Response returned when muting a user.
*/
export type MuteResponse = ValueOf<typeof muteResponse>;
/**
* Response returned when unmuting a user.
*/
export type UnmuteResponse = ValueOf<typeof unmuteResponse>;
/**
* Response returned when kicking a user.
*/
export type KickResponse = ValueOf<typeof kickResponse>;
/**
* Response returned when banning a user.
*/
export type BanResponse = ValueOf<typeof banResponse>;
/**
* Response returned when blocking a user.
*/
export type BlockResponse = ValueOf<typeof blockResponse>;
/**
* Response returned when unblocking a user.
*/
export type UnblockResponse = ValueOf<typeof unblockResponse>;
/**
* Response returned when timing out a user.
*/
export type TimeoutResponse = ValueOf<typeof timeoutResponse>;
/**
* Response returned when removing a timeout from a user.
*/
export type RemoveTimeoutResponse = ValueOf<typeof removeTimeoutResponse>;
export type PartialBushGuildMember = Partialize<BushGuildMember, 'joinedAt' | 'joinedTimestamp'>;
/**
* @typedef {BushClientEvents} VSCodePleaseDontRemove
*/
|