aboutsummaryrefslogtreecommitdiff
path: root/src/lib/extensions/discord.js/BushGuildMember.ts
blob: ac8fb54b5c62a1f0db4e3b1f472da1a19ebc1c74 (plain)
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
/* eslint-disable @typescript-eslint/no-unused-vars */
import {
	BushClientEvents,
	BushGuildTextBasedChannel,
	BushGuildTextChannelResolvable,
	BushThreadChannelResolvable,
	Moderation,
	ModLogType,
	type BushClient,
	type BushGuild,
	type BushRole,
	type BushUser
} from '#lib';
import { GuildMember, MessageEmbed, 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 the user to be punished.
	 * @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 punishDM(punishment: string, reason?: string | null, duration?: number, sendFooter = true): Promise<boolean> {
		const ending = await this.guild.getSetting('punishmentEnding');
		const dmEmbed =
			ending && ending.length && sendFooter
				? new MessageEmbed().setDescription(ending).setColor(util.colors.newBlurple)
				: undefined;
		const dmSuccess = await this.send({
			content: `You have been ${punishment} in **${this.guild.name}** ${
				duration !== null && duration !== undefined ? (duration ? `for ${util.humanizeDuration(duration)} ` : 'permanently ') : ''
			}for **${reason?.trim() ?? 'No reason provided'}**.`,
			embeds: dmEmbed ? [dmEmbed] : undefined
		}).catch(() => false);
		return !!dmSuccess;
	}

	/**
	 * 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 warn(options: BushPunishmentOptions): Promise<{ result: WarnResponse | null; 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 () => {
			// 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
				},
				true
			);
			caseID = result.log?.id;
			if (!result || !result.log) return { result: 'error creating modlog entry', caseNum: null };

			// dm user
			const dmSuccess = await this.punishDM('warned', options.reason);
			dmSuccessEvent = dmSuccess;
			if (!dmSuccess) return { result: 'failed to dm', caseNum: result.caseNum };

			return { result: 'success', caseNum: result.caseNum };
		})();
		if (!(['error creating modlog entry'] as const).includes(ret.result))
			client.emit('bushWarn', this, moderator, this.guild, options.reason ?? undefined, caseID!, dmSuccessEvent!);
		return ret as { result: WarnResponse | null; caseNum: number | null };
	}

	/**
	 * 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 addRole(options: AddRoleOptions): Promise<AddRoleResponse> {
		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
				});

				if (!modlog) return 'error creating modlog entry';
				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 'error creating role entry';
				}
			}

			const removeRoleSuccess = await this.roles.add(options.role, `${moderator.tag}`);
			if (!removeRoleSuccess) return 'error adding role';

			return 'success';
		})();
		if (
			!(['error adding role', 'error creating modlog entry', 'error creating role entry'] as const).includes(ret) &&
			options.addToModlog
		)
			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 removeRole(options: RemoveRoleOptions): Promise<RemoveRoleResponse> {
		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
				});

				if (!modlog) return 'error creating modlog entry';
				caseID = modlog.id;

				const punishmentEntrySuccess = await Moderation.removePunishmentEntry({
					type: 'role',
					user: this,
					guild: this.guild,
					extraInfo: options.role.id
				});

				if (!punishmentEntrySuccess) return 'error removing role entry';
			}

			const removeRoleSuccess = await this.roles.remove(options.role, `${moderator.tag}`);
			if (!removeRoleSuccess) return 'error removing role';

			return 'success';
		})();

		if (
			!(['error removing role', 'error creating modlog entry', 'error removing role entry'] as const).includes(ret) &&
			options.addToModlog
		)
			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) {
			client.console.debug(`${this.roles.highest.position} <= ${role.position}`);
			return 'user hierarchy';
		} else if (role.managed) {
			return 'role managed';
		} else if (this.guild.me!.roles.highest.position <= role.position) {
			return '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 mute(options: BushTimedPunishmentOptions): Promise<MuteResponse> {
		// checks
		if (!this.guild.me!.permissions.has('MANAGE_ROLES')) return 'missing permissions';
		const muteRoleID = await this.guild.getSetting('muteRole');
		if (!muteRoleID) return 'no mute role';
		const muteRole = this.guild.roles.cache.get(muteRoleID);
		if (!muteRole) return 'invalid mute role';
		if (muteRole.position >= this.guild.me!.roles.highest.position || muteRole.managed) return '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 'error giving mute role';

			// 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
			});

			if (!modlog) return 'error creating modlog entry';
			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 'error creating mute entry';

			// dm user
			const dmSuccess = await this.punishDM('muted', options.reason, options.duration ?? 0);
			dmSuccessEvent = dmSuccess;

			if (!dmSuccess) return 'failed to dm';

			return 'success';
		})();

		if (!(['error giving mute role', 'error creating modlog entry', 'error creating mute entry'] as const).includes(ret))
			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 unmute(options: BushPunishmentOptions): Promise<UnmuteResponse> {
		// checks
		if (!this.guild.me!.permissions.has('MANAGE_ROLES')) return 'missing permissions';
		const muteRoleID = await this.guild.getSetting('muteRole');
		if (!muteRoleID) return 'no mute role';
		const muteRole = this.guild.roles.cache.get(muteRoleID);
		if (!muteRole) return 'invalid mute role';
		if (muteRole.position >= this.guild.me!.roles.highest.position || muteRole.managed) return '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 'error removing mute role';

			// 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
			});

			if (!modlog) return 'error creating modlog entry';
			caseID = modlog.id;

			// remove mute entry
			const removePunishmentEntrySuccess = await Moderation.removePunishmentEntry({
				type: 'mute',
				user: this,
				guild: this.guild
			});

			if (!removePunishmentEntrySuccess) return 'error removing mute entry';

			// dm user
			const dmSuccess = await this.punishDM('unmuted', options.reason, undefined, false);
			dmSuccessEvent = dmSuccess;

			if (!dmSuccess) return 'failed to dm';

			return 'success';
		})();

		if (!(['error removing mute role', 'error creating modlog entry', 'error removing mute entry'] as const).includes(ret))
			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 '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 = await this.punishDM('kicked', options.reason);
			dmSuccessEvent = dmSuccess;

			// kick
			const kickSuccess = await this.kick(`${moderator?.tag} | ${options.reason ?? 'No reason provided.'}`).catch(() => false);
			if (!kickSuccess) return 'error kicking';

			// 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
			});
			if (!modlog) return 'error creating modlog entry';
			caseID = modlog.id;
			if (!dmSuccess) return 'failed to dm';
			return 'success';
		})();
		if (!(['error kicking', 'error creating modlog entry'] as const).includes(ret))
			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 '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 = await this.punishDM('banned', options.reason, options.duration ?? 0);
			dmSuccessEvent = dmSuccess;

			// ban
			const banSuccess = await this.ban({
				reason: `${moderator.tag} | ${options.reason ?? 'No reason provided.'}`,
				days: options.deleteDays
			}).catch(() => false);
			if (!banSuccess) return 'error banning';

			// 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
			});
			if (!modlog) return 'error creating modlog entry';
			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 'error creating ban entry';

			if (!dmSuccess) return 'failed to dm';
			return 'success';
		})();
		if (!(['error banning', 'error creating modlog entry', 'error creating ban entry'] as const).includes(ret))
			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 block(options: BlockOptions): Promise<BlockResponse> {
		const _channel = this.guild.channels.resolve(options.channel);
		if (!_channel || (!_channel.isText() && !_channel.isThread())) return 'invalid channel';
		const channel = _channel as BushGuildTextBasedChannel;

		// checks
		if (!channel.permissionsFor(this.guild.me!)!.has('MANAGE_CHANNELS')) return '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 'error blocking';

			// 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
			});
			if (!modlog) return 'error creating modlog entry';
			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 'error creating block entry';

			// 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() ?? 'No reason provided'}**.`
			}).catch(() => false);
			dmSuccessEvent = !!dmSuccess;

			if (!dmSuccess) return 'failed to dm';

			return 'success';
		})();

		if (!(['error creating modlog entry', 'error creating block entry', 'error blocking'] as const).includes(ret))
			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 unblock(options: UnblockOptions): Promise<UnblockResponse> {
		const _channel = this.guild.channels.resolve(options.channel);
		if (!_channel || (!_channel.isText() && !_channel.isThread())) return 'invalid channel';
		const channel = _channel as BushGuildTextBasedChannel;

		// checks
		if (!channel.permissionsFor(this.guild.me!)!.has('MANAGE_CHANNELS')) return '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 'error unblocking';

			// 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
			});
			if (!modlog) return 'error creating modlog entry';
			caseID = modlog.id;

			// remove punishment entry
			const punishmentEntrySuccess = await Moderation.removePunishmentEntry({
				type: 'block',
				user: this,
				guild: this.guild,
				extraInfo: channel.id
			});
			if (!punishmentEntrySuccess) return 'error removing block entry';

			// dm user
			const dmSuccess = await this.send({
				content: `You have been unblocked from <#${channel.id}> in **${this.guild.name}** for **${
					options.reason?.trim() ?? 'No reason provided'
				}**.`
			}).catch(() => false);
			dmSuccessEvent = !!dmSuccess;

			if (!dmSuccess) return 'failed to dm';

			return 'success';
		})();

		if (!(['error creating modlog entry', 'error removing block entry', 'error unblocking'] as const).includes(ret))
			client.emit(
				'bushUnblock',
				this,
				moderator,
				this.guild,
				options.reason ?? undefined,
				caseID!,
				dmSuccessEvent!,
				channel,
				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;
}

/**
 * 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;
}

export type PunishmentResponse = 'success' | 'error creating modlog entry' | 'failed to dm';

/**
 * Response returned when warning a user.
 */
export type WarnResponse = PunishmentResponse;

/**
 * Response returned when adding a role to a user.
 */
export type AddRoleResponse =
	| PunishmentResponse
	| 'user hierarchy'
	| 'role managed'
	| 'client hierarchy'
	| 'error creating role entry'
	| 'error adding role';

/**
 * Response returned when removing a role from a user.
 */
export type RemoveRoleResponse =
	| PunishmentResponse
	| 'user hierarchy'
	| 'role managed'
	| 'client hierarchy'
	| 'error removing role entry'
	| 'error removing role';

/**
 * Response returned when muting a user.
 */
export type MuteResponse =
	| PunishmentResponse
	| 'missing permissions'
	| 'no mute role'
	| 'invalid mute role'
	| 'mute role not manageable'
	| 'error giving mute role'
	| 'error creating mute entry';

/**
 * Response returned when unmuting a user.
 */
export type UnmuteResponse =
	| PunishmentResponse
	| 'missing permissions'
	| 'no mute role'
	| 'invalid mute role'
	| 'mute role not manageable'
	| 'error removing mute role'
	| 'error removing mute entry';

/**
 * Response returned when kicking a user.
 */
export type KickResponse = PunishmentResponse | 'missing permissions' | 'error kicking';

/**
 * Response returned when banning a user.
 */
export type BanResponse = PunishmentResponse | 'missing permissions' | 'error creating ban entry' | 'error banning';

/**
 * Response returned when blocking a user.
 */
export type BlockResponse =
	| PunishmentResponse
	| 'invalid channel'
	| 'error creating block entry'
	| 'missing permissions'
	| 'error blocking';

/**
 * Response returned when unblocking a user.
 */
export type UnblockResponse =
	| PunishmentResponse
	| 'invalid channel'
	| 'error removing block entry'
	| 'missing permissions'
	| 'error unblocking';

export type PartialBushGuildMember = Partialize<
	BushGuildMember,
	'joinedAt' | 'joinedTimestamp',
	'warn' | 'addRole' | 'removeRole' | 'mute' | 'unmute' | 'bushKick' | 'bushBan' | 'isOwner' | 'isSuperUser' | 'block'
>;

/**
 * @typedef {BushClientEvents} VSCodePleaseDontRemove
 */